码迷,mamicode.com
首页 > 其他好文 > 详细

多对一双向关联

时间:2014-11-09 12:31:23      阅读:256      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   color   ar   os   java   sp   

ps:双向关联与单向关联的区别在于

1、双向关联:A能推出B,B也能推出A。

  单向关联,只能由A推出B或者是B推出A。

例如:一个小组有多个员工,一个员工对应一个组。从员工的角度说是多对一,从小组的角度是一对多。

bubuko.com,布布扣
 1 package com.model;
 2 
 3 import java.util.Set;
 4 
 5 import javax.persistence.Entity;
 6 import javax.persistence.OneToMany;
 7 import javax.persistence.Table;
 8 
 9 @Entity
10 @Table(name="group")
11 public class Group {
12     private Integer id;
13     private String groupName;
14     private Set<Worker> workers;
15     
16     @OneToMany(mappedBy="group")
17     public Set<Worker> getWorkers() {
18         return workers;
19     }
20     public void setWorkers(Set<Worker> workers) {
21         this.workers = workers;
22     }
23     public Integer getId() {
24         return id;
25     }
26     public void setId(Integer id) {
27         this.id = id;
28     }
29     public String getGroupName() {
30         return groupName;
31     }
32     public void setGroupName(String groupName) {
33         this.groupName = groupName;
34     }
35 }
Group
bubuko.com,布布扣
  1 package com.model;
  2 
  3 import java.io.Serializable;
  4 import java.util.Date;
  5 
  6 import javax.persistence.Column;
  7 import javax.persistence.Entity;
  8 import javax.persistence.GeneratedValue;
  9 import javax.persistence.GenerationType;
 10 import javax.persistence.Id;
 11 import javax.persistence.JoinColumn;
 12 import javax.persistence.ManyToOne;
 13 import javax.persistence.Table;
 14 
 15 @Entity
 16 @Table(name="worker")
 17 public class Worker implements Serializable{
 18     /**
 19      * @author yzy
 20      * @2014年11月1日15:31:08
 21      */
 22     private static final long serialVersionUID = 1L;
 23     private Integer id;
 24     private String name;
 25     private String password;
 26     private Integer sex;
 27     private Integer age;
 28     private Date birthdate;
 29     private Group group;
 30     private String position;
 31     private String telephone;
 32     private Date enterDate;
 33     private Float performance;
 34     private Integer gold;
 35     
 36     public Worker(){
 37         
 38     }
 39     
 40     @Id @GeneratedValue(strategy=GenerationType.AUTO)
 41     @Column(name="worker_id")
 42     public Integer getId() {
 43         return id;
 44     }
 45     public void setId(Integer id) {
 46         this.id = id;
 47     }
 48     @Column(name="worker_name")
 49     public String getName() {
 50         return name;
 51     }
 52     public void setName(String name) {
 53         this.name = name;
 54     }
 55     @Column(name="worker_password")
 56     public String getPassword() {
 57         return password;
 58     }
 59     public void setPassword(String password) {
 60         this.password = password;
 61     }
 62     @Column(name="worker_sex")
 63     public Integer getSex() {
 64         return sex;
 65     }
 66     public void setSex(Integer sex) {
 67         this.sex = sex;
 68     }
 69     @Column(name="worker_age")
 70     public Integer getAge() {
 71         return age;
 72     }
 73     public void setAge(Integer age) {
 74         this.age = age;
 75     }
 76     @Column(name="worker_birthdate")
 77     public Date getBirthdate() {
 78         return birthdate;
 79     }
 80     public void setBirthdate(Date birthdate) {
 81         this.birthdate = birthdate;
 82     }
 83     @ManyToOne//多对一,一个员工只能在一个小组,一个小组有多名成员
 84     @JoinColumn(name="group_id")
 85     public Group getGroup() {
 86         return group;
 87     }
 88     public void setGroup(Group group) {
 89         this.group = group;
 90     }
 91     @Column(name="worker_position")
 92     public String getPosition() {
 93         return position;
 94     }
 95     public void setPosition(String position) {
 96         this.position = position;
 97     }
 98     @Column(name="worker_telephone")
 99     public String getTelephone() {
100         return telephone;
101     }
102     public void setTelephone(String telephone) {
103         this.telephone = telephone;
104     }
105     @Column(name="worker_enterDate")
106     public Date getEnterDate() {
107         return enterDate;
108     }
109     public void setEnterDate(Date enterDate) {
110         this.enterDate = enterDate;
111     }
112     @Column(name="worker_performance")
113     public Float getPerformance() {
114         return performance;
115     }
116     public void setPerformance(Float performance) {
117         this.performance = performance;
118     }
119     @Column(name="worker_gold")
120     public Integer getGold() {
121         return gold;
122     }
123     public void setGold(Integer gold) {
124         this.gold = gold;
125     }
126     
127     
128 }
Worker

一对多时在多的那一方设置一的外键,如上是在Worker中设置Group的外键。双向关联必设 mappedBy属性,总是由多的一方来维护。
举例:员工和组 一个group可以有多个worker,这里从group来说是一对多 而多个worker可以属于一个group 这里从worker来说是多对一 所以
不管是多对一还是一对多 都是在多的那方加外键。也就是说双向关联时在一的那一方设置mappedBy,即在Group实体中的getWorkers()方法上头加@OneToMany(mappedBy="group")

多对一双向关联

标签:style   blog   http   io   color   ar   os   java   sp   

原文地址:http://www.cnblogs.com/frankwin608/p/4084745.html

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