-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: "inverse=true" is more then just an optimization h
PostPosted: Mon Oct 18, 2004 5:20 pm 
Newbie

Joined: Sat Oct 09, 2004 3:03 pm
Posts: 5
Hi,

I use many-to-many relations (association tables) to map serveral graph structures. When testing an application using these structures, I had a quite bizar error that kept me cursing for quite some hours...

MySQL (v4.0.20a on Windows) reported that Hibernate (v2.1.6) wanted to violate a primary key constraint on one of the association tables!

Explanation:
My mapping was using a List, which means that Hibernate chooses one link together with the position column to be the primary key of that association table.
While testing, I deleted a 'child' (object on the side which wasn't PK) of that many-to-many relationship. The PK constraint was generated when I wanted to update the same many-to-many relationship with the same 'parent' (side which is PK):
Hibernate tried to reuse the value for the position column, instead of updating the current record.

From what I understand Hibernate 'forgot' about that record because the 'child' in the relation was deleted.

The reason why it wasn't deleted turned out (after I first tried to use Sets or Bags instead) to be the fact that I used "inverse=true" for one of the sides of the relation (as advised to avoid Hibernate doing double DB updates).

Conclusion (correct me if I am wrong!):
The setting "inverse=true" also has its effects the workings of the Hibernate referential integrity.

Questions:
1/ Why is this so?
2/ Isn't it enough to reserve the usage of "inverse=true" to updates on the link itself (read: updates on the collections on both sides)?
3/ Is it correct that I now have to choose between manually updating association tables or tolerating double DB updates each time I update the association?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 22, 2004 4:08 pm 
Newbie

Joined: Fri Oct 22, 2004 4:00 pm
Posts: 5
hi ikke44,

adding inverse="true" on both sides of the bidirectional relationship will lead to undesirable behaviours of your application.
Please take a look at this tutorial to see how to fix it:
http://www.warfrog.com/hibernatetutorial2/

and check http://www.hibernate.org/155.html to see why this problem. OK?

Have a good time!

alioum


Top
 Profile  
 
 Post subject: this is not my question
PostPosted: Fri Oct 22, 2004 4:54 pm 
Newbie

Joined: Sat Oct 09, 2004 3:03 pm
Posts: 5
Quote:
adding inverse="true" on both sides of the bidirectional relationship


I don't mean anything like that...
Besides, from what I understand, Hibernate will would never produce any code to update the association tables when both sides have inverse=true!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 22, 2004 5:02 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
and? what's the problem? hibernate is not going to do your job that is manage your model integrity.

As a best practice you should use parent.addChild() and parent.removeChild() method to be sure to manage both sides of a bidirectionnal association (not parent.getChildren().add() + child.setParent() or parent.getChildren().remove() + child.setParent(null)).

If you don't do that... well no problem but take care of your code and don't _blame_ a part of a tool which is extremely well documented and very usefull.

You should also read Hibernate in action ;)

but no problem, we can wait and discuss about this with other users even if i don't really know if someone has problem with that.

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject: Once again...
PostPosted: Fri Oct 22, 2004 6:19 pm 
Newbie

Joined: Sat Oct 09, 2004 3:03 pm
Posts: 5
Hi,

My question is not about maintaining the other side of a relation...
I've bought and read "Hibernate in Action", the many FAQs and the reference manual PDF. I am not using anything like setXxx(null) anywhere, just remove() objects.

The context:
I have a many-to-many link modelling a relationship between instances of the same type: A 'Traject' having two 'Locations', a start- and endlocation. The end side had an inverse=true in the mapping of Location.

When I delete the startlocation, the records of the 'traject' association table that correspond are deleted, as desired.
When I delete a location which is used as endlocation, the 'end' column is set to 0 instead of deleting the 'traject'.

Question (in other wording):
Why is this problem solved by removing "inverse=true"?!

Hope this helps you all in understanding my question,

Frederik


Top
 Profile  
 
 Post subject: More documentation?
PostPosted: Fri Oct 22, 2004 7:00 pm 
Newbie

Joined: Sat Oct 09, 2004 3:03 pm
Posts: 5
@antony
I've been looking at even more documentation, but couldn't find any information that goes deeper then described in the "Hibernate in Action", the reference manual or the FAQ topics.

Is reading the Java code the thing to do next?
Is this what you meant by 'extremely well documented'?!

I've been considering doing that for a week now, because I still have a feeling that Hibernate does things I don't know about... But the problem is that I can't afford the time... I am already using much more time then when I had to write tedious JDBC and caching code... I don't want to trade easyness of programming for knowledge about what my program does(n't) with the database...


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.