在JPA OneToMany上重置关系

时间:2017-03-03 14:02:33

标签: hibernate jpa eclipselink jpa-2.1

以下ping 198.168.57.98 && echo Success || echo failed 下的简洁代码段,

hibernate-entitymanager 5.2.8.Final

为什么上次更新必须在下面添加新关系@Entity public class A { @Id@GeneratedValue private Long id; @OneToMany(cascade = CascadeType.ALL) private List<B> b = new ArrayList<B>(); public void add(B b) { this.b.add(b); } } @Entity public class B { @Id@GeneratedValue private Long id; } public class Service { private EntityManagerFactory emf; private EntityManager em; private EntityTransaction tx; public Service() { emf = Persistence.createEntityManagerFactory("myPU"); em = emf.createEntityManager(); tx = em.getTransaction(); } public void create(Object obj) { tx.begin(); em.persist(obj); tx.commit(); } public void update(A a, B b) { tx.begin(); a.add(b); tx.commit(); } } public class ServiceTest { @Test public void test() { Service s = new Service(); A a = new A(); s.create(a); B b1 = new B(); s.update(a, b1); B b2 = new B(); s.update(a, b2); // reset relation before adding new ? } } 之前重置现有关系a_b1?这只是具体实施问题吗?

a_b2

@EDIT

它在insert into A (id) values (1); insert into B (id) values (2); insert into A_B (A_id, b_id) values (1, 2); insert into B (id) values (3); delete from A_B where A_id=1; insert into A_B (A_id, b_id) values (1, 2); insert into A_B (A_id, b_id) values (1, 3); 下无法重现,因此可能只是一个实施问题。

0 个答案:

没有答案