I'm having trouble figuring out how to set up JPA persistence (using EclipseLink and transaction-type="RESOURCE_LOCAL") correctly for the following entities:
@Entity
public class User {
// snip various members
@ManyToMany
private List<Company> companies;
public void setCompanies(List<Company> companies) {
this.companies = companies;
}
}
@Entity
public class Company {
// snip various members
}
What I'm trying to do is set up a cascade for the companies list so that, if a new Company that hasn't been previously persisted is in the list, it would be automatically persisted together with the User:
User newUser = new User();
Company newCompany = new Company();
List<Company> companies = new ArrayList<Company>();
companies.add(newCompany);
newUser.setCompanies(companies);
entityManager.persist(newUser);
By setting cascadeType.PERSIST on the @ManyToMany, this works just fine. But if the list of companies contains a Company that was previsouly persisted, I get a MySQLIntegrityConstraintViolationException, since it's trying to persist (INSERT) a new Company with the same primary key:
User newUser = new User();
Company oldCompany = companyDAO.find(oldCompanyId);
List<Company> companies = new ArrayList<Company>();
companies.add(oldCompany);
newUser.setCompanies(companies);
entityManager.persist(newUser);
So how should this be set up so that new Companies are automatically persisted, but existing Companies are simply added to the user-company mapping?
CascadeType. ALL specifies that when a Customer is created, if there is any Address association, then that Address will be created as well(CascadeType. PERSIST). If the Customer is deleted from persistence storage, the Address table will be deleted(CascadeType.
CascadeType enumerated types that define the cascade operations. These cascading operations can be defined with any type of mapping i.e. One-to-One, One-to-Many, Many-to-One, Many-to-Many.
CascadeType. DETACH. The detach operation removes the entity from the persistent context. When we use CascadeType. DETACH, the child entity will also get removed from the persistent context.
The cascade persist is used to specify that if an entity is persisted then all its associated child entities will also be persisted. The following syntax is used to perform cascade persist operation: - @OneToOne(cascade=CascadeType.PERSIST)
The best way to think about cascades in hibernate is if you call the method X on the parent then it will call the method X on each of the children. So yes, if you call persist on the user then it will call persist on each of the children, regardless of whether they have been persisted or not.
This situation is not ideally handled with cascades. Cascade persist is intended for situations where all children are created with the parent (for example, if a use had a list of "skills") and more intended for one-to-many.
I would personally not use a cascade in this situation. Flagrant use of cascades when they are not needed can slow an application down.
If you feel you must use a cascade you can use a cascade merge. Merge will persist entities when they are not persisted already. However, merge has some very bizarre side effects which is probably why you didn't notice it working. Consider the following example:
x = new Foo();
y = new Foo();
em.persist(x);
Foo z = em.merge(y);
//x is associated with the persistence context
//y is NOT associated with the persistence context
//z is associated with the persistence context
Your issues is you are corrupting your persistence context. Managed objects should only reference other managed objects. So having your new object reference an existing detached object is wrong.
What you need to do is do a find() to get the managed version of the existing detached object and have your new object reference it, then call persist on it.
You could also use merge() instead of persist and it should resolve your object's references. Note that merge does not make a detached object managed, it return a copy of the detached object that is managed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With