Skip to main content

Java persistence api-220 and CASCADE

4 replies [Last post]
rumpi
Offline
Joined: 2005-12-14

Hi,
I have following problem:

tables: Employee, Customer, Employee_Customer
I want to have persist and remove-CASCADE types for the Employee.customers, so that the table Employee_Customer is also changed, but not the Customer table. I have read the part of the specification, that didn't help. My problem is that I don't want that the customers are deleted, but only the according data sets in Employee_Customer and also that when I persist new Employee the binding table is updated. The simplest way would be when I wouldn't get an exception when the customer already exists, so I can have only PERSIST-CascadeType.

Is that possible with following definition (OneToMany) ?

@Table...
class Employee{
.......
@OneToMany(cascade={CascadeType.REMOVE, CascadeType.PERSIST})
@JoinTable(
name="Employee_Customer",
joinColumns=@JoinColumn(name="employeeID", referencedColumnName="ID"),
inverseJoinColumns=@JoinColumn(name="customerID", referencedColumnName="ID")
)
public Collection getCustomers(){...}
}
@Table...
class Customer{....}

many thanks

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
chris_delahunt
Offline
Joined: 2005-07-06

Hello,

If you are not trying to get persist or remove called on the referenced customer, then you should not put the cascade remove/persist on the relationship. This only is used if you plan on calling persist on Employees and want persist to be automatically called on the referenced Customers. The relationship though will be persisted regardless of the cascade option set - assuming you have not set readonly type options on it.

Best Regards,
Chris Delahunt

rumpi
Offline
Joined: 2005-12-14

Thanks for the rapid answer. Just one more question, so that I can solve my problem:

Should I load the Customer-objects in the EntityManager-context and work with the same entity manager when I persist the Employee(which has now reference to the customers, deserialised from database) so I don't get the exception com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException about duplicate entries?
I use now in my rdbms-adapter one entity manager-instance pro DAO-operation. Is that usually a correct approach? With this approach I cannot get the Customers from DB and put them in the EntityManager-context and then update the newly inserted Employee with reference to these Customers in the same context.

For example:

public Employee insertEmployee(Employee empl){
....
EntityManager em = getEntityManagerWithTransaction();
em.persist(empl);
commitAndClose();
....
}

rumpi
Offline
Joined: 2005-12-14

It is just like I described it in my former posting. You should use the same EntityManager-instance for both GET-Customers(then setCustomers in Employee) and PERSIST-Employee. My generic DAO unfortunately won't work.

mvatkina
Offline
Joined: 2005-04-04

Hi,

Did you try to use merge() in a new EM? Don't forget to reassign the result :)

regards,
-marina