前言:
一、单向多对多
单向多对多的例子用人和职位来举例,一个人可以有多个职位,一个职位会有多个人。单向多对多是指只能在一端来查询获取另一端的内容。多对多的关系在生成关系模型时会生成对象之前的关联表,关联表中存放着两个关系表的主键,它们的关系如下所示:
代码部分:
(1)映射和关系类
因为是单向的关系,所以只需要在一端进行维护,所以我们需要在User.hbm.xml配置文件中添加<many-to-many>标签,并在标签中加上对应的列关系,在<set>表中添加table属性来指明生成新表,User.hbm.xml代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
|
< span style = "font-family:KaiTi_GB2312;font-size:18px;" >< hibernate-mapping > < class name = "com.bjpowernode.hibernate.User" table = "t_user" > < id name = "id" > < generator class = "native" /> </ id > < property name = "name" /> < set name = "roles" table = "t_user_role" > < key column = "user_id" /> < many-to-many class = "com.bjpowernode.hibernate.Role" column = "role_id" /> </ set > </ class > </ hibernate-mapping ></ span > |
Role.hbm.xml代码比较简单,不需要添加多余的标签来维护关系:
1
2
3
4
5
6
7
8
|
< hibernate-mapping > < class name = "com.bjpowernode.hibernate.Role" table = "t_role" > < id name = "id" > < generator class = "native" /> </ id > < property name = "name" /> </ class > </ hibernate-mapping > |
因为user的映射中有set映射,所以需要在相应的类文件中添加Hashset,User.java代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
<span style= "font-family:KaiTi_GB2312;font-size:18px;" > import java.util.Set; public class User { private int id; private String name; private Set roles; public int getId() { return id; } public void setId( int id) { this .id = id; } public String getName() { return name; } public void setName(String name) { this .name = name; } public Set getRoles() { return roles; } public void setRoles(Set roles) { this .roles = roles; } }</span> |
Role.java代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
public class Role { private int id; private String name; public int getId() { return id; } public void setId( int id) { this .id = id; } public String getName() { return name; } public void setName(String name) { this .name = name; } } |
(2)添加和读取数据:
进行添加数据时,需要首先吧关系保存到数据库中,然后创建用户Hash表,在hash表中添加对应的关系,最后创建用户,将hash表添加到用户上。这部分需注意的是写入的先后顺序,否则会出现很多null值,和之前的映射一样的道理。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
public void testSave1() { Session session = null ; try { session = HibernateUtils.getSession(); session.beginTransaction(); Role r1 = new Role(); r1.setName( "数据录入人员" ); session.save(r1); Role r2 = new Role(); r2.setName( "商务主管" ); session.save(r2); Role r3 = new Role(); r3.setName( "商务经理" ); session.save(r3); Role r4 = new Role(); r4.setName( "项目会计" ); session.save(r4); User u1 = new User(); u1.setName( "张三" ); Set u1Roles = new HashSet(); u1Roles.add(r1); u1Roles.add(r2); u1.setRoles(u1Roles); session.save(u1); User u2 = new User(); u2.setName( "李四" ); Set u2Roles = new HashSet(); u2Roles.add(r1); u2Roles.add(r2); u2Roles.add(r3); u2.setRoles(u2Roles); session.save(u2); User u3 = new User(); u3.setName( "王五" ); Set u3Roles = new HashSet(); u3Roles.add(r3); u3Roles.add(r4); u3.setRoles(u3Roles); session.save(u3); session.getTransaction().commit(); } catch (Exception e) { e.printStackTrace(); session.getTransaction().rollback(); } finally { HibernateUtils.closeSession(session); } } |
读取时因为是单向关系,只需要通过一来读取另一端的内容,通过user来读取role的内容。代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
public void testLoad1() { Session session = null ; try { session = HibernateUtils.getSession(); session.beginTransaction(); User user = (User)session.load(User. class , 2 ); System.out.println(user.getName()); for (Iterator iter=user.getRoles().iterator(); iter.hasNext();) { Role role = (Role)iter.next(); System.out.println(role.getName()); } session.getTransaction().commit(); } catch (Exception e) { e.printStackTrace(); session.getTransaction().rollback(); } finally { HibernateUtils.closeSession(session); } } |
二、双向多对多映射
和之前介绍的一样,双向多对多就是在两端同时维护关系,从任何一端都能加载到另一端的内容,话不多说直接上代码:
因为是双向的所以需要同时加入双向的集合映射,在配置文件中添加<set>标签,添加多对多标签,Role.hbm.xml代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
|
<hibernate-mapping> < class name= "com.bjpowernode.hibernate.Role" table= "t_role" > <id name= "id" > <generator class = "native" /> </id> <property name= "name" /> <set name= "users" table= "t_user_role" > <key column= "role_id" not- null = "true" /> <many-to-many class = "com.bjpowernode.hibernate.User" column= "user_id" /> </set> </ class > </hibernate-mapping> |
User.hbm.xml代码如下,和单向映射代码是一样的:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
< span style = "font-family:KaiTi_GB2312;font-size:18px;" >< hibernate-mapping > < class name = "com.bjpowernode.hibernate.Role" table = "t_role" > < id name = "id" > < generator class = "native" /> </ id > < property name = "name" /> < set name = "users" table = "t_user_role" > < key column = "role_id" not-null = "true" /> < many-to-many class = "com.bjpowernode.hibernate.User" column = "user_id" /> </ set > </ class > </ hibernate-mapping > </ span > |
Role.java部分,和单向的user.java一样需要添加集合映射set,代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
import java.util.Set; public class Role { private int id; private String name; private Set users; public int getId() { return id; } public void setId( int id) { this .id = id; } public String getName() { return name; } public void setName(String name) { this .name = name; } public Set getUsers() { return users; } public void setUsers(Set users) { this .users = users; } } |
User.hbm.xml和User.java代码和上文中的代码相同,就不全部放上来了。
小结:
单向和多向通过几篇博客的介绍相信大家已经明白,我们只需要记住单向的双向的也就会了,挺简单的。
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
原文链接:http://blog.csdn.net/ww130929/article/details/54313397