jpa一对多

package com.demo.beans;


import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;


@Entity(name="Phones")
public class Phone {
 @Id
 private int phoneId;
 private int phoneNumber;


@ManyToOne(cascade=CascadeType.ALL)
 @JoinColumn(name="employeeId")
 private Employee employee;


public int getPhoneId() {
 return phoneId;
 }
 public void setPhoneId(int phoneId) {
 this.phoneId = phoneId;
 }
 public int getPhoneNumber() {
 return phoneNumber;
 }
 public void setPhoneNumber(int phoneNumber) {
 this.phoneNumber = phoneNumber;
 }
 public Employee getEmployee() {
 return employee;
 }
 public void setEmployee(Employee employee) {
 this.employee = employee;
 }


}



package com.demo.beans;


import java.util.Collection;
import java.util.List;
import java.util.Set;


import javax.persistence.CascadeType;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;




@Entity
@EntityListeners(MyMonitor.class)
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
 private int id;




 private String employeeName;


 
 @ElementCollection(targetClass = EmployeePeriod.class)
 private Collection employeePeriod;
 @ElementCollection
 private Set<String> nickName;
 
@OneToMany(mappedBy="employee", cascade=CascadeType.ALL)
 private List<Phone> phones;




@OneToOne(cascade=CascadeType.ALL)
 @JoinColumn(name="AddressId")
 private Address address;


public Address getAddress() {
 return address;
 }


public void setAddress(Address address) {
 this.address = address;
 }




public Collection getEmployeePeriod() {
    return employeePeriod;
}


public void setEmployeePeriod(Collection employeePeriod) {
    this.employeePeriod = employeePeriod;
}


public Set<String> getNickName() {
    return nickName;
}


public void setNickName(Set<String> nickName) {
    this.nickName = nickName;
}


public String getEmployeeName() {
 return employeeName;
 }


public int getId() {
    return id;
}


public void setId(int id) {
    this.id = id;
}


public void setEmployeeName(String employeeName) {
 this.employeeName = employeeName;
 }


public List getPhones() {
 return phones;
 }


public void setPhones(List phones) {
 this.phones = phones;
 }


}



 Employee employee = new Employee();
        
        
        employee.setEmployeeName("John Bai");
        Phone phone = new Phone();
        phone.setPhoneId(1);
      
        phone.setPhoneNumber(123);


        
        Phone phone2 = new Phone();
        phone2.setPhoneId(2);
      
        phone2.setPhoneNumber(123);
        
        phone2.setEmployee(employee);
        List<Phone> l = new LinkedList<Phone>();
        l.add(phone);
        l.add(phone2);
        employee.setPhones(l);
        userRepository.addEmployee(employee);


注意到第一个手机对象没有设置employee,结果后台数据库中表的记录如下:

mysql> select * from phones;
+---------+-------------+------------+
| phoneId | phoneNumber | employeeId |
+---------+-------------+------------+
|       1 |         123 |       NULL |
|       2 |         123 |          5 |
+---------+-------------+------------+
2 rows in set (0.00 sec)


mysql> select * from employee;
+----+--------------+-----------+
| id | employeeName | AddressId |
+----+--------------+-----------+
|  1 | John Smith   |         1 |
|  2 | John Bai     |      NULL |
|  5 | John Bai     |      NULL |
+----+--------------+-----------+


一般的一对多关系就这么用了


你可能感兴趣的:(jpa一对多)