This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes EJB and other Java EE Technologies and the fly likes EJB3: how to update the bean on the many side of a one-to-many relation? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "EJB3: how to update the bean on the many side of a one-to-many relation?" Watch "EJB3: how to update the bean on the many side of a one-to-many relation?" New topic
Author

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

Tong Wang
Ranch Hand

Joined: Jun 20, 2005
Posts: 44
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
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

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 ]

Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Tong Wang
Ranch Hand

Joined: Jun 20, 2005
Posts: 44
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
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

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
 
 
subject: EJB3: how to update the bean on the many side of a one-to-many relation?
 
Similar Threads
[JPA] merge() is inserting instead of updating
How do you update a collection if some exist and some are new?
How to update row in CMP
JPA and One-to-One blues
Object not being updated to database