多对一(一对多)的双向关联

一个组(Group)有多个用户(User),进行多对一(一对多)的双向配置,在数据库中生成的表结构如下图:

  Annotation配置:

      Group:

 1 package com.bjsxt.hibernate;
 2 
 3 import java.util.HashSet;
 4 import java.util.Set;
 5 
 6 import javax.persistence.Entity;
 7 import javax.persistence.GeneratedValue;
 8 import javax.persistence.Id;
 9 import javax.persistence.OneToMany;
10 import javax.persistence.Table;
11 
12 @Entity
13 @Table(name="t_group")
14 public class Group {
15     private Integer id;
16     
17     private String name;
18     
19     private Set<User> users = new HashSet<User>();
20 
21     @Id
22     @GeneratedValue
23     public Integer getId() {
24         return id;
25     }
26 
27     public void setId(Integer id) {
28         this.id = id;
29     }
30 
31     public String getName() {
32         return name;
33     }
34 
35     public void setName(String name) {
36         this.name = name;
37     }
38 
39     @OneToMany(mappedBy="group")//表示和User是一对多的关系,并且以User的group属性为准,自己就不做映射配置了
40     public Set<User> getUsers() {
41         return users;
42     }
43 
44     public void setUsers(Set<User> users) {
45         this.users = users;
46     }
47 }

      User:

 1 package com.bjsxt.hibernate;
 2 
 3 import javax.persistence.Entity;
 4 import javax.persistence.GeneratedValue;
 5 import javax.persistence.Id;
 6 import javax.persistence.JoinColumn;
 7 import javax.persistence.ManyToOne;
 8 import javax.persistence.Table;
 9 
10 @Entity
11 @Table(name="t_user")
12 public class User {
13     private Integer id;
14     
15     private String name;
16     
17     private Group group;
18     
19     @Id
20     @GeneratedValue
21     public Integer getId() {
22         return id;
23     }
24 
25     public void setId(Integer id) {
26         this.id = id;
27     }
28 
29     public String getName() {
30         return name;
31     }
32 
33     public void setName(String name) {
34         this.name = name;
35     }
36 
37     @ManyToOne
38     @JoinColumn(name="group_ID")//指定列名为 group_ID
39     public Group getGroup() {
40         return group;
41     }
42 
43     public void setGroup(Group group) {
44         this.group = group;
45     }
46 }

    XML 配置:

      Group:

 1 package com.bjsxt.hibernate;
 2 
 3 import java.util.HashSet;
 4 import java.util.Set;
 5 
 6 public class Group {
 7     private Integer id;
 8     
 9     private String name;
10     
11     private Set<User> users = new HashSet<User>();
12 
13     public Integer getId() {
14         return id;
15     }
16 
17     public void setId(Integer id) {
18         this.id = id;
19     }
20 
21     public String getName() {
22         return name;
23     }
24 
25     public void setName(String name) {
26         this.name = name;
27     }
28 
29     public Set<User> getUsers() {
30         return users;
31     }
32 
33     public void setUsers(Set<User> users) {
34         this.users = users;
35     }
36 }
View Code

      Group.hbm.xml:

 1 <?xml version="1.0"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC
 3         "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4         "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 5 
 6 <hibernate-mapping package="com.bjsxt.hibernate">
 7     <class name="Group" table="t_group">
 8         <id name="id" column="id">
 9             <generator class="native"/>
10         </id>
11         <property name="name" column="name" />
12         <set name="users">
13             <key column="groupId"></key>
14             <one-to-many class="User"/>
15         </set>
16     </class>
17 </hibernate-mapping>

      User:

 1 package com.bjsxt.hibernate;
 2 
 3 public class User {
 4     private Integer id;
 5     
 6     private String name;
 7     
 8     private Group group;
 9     
10     public Integer getId() {
11         return id;
12     }
13 
14     public void setId(Integer id) {
15         this.id = id;
16     }
17 
18     public String getName() {
19         return name;
20     }
21 
22     public void setName(String name) {
23         this.name = name;
24     }
25 
26     public Group getGroup() {
27         return group;
28     }
29 
30     public void setGroup(Group group) {
31         this.group = group;
32     }
33 }
View Code

      User.hbm.xml

 1 <?xml version="1.0"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC
 3         "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4         "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 5 
 6 <hibernate-mapping package="com.bjsxt.hibernate">
 7     <class name="User" table="t_user">
 8         <id name="id" column="id">
 9             <generator class="native"/>
10         </id>
11         <property name="name" column="name" />
12         <many-to-one name="group" column="groupId"></many-to-one>
13     </class>
14 </hibernate-mapping>

    两个配置文件的 groupId 必须取名一致,否则会出现两个外键。

 

两种方式最后生成的SQL语句:

jar包链接: https://pan.baidu.com/s/1eSuw9Aq 密码: zrsy

开发链接: https://pan.baidu.com/s/1hsA73Ju 密码: 3q2b

posted @ 2017-04-20 11:04  流年如水烟雨随风  阅读(1069)  评论(0编辑  收藏  举报