JPA many of the many are combined in the owner’s triggers, deleted by the connection table

I have many, many relationships between Customer and BusinessUnit:

public class Customer extends AbstractEntity { @JoinTable(name = "CUS_BUS_UNITS", joinColumns = { @JoinColumn(name = "CUS_ID", referencedColumnName = "CUS_ID")}, inverseJoinColumns = { @JoinColumn(name = "BUS_ID", referencedColumnName = "BUS_ID")}) @ManyToMany private Collection<BusinessUnit> businessUnits; } public class BusinessUnit extends AbstractEntity { @ManyToMany(mappedBy = "businessUnits") private Collection<Customer> customers; } 

When I call entityManager.merge (client); on the client (already in the database, not changed) I see these two sql commands in the log:

Hibernation: CLIENT update installed CUS_DESCR = ?, CUS_NAME = ?, CUS_ENABLED =? where is CUS_ID =? Hibernate: delete from CUS_BUS_UNITS, where CUS_ID =?

Why is hibernate trying to delete an entry from the connection table? I only need to update the client record and possibly the entries in the connection table - it depends on whether I added or deleted business units on the client. Business units should not be updated, deleted, or inserted.

EDIT: My equals / hashCode (defined in AbstractEntity):

 public int hashCode() { if (getId() != null) { return getId().hashCode(); } return super.hashCode(); } public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } AbstractEntity other = (AbstractEntity) obj; if (getId() == null || other.getId() == null) { return false; } if (!getId().equals(other.getId())) { return false; } return true; } 

EDIT 2 Converter for form:

 @FacesConverter("businessUnitConverter") public class BusinessUnitConverter implements Converter { /** * {@inheritDoc} */ @Override public String getAsString(FacesContext context, UIComponent component, Object object) { return ((BusinessUnit) object).getId().toString(); } @Override public Object getAsObject(FacesContext context, UIComponent component, String value) { BusinessUnit businessUnit = new BusinessUnit(); businessUnit.setId(Long.parseLong(value)); return businessUnit; } } 
+6
source share
1 answer

I found the answer: http://assarconsulting.blogspot.fr/2009/08/why-hibernate-does-delete-all-then-re.html

This behavior is correct due to the use of the collection (see related article). I used Set instead of collection and it works great.

+3
source

All Articles