-->
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: Many to many mappings
PostPosted: Mon Feb 20, 2006 5:59 am 
Regular
Regular

Joined: Fri Nov 04, 2005 12:37 pm
Posts: 81
I am having trouble with my many to many mappings being persisted.

Here are my mappings:

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
   <class name="vcf.code.entites.Stand,vcf" table="vcfStands">
      <id name="Code" column="code" type="Int32">
         <generator class="native"/>
      </id>
   
      
      <set name="Locations" inverse="true" table="vcfLocationLookup">
         <key column="stand_id"/>
         <many-to-many column="hesa_id" class="vcf.code.entites.classification.Location,vcf"/>
      
      </set>
      
   </class>
   
</hibernate-mapping>


Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
   <class name="vcf.code.entites.classification.Location,vcf" table="vcfLocation">
      <id name="Id" column="HESA_id" type="Int32" unsaved-value="0">
         <generator class="assigned"/>
      </id>
      
      
      <set name="Stands" table="vcfLocationLookup">
         <key column="hesa_id" />
         <many-to-many column="stand_Id" class="vcf.code.entites.Stand,vcf" />
      </set>
      
      
      <property column="description" type="String" name="Description" length="255" />   
   </class>
</hibernate-mapping>


The problem is that when i create a Stand and add some Locations to it, when i then add\update the Stand the lookups are not being added to vcfLoctionLookup. What am i doing wrong?


Code:
foreach(ListItem i in Locations.Items)
{
   if(i.Selected)
   {
      Location l = new Location();
      l.Id= Convert.ToInt32(i.Value);
      stand.Locations.Add(l);
   }
}
            
            

standDAO.updateStand(stand);


Top
 Profile  
 
 Post subject: Re: Many to many mappings
PostPosted: Mon Feb 20, 2006 6:31 am 
Expert
Expert

Joined: Thu Jan 19, 2006 4:29 pm
Posts: 348
bosh wrote:
[code]
<set name="Locations" inverse="true" table="vcfLocationLookup">

I think that for many-to-many, You should not use inverse="true"

Gert


Top
 Profile  
 
 Post subject: Re: Many to many mappings
PostPosted: Mon Feb 20, 2006 7:19 am 
Expert
Expert

Joined: Thu Jan 19, 2006 4:29 pm
Posts: 348
bosh wrote:
Code:
foreach(ListItem i in Locations.Items)
{
   if(i.Selected)
   {
      Location l = new Location();
      l.Id= Convert.ToInt32(i.Value);
      stand.Locations.Add(l);
   }
}


Or actually, as stand.Locations is inverse, You must add stand to location.Stands in order to make association persistent.

Code:
foreach(ListItem i in Locations.Items)
{
   if(i.Selected)
   {
      Location l = new Location();
      l.Id= Convert.ToInt32(i.Value);
      // or maybe Location l = LocationDAO.GetLocation(Convert.ToInt32(i.Value));
      stand.Locations.Add(l);
      l.Stands.Add(stand);
   }
}


Top
 Profile  
 
 Post subject: Mapping
PostPosted: Mon Feb 20, 2006 11:28 pm 
Regular
Regular

Joined: Tue Jan 03, 2006 7:21 am
Posts: 85
In NHibernate for a bi-directional mapping to work you have to set both the side of the association. Just as gert said you would have to add stand tp the location as well as add location to the stand for the relation to be persisted.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 21, 2006 5:45 am 
Regular
Regular

Joined: Fri Nov 04, 2005 12:37 pm
Posts: 81
Fistly thanks for your help.....that does the trick

Code:
foreach(ListItem i in Locations.Items)
{
   if(i.Selected)
   {
      Location l =locationdao.GetLocation(Convert.ToInt32(i.Value));
      stand.Locations.Add(l);
      l.Stands.Add(stand);
      locationdao.UpdateLocation(l);
   }
}

standDAO.updateStand(stand);


I just dont quite understand how this works.....do i need to update both Location and Stand? or one? If its both how does nHIbernate handel this does it recieve the first transaction, and then just wait until the second transaction occurs before persisting?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 21, 2006 6:07 am 
Expert
Expert

Joined: Thu Jan 19, 2006 4:29 pm
Posts: 348
bosh wrote:
I just dont quite understand how this works.....do i need to update both Location and Stand? or one? If its both how does nHIbernate handel this does it recieve the first transaction, and then just wait until the second transaction occurs before persisting?


AFAIK: If You say inverse="true" for association, then NHibernate ignores the list content when persisting object. Inverse means that this association end is meant only for reading data from database, all changes are made trought another end.

Gert


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.