• 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
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Deleting OneToMany Entities in EJB3

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

I'm really stuck trying to delete related entities in EJB3. I have a one to many relationship between two tables and I can't figure out how to delete the child entity.

Let's say my primary bean, the parent is using a list collection to hold the child beans



I haven't had any luck doing this:



It doesn't seem to actually remove the relationship, even if I do an em.merge(parent) on it. On the other hand if I try to remove the child directly I get exceptions:



produces:



So which way is the right way to get rid of the child entity and ensure it's deleted from the database? Is there something I need to do to ensure the object is unique in the session?

Thanks much for the help.
Mike
[ June 26, 2006: Message edited by: Mike Litherland ]
 
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
What is your cascade setting for your parent table. Does it cascade deletes. I think you would update the parent object after removing the children. Or can you loop through the childs calling the session.delete() and then commit the transaction?

Mark
 
Mike Litherland
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply.

I did find out from browsing through the O'reilly EJB3 book (version 5) that I'm essentially doing things the right way. They recommend "always wire both sides" of a relationship. In this context it's saying do both to delete the object:



However, in this case that's when I get this exception. I'm going to ask on the JBoss forums and see if their gurus have any thoughts on what I'm doing wrong.

Thanks again.
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Only a guess, not sure what is the correct answer, (just started to learn EJB3. )

How about you make the parent bean detached from the EM, (is there any method in em to do this job? clear() will detach all the managed entities), and then do em.remove(akid)? then if you want to use the parent, em.find(parent.class, xxx)...
[ June 27, 2006: Message edited by: Joe J. Wang ]
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm having a similar problem.

I do parent.remove(child)
then em.remove(child)


But still get an exception - what is the correct process?
 
Hey cool! They got a blimp! But I have a tiny ad:
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic