一对多的单向关联

例子:一个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.JoinColumn;
10 import javax.persistence.OneToMany;
11 import javax.persistence.Table;
12 
13 @Entity
14 @Table(name="t_group")
15 public class Group {
16     private Integer id;
17     
18     private String name;
19     
20     private Set<User> users = new HashSet<User>();
21 
22     @Id
23     @GeneratedValue
24     public Integer getId() {
25         return id;
26     }
27 
28     public void setId(Integer id) {
29         this.id = id;
30     }
31 
32     public String getName() {
33         return name;
34     }
35 
36     public void setName(String name) {
37         this.name = name;
38     }
39 
40     @OneToMany
41     @JoinColumn(name="groupId")//字段别名
42     public Set<User> getUsers() {
43         return users;
44     }
45 
46     public void setUsers(Set<User> users) {
47         this.users = users;
48     }
49 }

        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.Table;
 7 
 8 @Entity
 9 @Table(name="t_user")
10 public class User {
11     private Integer id;
12     
13     private String name;
14     
15     @Id
16     @GeneratedValue
17     public Integer getId() {
18         return id;
19     }
20 
21     public void setId(Integer id) {
22         this.id = id;
23     }
24 
25     public String getName() {
26         return name;
27     }
28 
29     public void setName(String name) {
30         this.name = name;
31     }
32 }

    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     public Integer getId() {
 9         return id;
10     }
11 
12     public void setId(Integer id) {
13         this.id = id;
14     }
15 
16     public String getName() {
17         return name;
18     }
19 
20     public void setName(String name) {
21         this.name = name;
22     }
23 }
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     </class>
13 </hibernate-mapping>

生成的SQL语句:

jar包链接: https://pan.baidu.com/s/1jIsm7Ng 密码: ixpe

代码链接: https://pan.baidu.com/s/1o7GQC4q 密码: y7mb

 

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