-->
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: one-to-many vs many-to-many
PostPosted: Wed Jun 27, 2007 11:09 am 
Newbie

Joined: Thu May 31, 2007 10:51 am
Posts: 8
Location: Edinburgh, UK
I am having big trouble with a many-to-many represented as a one-to-many. I have a "Group" object and a "Seller" object. A "Seller" can belong to many groups, and a "Group" can have many Sellers. the mapping of these two entities require specific properties defining the relationship between "seller" and "group".

So I have mapped the relations hip using a "SellerGroupMapping" object and a one-to-many. I only want to add a Seller to a Group not vice versa. So I have a method that adds mapping to the group. And I then want to save the group. (not save each mapping individually).

But nhibernate does not like this, I get SQL concurrency errors in that it cannot find the group in the database when it does in fact exist.

Here is my mapping file fro the SellerGroupMapping object:

Code:
<class name="SellerGroupMapping" table="Map_Sellers_Groups">
      <composite-id>
         <key-many-to-one name="Group" class="Group" column="GroupID" />
         <key-many-to-one name="Seller" class="Seller" column="SellerID" />
      </composite-id>

      <property name="SKChaseOnlinePublicCommission" column="SKCOnlinePublicCommission" />
      <property name="SKChaseOnlinePrivateCommission" column="SKCOnlinePrivateCommission" />
      <property name="SKChaseOfflineCommission" column="SKCOfflineCommission" />
      <property name="GroupOnlinePublicCommission" column="GroupOnlinePublicCommission" />
      <property name="GroupOnlinePrivateCommission" column="GroupOnlinePrivateCommission" />
      <property name="GroupOfflineCommission" column="GroupOfflineCommission" />
      <property name="FulfilmentCost" column="Fulfilment" />

   </class>


Mapping file for the group:
Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="SKChase.Model" namespace="SKChase.Model">
   
   <class name="Group" table="Groups">
      
      <id name="Id" type="Int32" column="ID" unsaved-value="-1" access="nosetter.pascalcase-underscore">
         <generator class="identity" />
      </id>
      
      <property name="Name" type="String" length="100" unique="true"/>
      <property name="Active" type="Boolean" />
      <property name="Type" type="SKChase.Model.GroupType, SKChase.Model" />
      <bag name="SellerMappings"
         table="Map_Sellers_Groups" cascade="save-update"
         inverse="false" lazy="true" access="nosetter.pascalcase-underscore">
         <key column="GroupID" />
         <one-to-many class="SKChase.Model.SellerGroupMapping, SKChase.Model" not-found="ignore" />
      </bag>
      
      <many-to-one name="Properties" column="PropertySetID"  class="PropertyData" cascade="all" fetch="join" />
      
   </class>


Can you please point out any glaring errors?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 28, 2007 7:19 pm 
Expert
Expert

Joined: Fri Oct 28, 2005 5:38 pm
Posts: 390
Location: Cedarburg, WI
It may not be related to your errors, but it's a good idea to use a single-column surrogate primary key rather than your composite of the seller id and group id. Make the combination of those two columns a unique index instead of the primary key.

Composite primary keys are almost always a bad idea ...

I think you need to set inverse="true" on the bag mapping.


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.