wood burning stoves 2.0*
The moose likes Object Relational Mapping and the fly likes new to Hibernate - cascading update question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "new to Hibernate - cascading update question" Watch "new to Hibernate - cascading update question" New topic
Author

new to Hibernate - cascading update question

Jan Vandernoot
Greenhorn

Joined: Mar 05, 2002
Posts: 6
hi all,

i'm just starting to work with hibernate and i have this situation i don't really understand. maybe someone can explain?

i have a product object that has a parent which is again a product. hibernate mapping looks something like this:



then in my code i load a product, set a new parent and try to save:



but i get an exception:
JDBCExceptionReporter - Duplicate key or integrity constraint violation message from server: "Cannot add or update a child row: a foreign key constraint fails"

if i save the parent before i save the product, it works. however, i was expecting the cascade settings to take care of this somehow. why is this not the case? and how can i use these cascades correctly then?

many thanks,
jan

[ January 05, 2005: Message edited by: Jan Vandernoot ]
[ January 05, 2005: Message edited by: Jan Vandernoot ]
Roger Rustin
Greenhorn

Joined: Jul 08, 2004
Posts: 19
something is missing here.
product.setParentProduct(parent);

I believe product doesn't have anything called setParentProduct.

- Roger
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
I'm fairly new to Hibernate myself, and with my object graphs I always save the root element, but here are a couple things you might want to try out.
  • Don't you need to call parent.getChildren().add(product), too?
  • In my parent-child relationships, the inverse attribute in the <set> tag is set to "true". Maybe this will help?
  • If you do #1, then you can save the parent instead of the product.


  • [ January 05, 2005: Message edited by: David Harkness ]
    Jan Vandernoot
    Greenhorn

    Joined: Mar 05, 2002
    Posts: 6
    Alright, I got it working... Mentioning the inverse attribute made me look that up in the manual again and there it was:

    Changes made only to the inverse end of the association are not persisted (...) The non-inverse side is used to save the in-memory representation to the database.


    So it the cascade was actually working just fine, the problen was with the bidirectional association! I actually had the inverse attribute set correctly (see my mapping above), but I was trying to save on the inverse side and it needs to be the other way around.

    For completeness, here's the working code (the difference is in the last line!):



    Thanks!
    Roger Rustin
    Greenhorn

    Joined: Jul 08, 2004
    Posts: 19
    Thanks for posting the code. Here is reference to a site which in detail describes what happens when inverse attribute is used. All the generated queris are even posted. Nice.


    http://weblog.neeraj.name/blog/_archives/2005/1/5/231813.html
     
    GeeCON Prague 2014
     
    subject: new to Hibernate - cascading update question