当前位置:七道奇文章资讯编程技术Java编程
日期:2011-03-22 16:13:00  来源:本站整理

hibernate3学习笔记(九) Query及HQL[Java编程]

赞助商链接



  本文“hibernate3学习笔记(九) Query及HQL[Java编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:

示例代码以下:

1.package com.hb3.pack_01;
2.
3.import java.util.Iterator;
4.import java.util.List;
5. 6.import org.hibernate.Query;
7.import org.hibernate.Session;
8.import org.hibernate.SessionFactory;
9.import org.hibernate.Transaction;
10.import org.hibernate.cfg.Configuration;
11. 12.import com.hb3.pack_01.model.User;
13.import com.hb3.pack_01.model.UserBak;
14. 15.public class BusinessService {
16. 17. public static void main(String[] args) {
18.
19. Configuration config = new Configuration().configure();
20. SessionFactory sessionFactory = config.buildSessionFactory();
21. Session session = sessionFactory.openSession();
22.
23. Query query = session.createQuery("from com.hb3.pack_01.model.User user order by user.age desc, user.name");//[== from User] 24. printUserInfo(query.list());
25.
26. query = session.createQuery("select new com.hb3.pack_01.model.UserBak(user.name, user.age) from User as user");
27. Iterator<?> iterator = query.list().iterator();
28. System.out.println("class_name \t\t name/age");
29. while (iterator.hasNext()) {
30. UserBak userbak = (UserBak) iterator.next();
31. System.out.println(userbak.getClz() + " \t " + userbak.getName() + "/" + userbak.getAge());
32. }
33.
34. query = session.createQuery("select upper(user.name) from User as user where (user.age/2 >= ?) and (user.age is not null)");
35. query.setInteger(0, 13);
36. List<?> names = query.list();
37. iterator = names.iterator();
38. while(iterator.hasNext()) {
39. System.out.println(iterator.next());
40. }
41.
42. query = session.createQuery("select user.name, user.age from User as user where user.age > :minAge");
43. query.setInteger("minAge", 25);
44. names = query.list();
45. iterator = names.iterator();
46. while(iterator.hasNext()) {
47. Object[] obj = (Object[]) iterator.next();
48. System.out.println(obj[0] + "\t" + obj[1]);
49. }
50.
51. query = session.getNamedQuery("com.hb3.pack_01.model.User.QueryUser");
52. query.setInteger("minAge", 25);
53. printUserInfo(query.list());
54.
55. Transaction tx= session.beginTransaction();
56. query = session.createQuery("update User set age=24 where name='chenyan'");
57. query.executeUpdate();
58. tx.commit();
59. 60. tx= session.beginTransaction();
61. query = session.createQuery("delete User where name='yaobin'");
62. query.executeUpdate();
63. tx.commit();
64.
65. session.close();
66. sessionFactory.close();
67. }
68.
69. public static void printUserInfo(List<?> users){
70.
71. Iterator<?> iterator = users.iterator();
72. System.out.println("id \t name/age");
73. while (iterator.hasNext()) {
74. User user = (User) iterator.next();
75. System.out.println(user.getId() + " \t " + user.getName() + "/" + user.getAge());
76. }
77. }
78.}


  以上是“hibernate3学习笔记(九) Query及HQL[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:

  • 在Hibernate里面动态切换SChema实现访谒差别的数据库的几种办法
  • Hibernate 3新增XML关系长期性介绍
  • Hibernate配置文件在单元测试中的操纵
  • 在Hibernate中动态切换Schema
  • hibernate annoation (八 关联映射)
  • hibernate annoation (九 cascading)
  • <b>hibernate annoation (十 映射查询)</b>
  • hibernate annoation(十一 缓存Ehcache 采纳annoation)
  • Hibernate:操作配置文件生成数据库
  • hibernate annoation (一 加载)
  • hibernate annoation (二 成立表)
  • <b>hibernate annoation (三 id生成器)</b>
  • 本文地址: 与您的QQ/BBS好友分享!
    • 好的评价 如果您觉得此文章好,就请您
        0%(0)
    • 差的评价 如果您觉得此文章差,就请您
        0%(0)

    文章评论评论内容只代表网友观点,与本站立场无关!

       评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论
    Copyright © 2020-2022 www.xiamiku.com. All Rights Reserved .