标签:style blog class code java color
在数据库中数据的对应关系有一对一,一对多和多对一.那么在hibernate中应该如何对应呢?如下:
一对一:
假设两个实体,一个实体类为husband,另外一个实体类为wife.一个丈夫只能对应一个妻子,反之亦然.那么在hibernate中使用annotation如下:(husband类)
1 import javax.persistence.Entity; 2 import javax.persistence.GeneratedValue; 3 import javax.persistence.Id; 4 import javax.persistence.JoinColumn; 5 import javax.persistence.OneToOne; 6 7 @Entity 8 public class Husband { 9 private int id; 10 private String name; 11 private Wife wife; 12 13 @Id 14 @GeneratedValue 15 public int getId() { 16 return id; 17 } 18 public void setId(int id) { 19 this.id = id; 20 } 21 public String getName() { 22 return name; 23 } 24 public void setName(String name) { 25 this.name = name; 26 } 27 28 @OneToOne //表明是一对一 29 @JoinColumn(name="wifeId") //使用JoinColumn可以更改数据库中对应字段的名字 30 public Wife getWife() { 31 return wife; 32 } 33 public void setWife(Wife wife) { 34 this.wife = wife; 35 } 36 }
wife类:
1 import javax.persistence.Entity; 2 import javax.persistence.GeneratedValue; 3 import javax.persistence.Id; 4 5 @Entity 6 public class Wife { 7 private int id; 8 private String name; 9 10 @Id 11 @GeneratedValue 12 public int getId() { 13 return id; 14 } 15 public void setId(int id) { 16 this.id = id; 17 } 18 public String getName() { 19 return name; 20 } 21 public void setName(String name) { 22 this.name = name; 23 } 24 }
一对多:
设想一个小组有多个成员,而每一个成员只能加入一个小组.在设计数据的时候,按照常理,人们会把小组的Id设到每个成员中,作为外键;下面分两种情况:
1.将小组Id设到成员中:
1 import javax.persistence.Entity; 2 import javax.persistence.GeneratedValue; 3 import javax.persistence.Id; 4 import javax.persistence.Table; 5 6 @Entity 7 @Table(name="t_Group") //这里注意:因为group是关键字,所以需要将表名更改 8 public class Group { 9 private int id; 10 private String name; 11 12 public String getName() { 13 return name; 14 } 15 public void setName(String name) { 16 this.name = name; 17 } 18 @Id 19 @GeneratedValue 20 public int getId() { 21 return id; 22 } 23 public void setId(int id) { 24 this.id = id; 25 } 26 }
Person类:
1 import javax.persistence.Entity; 2 import javax.persistence.GeneratedValue; 3 import javax.persistence.Id; 4 import javax.persistence.ManyToOne; 5 6 @Entity 7 public class Person { 8 private Group group; 9 private int id; 10 private String name; 11 12 @ManyToOne //表明多个Person对应一个Group 13 public Group getGroup() { 14 return group; 15 } 16 17 @Id 18 @GeneratedValue 19 public int getId() { 20 return id; 21 } 22 public String getName() { 23 return name; 24 } 25 public void setGroup(Group group) { 26 this.group = group; 27 } 28 public void setId(int id) { 29 this.id = id; 30 } 31 public void setName(String name) { 32 this.name = name; 33 } 34 }
2.将person的Id作为外键设到group中.方法类似于上面.只需将person属性添加到group中并在get方法上添加注解@OneToMany和@JoinColumn(name="groupId"),另外:因为把person添加到group中去了,那么在group实体类中person的返回类型应该是一个数组,Map,Set或者List,个人觉得Set比较好,因为set中元素没有重复的,符合数据库中的规律.所以你的代码应该写成类似这样:
1 private Set<Person> person = new HashSet<Person>(); 2 3 @OneToMany 4 @JoinColumn(name="personId") 5 public Set<Person> getPerson() { 6 return person; 7 } 8 public void setPerson(Set<Person> person) { 9 this.person = person; 10 }
3.多对多就很简单了,分解为多对一即可.这里就不一一赘述了.
标签:style blog class code java color
原文地址:http://www.cnblogs.com/zhouxuanyu/p/3716831.html