• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

EJB3: how to update the bean on the many side of a one-to-many relation?

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I am having trouble with updating an entity bean that's on the many side of a one-to-many relation. The entity bean owning the relation is Customer (on the one side); the other side is PhoneNumbers (many side): i.e., one customer can have many phone numbers.

Here are the two ways I tried, but neither worked correctly:

1. This ends up with a new row in the PhoneNumber table and it's orphaned also. The old row didn't get updated either.



2. This ends up with a new row in the PhoneNumber table with the proper relationship set up. But, the old row didn't get updated.


Assumptions:

em is the EntityManager;
customer is the Customer entity bean;
phoneNumber is the updated PhoneNumber entity bean that belongs to customer;

Can some one help me with this?

Thanks,
Tong
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your mapping of the Association, The Collection of Phone Numbers in the Customer class, what Cascade options did you choose. That determines how a JPA implementation will save/update/delete associations. You will probably want "persist, delete" Cascade options.

Mark
[ February 09, 2007: Message edited by: Mark Spritzler ]
 
Tong Wang
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply, Mark.

I had the cascade type set to ALL. I just like to find out what's the proper way to update one of a bean's children programmatically.
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Within a Session

Well, if you have the Collection Cascade ALL, and you load the One side, go into the Collection, change one of the objects in the collection. and call Commit on the transaction or flush the Session.

If you loaded the One side in a Session, along with the Many side, then close the session, then open a new session, then in the new session you will have to call session.save(one);

Mark
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic