Hibernate查詢方式---HQL方式

來源:互聯網
上載者:User

標籤:style   blog   class   code   java   c   

這一種我最常用,也是最喜歡用的,因為它寫起來靈活直觀,而且與所熟悉的SQL的文法差不太多。條件查詢、分頁查詢、串連查詢、巢狀查詢,寫起來與SQL文法基本一致,唯一不同的就是把表名換成了類或者對象。其它的,包括一些查詢函數(count(),sum()等)、查詢條件的設定等,全都跟SQL文法一樣。
###注意:
     在hql中關鍵字不區分大小寫,但是屬性和類名區分大小寫
樣本1:

static void query(String name){  Session s=null;  try{   s=HibernateUtil.getSession();   //from後面是對象,不是表名   String hql="from Admin as admin where admin.aname=:name";//使用具名引數,推薦使用,易讀。   Query query=s.createQuery(hql);   query.setString("name", name);   List<Admin> list=query.list();   for(Admin admin:list){    System.out.println(admin.getAname());  }  }finally{   if(s!=null)   s.close(); }}

######!!!!!!!!!!!!!對於多對一關聯性查詢:
String hql = "from Student where Class.className = ‘二班‘";
(Student實體類中含有Class對象的引用。這樣相當於兩張表的聯集查詢)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.