首先要感谢满江红社区:)给我们翻译了大量高质量的文档,我学习hibernate的文档资料主要来自他们的贡献。
对Hibernate概念模糊的一定要去看看 夏昕 写的《Hibernate 开发指南 》
一。Hibernate最小依赖包
手动添加每个项目的最小依赖包,并认识每个jar的大体作用是很有必要的:)
二、加载方式与核心类
1.加载Session
Session是持久层操作的基础,相当于JDBC中的 Connection。(注意Hibernate3中使用了与Hibernate2中不同的Session。老式的Session被迁移到org.hibernate.classic.Session)
Hibernate3以后应该使用如下的加载代码,可以获得绑定线程的Session变量。
1. public void
2. try
3. new
4. SessionFactory sf = cfg.buildSessionFactory();
5. session = sf.getCurrentSession();
6. catch
7. e.printStackTrace();
8. }
9. }
三。查询结构:
用一个RBAC的权限模型的一部分(USER,ROLE,USER_TO_ROLE )做例子
POJO如下:
1. package
2.
3. import
4. import
5.
6.
7. /**
8. * User entity. @author MyEclipse Persistence Tools
9. */
10.
11. public class User implements
12.
13.
14. // Fields
15.
16. private
17. private
18. private
19. private
20. private
21. private Set userToRoles = new HashSet(0);
22.
23.
24. // Constructors
25.
26. /** default constructor */
27. public
28. }
29.
30.
31. /** full constructor */
32. public
33. this.department = department;
34. this.loginId = loginId;
35. this.password = password;
36. this.name = name;
37. this.userToRoles = userToRoles;
38. }
39.
40.
41. // Property accessors
42.
43. public
44. return this.userId;
45. }
46.
47. public void
48. this.userId = userId;
49. }
50.
51. public
52. return this.department;
53. }
54.
55. public void
56. this.department = department;
57. }
58.
59. public
60. return this.loginId;
61. }
62.
63. public void
64. this.loginId = loginId;
65. }
66.
67. public
68. return this.password;
69. }
70.
71. public void
72. this.password = password;
73. }
74.
75. public
76. return this.name;
77. }
78.
79. public void
80. this.name = name;
81. }
82.
83. public
84. return this.userToRoles;
85. }
86.
87. public void
88. this.userToRoles = userToRoles;
89. }
90.
91.
92.
93.
94.
95.
96.
97.
98.
99. }
1. package
2.
3. import
4. import
5.
6. /**
7. * Role entity.
8. *
9. * @author MyEclipse Persistence Tools
10. */
11.
12. public class Role implements
13.
14. // Fields
15.
16. private
17. private
18. private
19. private Set userToRoles = new HashSet(0);
20. private Set roleToResources = new HashSet(0);
21.
22. // Constructors
23.
24. /** default constructor */
25. public
26. }
27.
28. /** full constructor */
29. public
30. Set roleToResources) {
31. this.roleName = roleName;
32. this.comment = comment;
33. this.userToRoles = userToRoles;
34. this.roleToResources = roleToResources;
35. }
36.
37. // Property accessors
38.
39. public
40. return this.roleId;
41. }
42.
43. public void
44. this.roleId = roleId;
45. }
46.
47. public
48. return this.roleName;
49. }
50.
51. public void
52. this.roleName = roleName;
53. }
54.
55. public
56. return this.comment;
57. }
58.
59. public void
60. this.comment = comment;
61. }
62.
63. public
64. return this.userToRoles;
65. }
66.
67. public void
68. this.userToRoles = userToRoles;
69. }
70.
71. public
72. return this.roleToResources;
73. }
74.
75. public void
76. this.roleToResources = roleToResources;
77. }
78.
79. }
1. package
2.
3. /**
4. * UserToRole entity.
5. *
6. * @author MyEclipse Persistence Tools
7. */
8.
9. public class UserToRole implements
10.
11. // Fields
12.
13. private
14. private
15. private
16. private
17.
18. // Constructors
19.
20. /** default constructor */
21. public
22. }
23.
24. /** full constructor */
25. public
26. this.user = user;
27. this.role = role;
28. this.projectId = projectId;
29. }
30.
31. // Property accessors
32.
33. public
34. return this.urId;
35. }
36.
37. public void
38. this.urId = urId;
39. }
40.
41. public
42. return this.user;
43. }
44.
45. public void
46. this.user = user;
47. }
48.
49. public
50. return this.role;
51. }
52.
53. public void
54. this.role = role;
55. }
56.
57. public
58. return this.projectId;
59. }
60.
61. public void
62. this.projectId = projectId;
63. }
64.
65. }
生成的映射为:
1. <?xml version="1.0" encoding="utf-8"?>
2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
3. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
4. <!--
5. Mapping file autogenerated by MyEclipse Persistence Tools
6. -->
7. <hibernate-mapping>
8. <class name="model.User" table="user" catalog="gov">
9. <id name="userId" type="java.lang.Integer">
10. <column name="USER_ID" />
11. <generator class="identity" />
12. </id>
13. <many-to-one name="department" class="model.Department" fetch="select">
14. <column name="DEPARTMENT_ID">
15. <comment>ËùÔÚ²¿ÃÅ</comment>
16. </column>
17. </many-to-one>
18. <property name="loginId" type="java.lang.String">
19. <column name="LOGIN_ID" length="20" />
20. </property>
21. <property name="password" type="java.lang.String">
22. <column name="PASSWORD" length="20" />
23. </property>
24. <property name="name" type="java.lang.String">
25. <column name="NAME" length="20" />
26. </property>
27. <set name="userToRoles" inverse="true">
28. <key>
29. <column name="USER_ID" />
30. </key>
31. <one-to-many class="model.UserToRole" />
32. </set>
33. </class>
34. </hibernate-mapping>
35.
1. <?xml version="1.0" encoding="utf-8"?>
2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
3. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
4. <!--
5. Mapping file autogenerated by MyEclipse Persistence Tools
6. -->
7. <hibernate-mapping>
8. <class name="model.Role" table="role" catalog="gov">
9. <id name="roleId" type="java.lang.Integer">
10. <column name="ROLE_ID" />
11. <generator class="identity" />
12. </id>
13. <property name="roleName" type="java.lang.String">
14. <column name="ROLE_NAME" length="40">
15. <comment>ְλÃû</comment>
16. </column>
17. </property>
18. <property name="comment" type="java.lang.String">
19. <column name="COMMENT" length="200">
20. <comment>ְλÃèÊö</comment>
21. </column>
22. </property>
23. <set name="userToRoles" inverse="true">
24. <key>
25. <column name="ROLE_ID" />
26. </key>
27. <one-to-many class="model.UserToRole" />
28. </set>
29. <set name="roleToResources" inverse="true">
30. <key>
31. <column name="ROLE_ID" />
32. </key>
33. <one-to-many class="model.RoleToResource" />
34. </set>
35. </class>
36. </hibernate-mapping>
37.
1. <?xml version="1.0" encoding="utf-8"?>
2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
3. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
4. <!--
5. Mapping file autogenerated by MyEclipse Persistence Tools
6. -->
7. <hibernate-mapping>
8. <class name="model.UserToRole" table="user_to_role" catalog="gov">
9. <id name="urId" type="java.lang.Integer">
10. <column name="UR_ID" />
11. <generator class="identity" />
12. </id>
13. <many-to-one name="user" class="model.User" fetch="select">
14. <column name="USER_ID" />
15. </many-to-one>
16. <many-to-one name="role" class="model.Role" fetch="select">
17. <column name="ROLE_ID" />
18. </many-to-one>
19. <property name="projectId" type="java.lang.Integer">
20. <column name="PROJECT_ID" />
21. </property>
22. </class>
23. </hibernate-mapping>
24.
下面做了个TestCase。分别描述了HQL和SQL方式的查询,至于Criteria方式虽然比较面向对象,但是我不是很习惯:)
1. package
2.
3. import
4.
5. import
6.
7. import
8. import
9. import
10. import
11. import
12. import
13. import
14. import
15. import
16.
17. public class
18.
19. null;
20.
21. @Before
22. // 读取classpath下的配置文件hibernate.cfg.xml
23. // current_session_context_class=thread,show_sql=true
24. public void
25. try
26. new
27. SessionFactory sf = cfg.buildSessionFactory();
28. session = sf.getCurrentSession();
29. catch
30. e.printStackTrace();
31. }
32. }
33.
34. /**
35. * 使用hql查询User_To_Role表中role_id字段
36. */
37. @Test
38. public void
39. session.beginTransaction();
40.
41. "select role.roleId from UserToRole";
42. List list1 = session.createQuery(hql_findRole).list();
43.
44. session.getTransaction().commit();
45. }
46.
47. /**
48. * 使用sql语法查询返回表中一个字段,返回Hibernate包装的类型
49. */
50. @Test
51. public void
52. session.beginTransaction();
53.
54. "select role_id from gov.user_to_role";
55. SQLQuery sq2 = session.createSQLQuery(sql_findRoleId).addScalar(
56. "role_id", new
57. List list2 = sq2.list();
58.
59. session.getTransaction().commit();
60.
61. }
62.
63. /**
64. * 使用sql语法查询表,返回Hibernate包装的对象
65. */
66. @Test
67. public void
68. session.beginTransaction();
69.
70. "select * from gov.role";
71. SQLQuery sq3 = session.createSQLQuery(sql_findRole).addEntity(
72. class);
73. List list3 = sq3.list();
74.
75. session.getTransaction().commit();
76.
77. }
78.
79. @After
80. public void
81. try
82. // 这里session由于绑定事务到当前线程,在提交后自动关闭。
83. // 如果去掉下面2行注释,再次关闭就报错。
84.
85. // if (session != null) {
86. // session.close();
87. // }
88. catch
89. e.printStackTrace();
90. }
91. }
92.
93. }
94.