HQL查询及语法Word文件下载.docx
《HQL查询及语法Word文件下载.docx》由会员分享,可在线阅读,更多相关《HQL查询及语法Word文件下载.docx(7页珍藏版)》请在冰豆网上搜索。
查询示例:
publicclassHqlQuery
...{
publicstaticvoidmain(String[]args)throwsException...{
HqlQuerymgr=newHqlQuery();
//调用查询方法
mgr.findPersons();
//调用第二个查询方法
mgr.findPersonByHappenDate();
HibernateUtil.sessionFactory.close();
}
//第一个查询方法
privatevoidfindPersons()...{
//获得HibernateSession
Sessionsess=HibernateUtil.currentSession();
//开始事务
Transactiontx=sess.beginTransaction();
//以HQL语句创建Query对象
//执行setString方法为HQL语句的参数赋值
//Query调用list方法访问查询的全部实例
Listp1=sess.createQuery("
fromPersonpwhereo.myEvents.title=:
eventTitle"
).setString("
"
很普通事情"
).list();
//遍历查询的全部结果
for(Iteratorpit=p1.iterator();
pit.haxNext();
)
Personp=(Person)pit.next();
System.out.println(p.getName());
//提交事务
mit();
HibernateUtil.closeSession();
//第二个查询方法
privatevoidfindPersonByHappenDate()throwsException...{
//解析出Date对象
SimpleDateFormatsdf=newSimpleDateFormat("
yyyy-MM-dd"
);
Datestart=sdf.parse("
2007-11-27"
System.out.println("
系统开始通过日期查找人"
+start);
//通过Session的createQuery方法创建Query对象
//设置参数
//返回结果集
Listpl=sess.createQuery(
"
fromPersonpwherep.myEvents.happenDatebetween:
firstDate
and:
endDate"
)
.setDate("
firstDate"
start)
newDate())
.list();
//遍历结果集
for(Iteratorpit=pl.iterator();
pit.hasNext();
$下面介绍HQL语句的语法
1.from子句
fromPerson
表明从Person持久化类中选出全部的实例。
推荐:
fromPersonasp
2.select子句
selectp.namefromPersonasp
selectp.name.firstNamefromPersonasp
selectnewlist(p.name,p.address)fromPersonasp
selectnewClassTest(p.name,p.address)fromPersonasp(有前提)
selectp.nameaspersonNamefromPersonasp
selectnewmap(p.nameaspersonName)fromPersonasp(与newmap()结合更普遍)
3.聚集函数
avg,count,max,min,sum
selectcount(*)fromPerson
selectmax(p.age)fromPersonasp
selectp.name||"
||p.addressfromPersonasp
4.多态查询
fromjava.lang.Objecto
fromNamedasn
5.where子句
fromPersonwherenamelike"
tom%"
fromPersonaspwherep.namelike"
fromCatcatwherecat.mate.namelike"
kit%"
select*fromcat_tableastable1cat_tableastable2wheretable1.mate=
table2.idandtable1.namelike"
fromFoofoowherefoo.bar.baz.customer.address.citylike"
fuzhou%"
fromCatcat,Catrivalwherecat.mate=rival.mate
selectcat,mate
fromCatcat,Catmate
wherecat.mate=mate
fromCatascatwherecat.id=123
fromCatascatwherecat.mate.id=69
fromPersonasperson
whereperson.id.country='
AU'
andperson.id.medicareNumber=123456
fromAccountasaccount
whereaccount.owner.id.country='
andaccount.owner.id.medicareNumber=123456
fromCatcatwherecat.class=DomesticCat
fromAccountasawherea.person.name.firstNamelike"
dd%"
//正确
fromAccountasawherea.person.namelike"
//错误
6.表达式
fromDomesticCatcatwherecat.namebetween'
A'
and'
B'
fromDomesticCatcatwherecat.namein('
Foo'
'
Bar'
Baz'
fromDomesticCatcatwherecat.namenotbetween'
fromDomesticCatcatwherecat.namenotin('
fromDomesticCatcatwherecat.nameisnull
fromPersonaspwherep.addressisnotnull
<
propertyname="
hibernate.query.substitutions"
>
true1,false0<
/property>
fromCatcatwherecat.alive=true
fromCatcatwherecat.kittens.size>
0
fromCatcatwheresize(cat.kittens)>
fromCalendarcalwheremaxelement(cal.holidays)>
currentdate
fromOrderorderwheremaxindex(order.items)>
100
fromOrderorderwhereminelement(order.items)>
10000
//操作集合元素
selectmotherfromCatasmother,Cataskit
wherekitinelements(foo.kittens)
//p的name属性等于集合中某个元素的name属性
selectpfromNameListlist,Personp
wherep.name=someelements(list.names)
fromCatcatwhereexistselements(cat.kittens)
fromPlayerpwhere3>
allelements(p.scores)
fromShowshowwhere'
fizard'
inindices(show.act