Hibernate---一对多,单向和双向注解

  

     一对多单向:

  

import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
@Entity
public class Department {
 private long id;
 private String name;
 private Set<Employee> emps;
 @Id
 @GeneratedValue
 public long getId() {
  return id;
 }
 public void setId(long id) {
  this.id = id;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 @OneToMany(cascade = CascadeType.ALL)
 @JoinColumn(name = "dept_id")//不加该注解,则会生成中间表。加上该注解则会在多方的表中生成一列"dept_id"
 public Set<Employee> getEmps() {
  return emps;
 }
 public void setEmps(Set<Employee> emps) {
  this.emps = emps;
 }
}

 

    

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Transient;
@Entity
public class Employee {
 private long id;
 private Department depart;
 private String name;
 @Id
 @GeneratedValue
 public long getId() {
  return id;
 }
 public void setId(long id) {
  this.id = id;
 }
 
 @Transient//不插入数据库
 public Department getDepart() {
  return depart;
 }
 public void setDepart(Department depart) {
  this.depart = depart;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
}

    注意上面这个注解,在被维护方(一方)的@JoinColumn要name是维护方以数据库表形式存在的ID,除了有一个@JoinColumn外也可以有一个referencedColumnName这个属性

 

    上面是一对多其中一种方式,另外一种就是在维护端设计关联关系,代码如下:

 

import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
@Entity
public class Department {
 private long id;
 private String name;
 @Id
 @GeneratedValue
 public long getId() {
  return id;
 }
 public void setId(long id) {
  this.id = id;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }

}

    

 

     

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Transient;
@Entity
public class Employee {
 private long id;
 private Department depart;
 private String name;
 @Id
 @GeneratedValue
 public long getId() {
  return id;
 }
 public void setId(long id) {
  this.id = id;
 }
 
@ManyToOne(cascade={CascadeType.PERSIST, CascadeType.MERGE}, fetch=FetchType.LAZY)
    @JoinColumn(name="dept_id")
 public Department getDepart() {
  return depart;
 }
 public void setDepart(Department depart) {
  this.depart = depart;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
}
 

 

 

     其实一对多就是在多的一方也就是维护方建立关联少一个的关联关系(一般是通过外键ID)

     一对多双向:

    

      

import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
@Entity
public class Department {
 private long id;
 private String name;
 private Set<Employee> emps;
 @Id
 @GeneratedValue
 public long getId() {
  return id;
 }
 public void setId(long id) {
  this.id = id;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
@OneToMany(mappedBy="depart",cascade=CascadeType.ALL,fetch=FetchType.EAGER)
 public Set<Employee> getEmps() {
  return emps;
 }
 public void setEmps(Set<Employee> emps) {
  this.emps = emps;
 }
}

 

     

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Transient;
@Entity
public class Employee {
 private long id;
 private Department depart;
 private String name;
 @Id
 @GeneratedValue
 public long getId() {
  return id;
 }
 public void setId(long id) {
  this.id = id;
 }
 
@ManyToOne(cascade={CascadeType.PERSIST, CascadeType.MERGE}, fetch=FetchType.LAZY)
 @JoinColumn(name="dept_id")
 public Department getDepart() {
  return depart;
 }
 public void setDepart(Department depart) {
  this.depart = depart;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
}

 

 

 

     一对多双向关联,其实也是在维护端设置关联关系。

    

 

 

你可能感兴趣的:(Hibernate)