I am re-posting this threat after having received no response from an earlier threat.
I need advice on which JPA CascadeType to use in order to enforce the unique constraint entries that exist in a Customer and Telephone OneToMany JoinTable relationship.
Let's examine the code snippets of Customer.java entity object is as follows:
The EntityManager.persist() method does a good job of inserting both Customer and Telephone records by abiding to the unique constraint of one telephone per customer when inserting new database records. EntityManager.persist() cannot be used for updating entity records as it continually try to insert existing records which throw exceptions. On the other hand, EntityManager.merge() updates or synchronise existing detached Customer entities correctly while Telephone records were duplicated (no longer unique) on the ManyToOne unidirectional side, with Customer being the owning side. Any idea on which combination of EntityManager and CascadeType would only update both Customer and Telephone entities while preserving the unique constraint OneToMany Unidirectinal relationship?
This Java EE 5 application has been working for a few years.
I am running JDK1.6.0_7, GF2.1 (EJB 3.0) and MySQL on Windows XP.
>> This Java EE 5 application has been working for a few years.
What exactly isn't working? persist() is for new objects, merge() is to merge detached existing or new objects. Using merge() on a detached object should cause any changes to be updated, and any new objects to be inserted. Nothing will be deleted unless you call remove() or use orphanRemoval in JPA 2.0. Cascade ALL will cascade the merge, so should update any changed phones assuming their Id is correct.
Thanks for helping out with another JPA update issue.
The problem is an addition of existing phones resulting in duplicate phones with multiple IDs even though merge() did not make the same mistake with owning Customer unidirectional entity. Furthermore, the duplicate phone records are identical to existing ones already in the database.
It would be ideal if it is possible to eliminate update/merge() altogether and only persist() for new customer.
Any idea on which method could resolve this issue?
posted 9 years ago
I am trying to avoid doing the following searches for every records about to be added into database:
This will ensure that only new Customers and Phones are added uniquely with no persist() exception but it is very inefficient. On the other hand, a one
liner with merge() with CascadeType=ALL, for all Customers and Phones (regardless whether they exist or not) will end up with unique Customers (great) but duplicate Phones.
What is the simplest efficient approach to add new unique Customers and Phones without getting persist() exception?
Thanks a lot,
posted 9 years ago
merge() should work for this. Are the duplicate Phones on a new Customer or an existing Customer?
Turning logging on finest and include the log, what JPA provider/version are you using, try upgrading to the latest version.
The find() is basically what merge() should end up doing, so what you are doing will at least not be less efficient than a merge().