How does the JPA CascadeType.PERSIST work ?

前端 未结 1 618
我寻月下人不归
我寻月下人不归 2021-02-12 22:47

In my example, Employee has an OneToOne relationship to Department with CascadeType.PERSIST. When I persist multiple Em

相关标签:
1条回答
  • 2021-02-12 23:01

    JPA maintains object identity and will not persist an existing object.

    Change you code to be correct,

    for(int i=1; i <= 10; i++) {
        Department department = new Department("Test Department");
        Employee e = new Employee("EMP" + i, department);
        em.persist(e);
    }
    
    0 讨论(0)
提交回复
热议问题