-->
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.  [ 2 posts ] 
Author Message
 Post subject: What is the semantic of "inverse=true" for N:M man
PostPosted: Fri Jan 05, 2007 7:41 am 
Newbie

Joined: Fri Nov 10, 2006 2:42 pm
Posts: 2
Hibernate works nicely for me since I'm generating hbm files from UML models using my own smartGENERATOR template. This way I only have to draw the UML model and from then on it's all downhill.

But I'm having trouble getting the semantic for N:M relations right.
I read some links about it:

One explicitly states:
Quote:
...
This issue applies if you want the Java Collection updates to be equal to the database sync state. So, in the case of a many-to-many in Java you set both sides of the collection thus mapping has to have one side set as inverse=true so you dont get two updates (which will conflict) to the database. If you are only setting one side of the relationship in Java then no need to have the inverse true on any side - but be carefull as the Java collection will not match the database. In many cases, your throwing the collection away after the unit-of-work so its fine.
...
So, if I don't want synchronization with database on every add or remove from my collection, but on the end of the transaction it's Ok to remove inverse="true" from collection mapping on both sides?
...
Yes - Experiment a bit and see.


Before reading this I was under the impression that using inverse=true on one side of the relation was recommended so I was generating something like:

Customer.hbm.xml:
Code:
   <!--   Relation: Bankrelation
          customers may have accounts
         from myOwner:Customer N
          The owner of this account
         to myAccounts:Account N
          The accounts of a customer-->
      <idbag name="myAccountsCollection" table="Bankrelation">
      <collection-id column="Bankrelation_OID" type="string">
           <generator class="uuid"/>
         </collection-id>
         <key column="myOwner_OID"/>
         <many-to-many column="myAccounts_OID"   class="com.bitplan.swarchtest.impl.AccountImpl"/>
      </idbag>

Account.hbm.xml:
Code:
      <!--   Relation: Bankrelation
          customers may have accounts
         from myAccounts:Account N
          The accounts of a customer
         to myOwner:Customer N
          The owner of this account-->
      <bag name="myOwnerCollection" inverse="true" table="Bankrelation">
         <key column="myAccounts_OID"/>
         <many-to-many column="myOwner_OID"   class="com.bitplan.swarchtest.impl.CustomerImpl"/>
      </bag>


In the case when there is an Association class involved. The underlying model here is
Customer (N) - Bankrelation - (M) Account

This works nicely - I have some JUnit tests checking the proper behaviour.
In the case where no AssociationClass is involved I ran in to trouble.
The model
Role (N) - (M) AccessRight
was first generated by me with these hibernate declarations:

AccessRight.hbm.xml:
Code:
      <!--   Relation: AccessRights
         
         from myAccessRights:AccessRight N
          <ul>
<li>de: meine Berechtigungen
<li>en: the access rights for this role
</ul>
         to allowedRoles:Role N
          <ul>
<li>de: meine berechtigten Rollen
<li>en: the roles that are allowed for this activity
</ul>-->
      <bag name="allowedRolesCollection" table="AccessRights">
         <key column="myAccessRights_OID"/>
         <many-to-many column="allowedRoles_OID"   class="de.sparkasseninformatik.ccmserver.impl.RoleImpl"/>
      </bag>

Role.hbm.xml:
Code:
      <!--   Relation: AccessRights
         
         from allowedRoles:Role N
          <ul>
<li>de: meine berechtigten Rollen
<li>en: the roles that are allowed for this activity
</ul>
         to myAccessRights:AccessRight N
          <ul>
<li>de: meine Berechtigungen
<li>en: the access rights for this role
</ul>-->
      <bag name="myAccessRightsCollection" inverse="true" table="AccessRights">
         <key column="allowedRoles_OID"/>
         <many-to-many column="myAccessRights_OID"   class="de.sparkasseninformatik.ccmserver.impl.AccessRightImpl"/>
      </bag>

The Role side has "inverse=true" and I ran into trouble with this approach .. I could not add AccessRights to a role via my Swing GUI.
When I put the "inverse=true" on the other side it works from one side but now not from the other.

But if I remove the "inverse=true" on both ends I know get the impression that I would run into other problems regarding the proper synchronization of database and objects in memory.

Since I i'm also still struggling with the transaction handling in Swing the question of how to do transaction handling with hibernate in Swing and the above question might be interlinked. Currently I still force my users to commit changes manually via a button and then do a complete refresh of all displayed tables to get reasonable results. By the way I'm always using saveOrUpdate at this time.

Since I intend to publish my generator template openly I'd like to do it right. Comments are welcome so that I can give more details and ask better questions.

It looks like I should also make some things configurable like whether to use lazy, bag/set and may be other settings where the default behaviour of hibernate often needs changes. Which ones would you recommend?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 05, 2007 8:10 am 
Senior
Senior

Joined: Sun Jun 04, 2006 1:58 am
Posts: 136
we used long running session ... you might find this useful....if you go that route..

http://www.jroller.com/page/scarface?entry=hibernate_rich_clients_and_long

_________________
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.  [ 2 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.