This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes JPA CascadeType enquiry Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "JPA CascadeType enquiry" Watch "JPA CascadeType enquiry" New topic
Author

JPA CascadeType enquiry

Jack Bush
Ranch Hand

Joined: Oct 20, 2006
Posts: 235
Hi All,

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.

Your advice would be much appreciated.

Many thanks,

Jack
James Sutherland
Ranch Hand

Joined: Oct 01, 2007
Posts: 553
Not sure I understand what your issue is,

>> 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.


TopLink : EclipseLink : Book:Java Persistence : Blog:Java Persistence Performance
Jack Bush
Ranch Hand

Joined: Oct 20, 2006
Posts: 235
Hi James,

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?

Thanks again,

Jack
Jack Bush
Ranch Hand

Joined: Oct 20, 2006
Posts: 235
Hi James,

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,

Jack
James Sutherland
Ranch Hand

Joined: Oct 01, 2007
Posts: 553
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().
Jack Bush
Ranch Hand

Joined: Oct 20, 2006
Posts: 235
Hi James,

Thanks for your suggestion once again.

I have taken up your advice by upgrading to GF3.1 but the process has not been smooth. Refer to http://forums.java.net/jive/thread.jspa?threadID=153714&tstart=30. I will respond with the outcome from your suggestion once the GF3.1 teething issue has been ironed out.

Jack
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JPA CascadeType enquiry
 
Similar Threads
Need assistance in understanding JPA ManyToMany code example (Java EE 5)
How to add JPA ManyToMany records?
How to avoid duplicate inserts in JPA HQL (EJB 3.0 - Java EE 5)
Mysql auto generated PK used for another table and JPA
Cascade Type problem