简体   繁体   English

Java JPA flush()不会以一对多关系更新数据库吗?

[英]Java JPA flush() doesn't update database in one to many relationship?

So I have a Person class which has one to many relationship to Address class ( not really in my code but its similar like this, just to make it easier ). 所以我有一个Person类,它与Address类具有一对多的关系(不是真正在我的代码中,而是类似这样,只是为了使它更简单)。

I'm trying to set null to addresses then flush it then re insert with new addresses but it won't work since there is a possibility of duplicate key in Address table even if I set addresses to null then flush it. 我试图将null设置为地址,然后刷新它,然后用新地址重新插入,但是由于即使将地址设置为null然后刷新它,地址表中也可能存在重复键,因此无法正常工作。

I need to remove every address first then set them to null then flush. 我需要先删除每个地址,然后将它们设置为null,然后刷新。

-------------------------------------------------------------
// 1, this won't work, duplicate key exception when committing in Address table if there is a same address in newAddress with address in this person addresses

em.getTransaction().begin();
person.setAddresses(null);
em.flush();

// print the address, count is still the same with before depends on how many address this person has

String query = "SELECT P.addresses FROM Person P WHERE P = :pr"; 
ObservableList<Address> addresses= FXCollections.observableArrayList(
                                          em.createQuery(query, Address.class)
                                            .setParameter("pr", person)
                                            .getResultList());
System.out.println("COUNT: " + addresses.size() +  " name: " +  addresses.get(0).getName());  

person.setAddresses(newAddresses);
em.commit();             // exception

-----------------------------------------------------------------------

// 2, this works

em.getTransaction().begin();
for(Address a : person.getAddresses() {
em.remove(a);
}
person.setAddresses(null);
em.flush();

// print the address, count is still one and its null

String query = "SELECT P.addresses FROM Person P WHERE P = :pr"; 
ObservableList<Address> addresses= FXCollections.observableArrayList(
                                          em.createQuery(query, Address.class)
                                            .setParameter("pr", person)
                                            .getResultList());
System.out.println("COUNT: " + addresses.size() ); 
em.commit();         // fine

---------------------------------------------------------------------

One to many relationships are controlled by the the owner side, which is address in your case. 一对多关系由所有者一方控制,这是您所要解决的问题。 Therefore you must set/remove the relationship from the address. 因此,您必须从地址中设置/删除关系。

Person.setAddresses won't have any effect for JPA, the proper way is iterating through the addresses and setting address.person = null or removing the addresses completely. Person.setAddresses对JPA无效,正确的方法是遍历地址并设置address.person = null或完全删除地址。 The rest should follow. 其余应遵循。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM