一对一设置确实允许将父表中的多个记录与一个记录相关联

时间:2019-01-06 08:22:50

标签: jpa

请善良地帮助我理解为什么,通过运行附加的代码,我从数据库(MySQL)的父表中写入了多个记录,并与相关表中的记录相对应,尽管该记录是用Java代码设置的用于一对一关系(即,父项中的一条记录仅与其他相关表中的一条记录相对应。

对于您可能对此主题的任何疑问,我随时为您服务。

非常感谢您的支持 修士

public class Main {

public static void main(String[] args) {

        createParkingLot();
        AddEmployees(1);
        AddEmployees(2);
}
public static void createParkingLot(){
        EntityManagerFactory emf = 
Persistence.createEntityManagerFactory("java2curs4e1PU");
        EntityManager em = emf.createEntityManager();
        ParkingLot p1= new ParkingLot();
        p1.setParking_lot_no(1);
        try{
            em.getTransaction().begin();
            em.persist(p1);
            em.getTransaction().commit();
        } finally {
            em.close();
            emf.close();
        }
}

public static void AddEmployees(int employee_number){
        EntityManagerFactory emf =  
Persistence.createEntityManagerFactory("java2curs4e1PU");
        EntityManager em = emf.createEntityManager();
        ParkingLot p2 = em.find(ParkingLot.class,1);
        Employee a1 = new Employee();
        a1.setEmployee_name("Employee_name_#"+employee_number);
        a1.setParking_lot(p2);
        try{
            em.getTransaction().begin();
            em.persist(a1);
            em.getTransaction().commit();
        } finally {
            em.close();
            emf.close();
        }
}

}

++++++++++++++++++++++++++++++
相关实体说明:
+++++++++++++++++++++++++++++++
实体员工
+++++++++++++++++++++++++++++++

 @Entity
 @Table(name = "employees")
 public class Employee implements Serializable{
 @Id
 @GeneratedValue(strategy = GenerationType.IDENTITY)     
 private int id;
 private String employee_name;
 @OneToOne(cascade = CascadeType.ALL,optional = false,fetch = 
 FetchType.LAZY)
 @JoinColumn(name = "id_parking_lot")
 private ParkingLot parking_lot;

 public int getId() {
    return id;
 }

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

 public String getEmployee_name() {
    return employee_name;
 }

public void setEmployee_name(String employee_name) {
    this.employee_name = employee_name;
}

public ParkingLot getParking_lot() {
    return parking_lot;
}

public void setParking_lot(ParkingLot parking_lot) {
    this.parking_lot = parking_lot;
}
@Override
public String toString() {
    return "Employee{" + "id=" + id + ", name=" + employee_name + ", 
 parking lot=" + parking_lot + '}';
}

}

++++++++++++++++++++++++++++++
实体停车位:
++++++++++++++++++++++++++++++

 @Entity
 @Table(name = "parking_lots")
 public class ParkingLot implements Serializable{
 @Id
 @GeneratedValue(strategy = GenerationType.IDENTITY )
 private int id;
 private int parking_lot_no;
 @OneToOne(cascade = CascadeType.ALL,fetch = FetchType.LAZY,optional = 
 false,mappedBy = "parking_lot")
 private Employee employee;

public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public int getParking_lot_no() {
    return parking_lot_no;
}
public void setParking_lot_no(int parking_lot_no) {
    this.parking_lot_no = parking_lot_no;
}
public Employee getEmployee() {
    return employee;
}
public void setEmployee(Employee employee) {
    this.employee = employee;
}
    @Override
public String toString() {
    return "ParkingLot{" + "id=" + id + ", parking lot number=" + 
parking_lot_no + '}';
}
}

通常,我希望程序会遇到异常,因为我试图插入具有相同停车场(id 1)的另一名员工(Employee_name_#2)。

我发现我有2名拥有相同停车场的员工,而不是餐桌员工。

0 个答案:

没有答案