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: Example for many-to-many relationship
PostPosted: Tue Aug 08, 2006 2:53 am 
Newbie

Joined: Mon Aug 07, 2006 5:35 am
Posts: 2
Hibernate version: v.1.2.0.Alpha1

Assume I have 3 table Contact(ContactID),Category(CategoryID) and table for relate CategoryContact(ContactID[\u],[u]CategoryID)

Mapping file:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" default-access="property">
<class name="TestNHibernate.Category, TestNHibernate" table="Categories">
<id name="Id" column="CategoryId">
<generator class="assigned" />
</id>
<property name="Description" column="CatDesc" />
<bag name="Contacts" table="CategoryContacts" cascade="none" access="nosetter.camelcase" lazy="false" inverse="true">
<key column="CategoryId" />
<many-to-many column="ContactID" class="TestNHibernate.Contact, TestNHibernate" />
</bag>
</class>

<class name="TestNHibernate.Contact, TestNHibernate" table="Contacts" discriminator-value="?" >
<id name="Id" column="ContactId" access="nosetter.camelcase" unsaved-value="0">
<generator class="identity" />
</id>
<discriminator column="ContactType" />
<property name="Name" column="ContactName" />
<bag name="Categories" table="CategoryContacts" cascade="none" access="nosetter.camelcase" lazy="false" inverse="false">
<key column="ContactId" />
<many-to-many column="CategoryId" class="TestNHibernate.Category, TestNHibernate" />
</bag>
</class>
</hibernate-mapping>

File class:
Category.cs
public class Category
{
private string id;
private string description;
private IList contacts = new ArrayList(); public string Id {
get { return this.id; }
set { this.id = value; }
}

public string Description {
get { return this.description; }
set { this.description = value; }
}

public IList Contacts {
get { return this.contacts; }
}
}
Contact.cs
public class Contact
{
private int id;
private string name;
private IList categories = new ArrayList(); public int Id {
get { return this.id; }
}

public string Name {
get { return this.name; }
set { this.name = value; }
}

public IList Categories {
get { return this.categories; }
}

}

Not at all when I Insert into Contact and Category table, CategoryContact will have a new row consist of ContactID and CategoryID values automatically.

But, my problem is if CategoryContact table have more fields, ex: Quantity field
CategoryContact will become:
CategoryContact(ContactID,CategoryID,Quantity,...)
I cant add data for Quantity column

Any body help me, whether the same example or your solutions...
Thank you so much!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 08, 2006 4:55 am 
Senior
Senior

Joined: Wed Jun 15, 2005 4:17 am
Posts: 156
you have to break the many-to-many into 2 one-to-many and your CategoryContact will become an entity on which you can map any number of properties.

cheers,
radu


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 08, 2006 5:21 am 
Newbie

Joined: Mon Aug 07, 2006 5:35 am
Posts: 2
Thank you!
But dont want to break into 2 one-to-many
I found that can use double key with <composite-id> tag but I dont know how to solve:
<composite-id>
<key-many-to-one name="Contact" column="ContactID" class="TestNHibernate.Contact, TestNHibernate"/>
<key-many-to-one name="Category" column="CategoryID" class="TestNHibernate.Category, TestNHibernate"/>
</composite-id>

vantamvtf


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 08, 2006 7:31 am 
Newbie

Joined: Wed Jun 21, 2006 4:48 am
Posts: 2
There is one way to use many-to-many by using Ayende Generics that overrides the default accessor.

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" namespace="DAL" assembly="DAL" default-cascade="save-update">

   <class name="CAdmin" table="Admin" >

      <id name="Id" type="Int32" column="id" unsaved-value="0">
         <generator class="native" />
      </id>

      <map name="Pages" inverse="false" lazy="false" cascade="save-update" table="Admin2Pages" access="NHibernate.Generics.GenericAccessor, NHibernate.Generics">
         <key column="IdAdmin" />
         <index-many-to-many column="IdPage" class="CPage" />
         <element column="AccessType" type="Int32"  not-null="true" />
      </map>

   </class>

</hibernate-mapping>



Notice the Key element AccessType that is part of the relation table.
This XML will appear in classes as:

Code:

namespace DAL
{
   public class CAdmin: MarshalByRefObject
   {
      private int id;
      private EntityDictionary<CPage, int> _pages;

      public CAdmin()
      {

         _pages = new EntityDictionary<CPage, int>(
                     delegate( KeyValuePair<CPage, int> obj )
                     {
                        obj.Key.Admins.Add( this, obj.Value );
                     },
                     delegate( KeyValuePair<CPage, int> obj )
                     {
                        obj.Key.Admins.Remove( this );
                     },
                     InitializeOnLazy.Always );
      }

      public virtual int Id
      {
         get
         {
            return id;
         }
         set
         {
            id = value;
         }
      }

      public virtual IDictionary<CPage, int> Pages
      {
         get
         {
            return _pages;
         }
      }
   }
}


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.