标签:
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