• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

JPA CascadeType enquiry

 
Ranch Hand
Posts: 235
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 553
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Jack Bush
Ranch Hand
Posts: 235
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 235
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 553
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 235
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
That feels good. Thanks. Here's a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic