hibernate3学习笔记(九) Query及HQL - 编程入门网
ge1 shenbin/282 chenyan/269 shenyi/2310 yangye/2311 chenyong/2213 chendong/null12 yaobin/nullHibernate: select user0_.name as col_0_0_, user0_.age as col_1_0_ from user user0_class_name name/ageUserBak shenbin/28UserBak chenyan/26UserBak shenyi/23UserBak yangye/23UserBak chenyong/22UserBak yaobin/nullUserBak chendong/nullHibernate: select upper(user0_.name) as col_0_0_ from user user0_ where user0_.age/2>=? and (user0_.age is not null)SHENBINCHENYANHibernate: select user0_.name as col_0_0_, user0_.age as col_1_0_ from user user0_ where user0_.age>?shenbin 28chenyan 26Hibernate: select user0_.id as id0_, user0_.name as name0_, user0_.age as age0_ from user user0_ where user0_.age>?id name/age1 shenbin/282 chenyan/26Hibernate: update user set age=24 where name=''chenyan''Hibernate: delete from user where name=''yaobin''
注意点: 1.在Hibernate3中,可以直接使用HQL指定更新或刪除。 2.使用HQL可以自定义返回的类型。(如:UserBak) 3.使用HQL可以结合各种查询条件,使用where子句限定查询条件,除了 = 运算之外,还有 >、>=、<、<=、!= 或 <>等比较运算: Query query = session.createQuery("from User user where user.age between 20 and 30"); Query query = session.createQuery("from User user where user.name in(''catERPillar'', ''momor'')"); Query query = session.createQuery("from User user where user.name like ''cater%''"); Query query = session.createQuery("select avg(user.age) from User as user"); Query query = session.createQuery("select count(*) from User as user"); 也可以配合GROUP BY子句,假设在数据库表中追加一个Sex字段。 Query query = session.createQuery("select user.sex, avg(user.age) from User user group by user.sex"); 运行结果可能是:
可以結合having子句,例如只将平均年龄大于20的资料分组显示出来: Query query = session.createQuery("select user.sex, avg(user.age) from User user group by user.sex having avg(user.age) > 20"); 使用HQL是官方推荐的查询方式。 |
凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢! |