雙向多對多關(guān)聯(lián)映射原理:
假設(shè),一個員工可能有多個角色,一個角色可能有多個員工,從員工或角色的角度看,這就是多對多的關(guān)系,不管從哪一個角度看,都是多對多的聯(lián)系。多對多關(guān)聯(lián)映射關(guān)系一般采用中間表的形式來實現(xiàn),即新增一種包含關(guān)聯(lián)雙方主鍵的表。實現(xiàn)多對多關(guān)聯(lián)關(guān)系,在數(shù)據(jù)庫底層通過添加中間表指定關(guān)聯(lián)關(guān)系,而在hibernate框架在雙方的實體中添加一個保存對方的集合,在雙方的映射文件中使用<set>元素和<many-to-many>元素進行關(guān)聯(lián)關(guān)系的配置。
如下圖所示:
(1)XML版
Role類:
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
|
package Hibernate_demo1.Demo15.Entity; import java.util.Set; public class Role { private String id; private String rame; private Set<User> users; public String getId() { return id; } public void setId(String id) { this .id = id; } public String getRame() { return rame; } public void setRame(String rame) { this .rame = rame; } public Set<User> getUsers() { return users; } public void setUsers(Set<User> users) { this .users = users; } } |
User類:
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
|
package Hibernate_demo1.Demo15.Entity; import java.util.Set; public class User { private String id; private String uname; private Set<Role> roles; public String getId() { return id; } public void setId(String id) { this .id = id; } public String getUname() { return uname; } public void setUname(String uname) { this .uname = uname; } public Set<Role> getRoles() { return roles; } public void setRoles(Set<Role> roles) { this .roles = roles; } } |
Role.hbm.xml映射類:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<? xml version = "1.0" ?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> < hibernate-mapping > < class name = "Hibernate_demo1.Demo15.Entity.Role" table = "role" > < id name = "id" type = "java.lang.String" > < column name = "id" /> < generator class = "uuid" > </ generator > </ id > < property name = "rame" column = "rname" /> < set name = "users" table = "user_role" > < key column = "roleid" ></ key > < many-to-many class = "Hibernate_demo1.Demo15.Entity.User" column = "userid" ></ many-to-many > </ set > </ class > </ hibernate-mapping > |
User.hbm.xml映射類:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<? xml version = "1.0" ?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> < hibernate-mapping > < class name = "Hibernate_demo1.Demo15.Entity.User" table = "user" > < id name = "id" > < generator class = "uuid" > </ generator > </ id > < property name = "uname" column = "uname" /> < set name = "roles" table = "user_role" > < key column = "userid" /> < many-to-many class = "Hibernate_demo1.Demo15.Entity.Role" column = "roleid" /> </ set > </ class > </ hibernate-mapping > |
測試類:
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
package Hibernate_demo1.Demo15; import java.util.HashSet; import java.util.Set; import org.hibernate.Session; import Hibernate_demo1.Demo15.Entity.Role; import Hibernate_demo1.Demo15.Entity.User; import Hibernate_demo1.Demo15.Util.HibernateUtils; public class App { public static void main( String[] args ) { Session session = null ; try { session = HibernateUtils.getSession(); session.beginTransaction(); Role r1 = new Role(); r1.setRame( "數(shù)據(jù)錄入人員" ); session.save(r1); Role r2 = new Role(); r2.setRame( "商務(wù)主管" ); session.save(r2); Role r3 = new Role(); r3.setRame( "商務(wù)經(jīng)理" ); session.save(r3); Role r4 = new Role(); r4.setRame( "項目會計" ); session.save(r4); User u1 = new User(); u1.setUname( "張三" ); Set<Role> u1Roles = new HashSet<Role>(); u1Roles.add(r1); u1Roles.add(r2); u1.setRoles(u1Roles); session.save(u1); User u2 = new User(); u2.setUname( "李四" ); Set<Role> u2Roles = new HashSet<Role>(); u2Roles.add(r1); u2Roles.add(r2); u2Roles.add(r3); u2.setRoles(u2Roles); session.save(u2); User u3 = new User(); u3.setUname( "王五" ); Set<Role> u3Roles = new HashSet<Role>(); 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); } } } |
執(zhí)行結(jié)果:
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
Hibernate: insert into role (rname, id) values (?, ?) Hibernate: insert into role (rname, id) values (?, ?) Hibernate: insert into role (rname, id) values (?, ?) Hibernate: insert into role (rname, id) values (?, ?) Hibernate: insert into user (uname, id) values (?, ?) Hibernate: insert into user (uname, id) values (?, ?) Hibernate: insert into user (uname, id) values (?, ?) Hibernate: insert into user_role (userid, roleid) values (?, ?) Hibernate: insert into user_role (userid, roleid) values (?, ?) Hibernate: insert into user_role (userid, roleid) values (?, ?) Hibernate: insert into user_role (userid, roleid) values (?, ?) Hibernate: insert into user_role (userid, roleid) values (?, ?) Hibernate: insert into user_role (userid, roleid) values (?, ?) Hibernate: insert into user_role (userid, roleid) values (?, ?) |
數(shù)據(jù)庫內(nèi)容為:
user表:
role表:
user_role表:
(2)注解版
Role類:
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
|
package Hibernate_demo1.Demo16.Entity; import java.util.HashSet; import java.util.Set; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.ManyToMany; import javax.persistence.Table; import org.hibernate.annotations.GenericGenerator; @Entity @Table (name= "role" ) public class Role { @Id @GenericGenerator (name= "uuidGenerator" , strategy= "uuid" ) @GeneratedValue (generator= "uuidGenerator" ) private String id; @Column (name= "rname" ) private String rame; @ManyToMany (cascade=CascadeType.REFRESH,mappedBy= "roles" ) private Set<User> users= new HashSet<User>(); public String getId() { return id; } public void setId(String id) { this .id = id; } public String getRame() { return rame; } public void setRame(String rame) { this .rame = rame; } public Set<User> getUsers() { return users; } public void setUsers(Set<User> users) { this .users = users; } } |
User類:
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
56
57
58
59
60
61
62
|
package Hibernate_demo1.Demo16.Entity; import java.util.HashSet; import java.util.Set; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinTable; import javax.persistence.ManyToMany; import javax.persistence.Table; import javax.persistence.JoinColumn; import org.hibernate.annotations.GenericGenerator; @Entity @Table (name= "user" ) public class User { @Id @GenericGenerator (name= "uuidGenerator" , strategy= "uuid" ) @GeneratedValue (generator= "uuidGenerator" ) private String id; @Column (name= "uname" ) private String uname; /* * @ManyToMany表示多對多關(guān)聯(lián),對于這種關(guān)聯(lián)極少數(shù)情況會使用級聯(lián)刪除,我這里設(shè)置的是級聯(lián)刷新; * 因為有中間表的存在這里使用@JoinTable來設(shè)置關(guān)聯(lián)表后面的name配置的是關(guān)聯(lián)表的名稱 * inverseJoinColumn配置的是關(guān)系被維護一方主鍵對應(yīng)的中間表字段 * joinColumn配置的是關(guān)系維護方主鍵對應(yīng)的中間表字段。 */ @ManyToMany (cascade=CascadeType.REFRESH) @JoinTable (name= "user_role" ,inverseJoinColumns= @JoinColumn (name= "roleid" ),joinColumns= @JoinColumn (name= "userid" )) private Set<Role> roles= new HashSet<Role>(); public String getId() { return id; } public void setId(String id) { this .id = id; } public String getUname() { return uname; } public void setUname(String uname) { this .uname = uname; } public Set<Role> getRoles() { return roles; } public void setRoles(Set<Role> roles) { this .roles = roles; } } |
測試類:
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
56
57
58
59
60
61
62
63
64
65
66
67
68
|
package Hibernate_demo1.Demo16; import java.util.HashSet; import java.util.Set; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import Hibernate_demo1.Demo16.Entity.Role; import Hibernate_demo1.Demo16.Entity.User; public class Test { public static void main(String[] args) { SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); Session session = sessionFactory.getCurrentSession(); Transaction tx = session.beginTransaction(); //創(chuàng)建兩個用戶 User us1= new User(); us1.setUname( "小明" ); User us2= new User(); us2.setUname( "小黑" ); //創(chuàng)建用戶集合 Set<User> su= new HashSet<User>(); su.add(us1); su.add(us2); //創(chuàng)建兩個角色 Role ro1= new Role(); ro1.setRame( "程序員" ); ro1.setUsers(su); Role ro2= new Role(); ro2.setRame( "技術(shù)經(jīng)理" ); ro2.setUsers(su); //創(chuàng)建角色集合 Set<Role> sr= new HashSet<Role>(); sr.add(ro1); sr.add(ro2); //往用戶添加角色集合 us1.setRoles(sr); us2.setRoles(sr); //保存用戶和角色 session.save(us1); session.save(us2); session.save(ro1); session.save(ro2); tx.commit(); session.close(); } } |
執(zhí)行結(jié)果如下:
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
56
|
Hibernate: insert into user (uname, id) values (?, ?) Hibernate: insert into user (uname, id) values (?, ?) Hibernate: insert into role (rname, id) values (?, ?) Hibernate: insert into role (rname, id) values (?, ?) Hibernate: insert into user_role (userid, roleid) values (?, ?) Hibernate: insert into user_role (userid, roleid) values (?, ?) Hibernate: insert into user_role (userid, roleid) values (?, ?) Hibernate: insert into user_role (userid, roleid) values (?, ?) |
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://blog.csdn.net/u011781521/article/details/71335507?utm_source=tuicool&utm_medium=referral