[java]代码库
package com.hibernate.model;import java.util.HashSet;import java.util.Set;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.JoinColumn;import javax.persistence.OneToMany;import javax.persistence.OneToOne;import javax.persistence.Table;@Entity@Table(name=_Group)public class Group {private int id;private String name;private Set
<user>
users = new HashSet
<user>
();@Id@GeneratedValuepublic int getId() { return id;}public void setId(int id) { this.id = id;}public String getName() { return name;}@OneToMany(mappedBy=group)//双向的必须写上mappedBy,而且在数据库方面考虑,关联关系应该设计在多的一方,所以必须在OneToMany这方设mappedBypublic Set
<user>
getUsers() { return users;}public void setUsers(Set
<user>
users) { this.users = users;}public void setName(String name) { this.name = name;}}
</user>
</user>
</user>
</user>//源代码片段来自云代码http://yuncode.net
package com.hibernate.model;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.ManyToOne;import javax.persistence.Table;@Entity@Table(name=_User)public class User {private int id;private String name;private Group group;@ManyToOnepublic Group getGroup() { return group;}public void setGroup(Group group) { this.group = group;}@Id@GeneratedValuepublic 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;}}//源代码片段来自云代码http://yuncode.net