码迷,mamicode.com
首页 > 移动开发 > 详细

Hibernate One-To-Many Unidirectional Mapping

时间:2015-10-24 07:54:01      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:

Group- User: one to many

In group class, we have a Set<User> users to store user;

 

1.Annotation

@Entity
@Table(name="x_Group")
public class Group {
    private int id;
    private String name;
    //for one-to-many illustration
    private Set<User> users=new HashSet<User>();
    
    @Id
    @GeneratedValue
    public int getId() {
        return id;
    }
    
    public void setId(int id) {
        this.id = id;
    }
    
    
    @OneToMany
    @JoinColumn(name="groupId")
    public Set<User> getUsers() {
        return users;
    }

    public void setUsers(Set<User> users) {
        this.users = users;
    }
 
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }    
}

@Entity
@Table(name="x_user")
public class User {
   private int id;
   private String name;
       
    @Id
    @GeneratedValue
    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.XML

 

<hibernate-mapping package="com.hibernate.model">
<class name="Group" table="T_Group">
   <id name="id"></id>
    <property name="name"></property>
    <set name="users">
      <key column="groupId"></key>
      <one-to-many class="com.hibernate.model.User"/>
    </set>
</class>

<hibernate-mapping package="com.hibernate.model">
<class name="User" table ="U_User">
   <id name="id"></id>
    <property name="name"></property>
</class>

 

Hibernate One-To-Many Unidirectional Mapping

标签:

原文地址:http://www.cnblogs.com/fifi043/p/4906121.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!