Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Tong Wang
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • 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
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic