熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

Hibernate高級查詢實戰[1]

2022-06-13   來源: Java開源技術 

  大家知道在Hibernate Framework中提供了三種查詢數據的方式

  Hibernate Query Language (HQL)

  它是ANSI SQL的最小OO Dialect

sessioncreateQuery(from Category c where cname like Laptop%);
                     entityManagercreateQuery(select c from Category c where cname like Laptop%);

  Criteria query

  它是HQL查詢的延伸提供了一些高級查詢功能例

sessioncreateCriteria(Categoryclass)
                add( Restrictionslike(name Laptop%) );

  Native SQL query :

sessioncreateSQLQuery(
select {c*} from CATEGORY {c} where NAME like Laptop%
)addEntity(c Categoryclass);

  最近我在項目中經常遇到Hibernate數據查詢的任務我對一些我遇到的較難的課題舉例進行說明

  《表述SELECT》

Criteria crit = sessioncreateCriteria(Userclass)
setProjection( ProjectionsprojectionList()
add( Projectionsproperty(lastname))
add( Projectionsproperty(firstname))
list();

  《表述WHERE》

Criteria crit = sessioncreateCriteria(Userclass)
add(Restrictionseq(email foo@hibernateorg))
uniqueResult();

  《表述GROUP》

Criteria crit = sessioncreateCriteria(Userclass)
setProjection( ProjectionsprojectionList()
add( ProjectionsgroupProperty(lastname))
add( ProjectionsgroupProperty(firstname))
list()

  《表述ORDER》

Criteria crit = sessioncreateCriteria(Userclass)
addOrder( Orderasc(lastname) )
addOrder( Orderasc(firstname) )
list();

[]  []  


From:http://tw.wingwit.com/Article/program/Java/ky/201311/29154.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.