-->
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.  [ 4 posts ] 
Author Message
 Post subject: inverse="true" ... use with hibernate tools
PostPosted: Wed Jul 26, 2006 8:22 am 
Newbie

Joined: Wed Jul 26, 2006 7:51 am
Posts: 4
Hi!

I'm currently switching between the book "Hibernate - A Developers Notebook" and the "Hibernate Reference Documentation" to solve my understanding problem with inverse="true"....

The book and the documentation says that i have to make one side "inverse" in a association mapping and that it is regardless of which side of (e. g. a many-to-many) association it is.

I have a little testing example that consists of "Groups" and "Persons". A Group can have many Persons in it and one Person can be part of many Groups (many-to-many). I made the Group-side of the many-to-many as "inverse":

Person mapping:
Code:
      <set name="groups" table="PERSON_GROUP">
         <key column="PERSON_ID" />
         <many-to-many class="foo.Group" column="GROUP_ID" />
      </set>      



Group mapping:
Code:
      <set name="persons" table="PERSON_GROUP" inverse="true">
         <key column="GROUP_ID" />
         <many-to-many class="foo.Person" column="PERSON_ID" />
      </set>      



Now I generated my java-files with the "hbm2java"-task from hibernate tools, which works great.

With the following code i inserted a new group, a new person, and a association:

Code:
      Person aPerson = new Person("Stefan", "Hofmann", "Bamberg");
      Group aGroup = new Group("great group");
      aPerson.getGroups().add(aGroup);



This works.

The following code doesn't work (ony third line changed):

Code:
      Person aPerson = new Person("Stefan", "Hofmann", "Bamberg");
      Group aGroup = new Group("great group");
      aGroup.getPersons().add(aPerson);



Here the association isn't saved. I know this is because the inverse="true" deactivates this association somehow.

First question: why do i have to set one side as inverse="true" when the association works only in one direction then? the documentation says, that hiberate does not have enough information to build correct sql statements..... ok, i can live with it, because the documentation gives a solution:

adding two methods like this (in Person class):

Code:
      public void addGroup(Group group) {
         this.getGroups().add(group);
         group.getPersons().add(this);
      }
      public void removeGroup(Group group) {
         this.getGroups().remove(group);
         group.getPersons().remove(this);
      }



But i cannot add this methods in the class-sources because my java-files were generated by hbm2java. Ok, i could (!) change it, but after the next hbm2java-generation the changes are gone.....

Question 2: how can I use the bi-directional mapping from both sides without setting the association in my code always two times....

thanks for reading (and answering :).
Stefan.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 26, 2006 8:28 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
either:

a) use <meta attribute="generated-class">PersonBase</meta> and you write a class Person extends PersonBase with the extra methods. Then on regeneration only PersonBase is overwritten.

b) contribute/patch templates that actually creates such methods.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 26, 2006 8:58 am 
Newbie

Joined: Wed Jul 26, 2006 7:51 am
Posts: 4
thanks for the quick reply.

is this a common problem? i couldn't find anything about this topic. is there any further description (maybe in the hibernate docs that i've overlooked)?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 26, 2006 9:31 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
its covered in the docs of the tools.

_________________
Max
Don't forget to rate


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.