-->
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.  [ 3 posts ] 
Author Message
 Post subject: Many-To-Many Table Mapping question
PostPosted: Mon Nov 21, 2005 10:35 pm 
Newbie

Joined: Mon Nov 21, 2005 10:21 pm
Posts: 2
I have a question about doing a mapping with a many-to-many table. My 3 tables look like this:

Product
======
product_id
product_name

ProductGroup
=======
product_group_id
product_group_name

ProductProductGroup
=======
product_id
product_group_id
order_index

The ProductProductGroup table provides a many-to-many relationship between the Product and ProductGroup tables. This is an existing database so I can't change the schema.

Currently I have the mapping:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
   <class name="MyProducts.Product,MyProducts" table="Product">
      <id name="ProductId" column="product_id" type="String">
         <generator class="assigned"/>
      </id>
      <property name="ProductName" column="product_name" type="String"  />
      <set name="ProductGroupList" inverse="true" lazy="true" table="ProductProductGroup" >
         <key column="product_id" />
         <many-to-many class="MyProducts.ProductGroup,MyProducts" />
      </set>
   </class>
</hibernate-mapping>


<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
   <class name="MyProducts.ProductGroup,MyProducts" table="ProductGroup">
      <id name="ProductGroupId" column="product_group_id" type="String">
         <generator class="assigned"/>
      </id>
      <property name="ProductGroupName" column="product_group_name" type="String"  />
      <set name="ProductList" inverse="true" lazy="true" table="ProductProductGroup" >
         <key column="product_group_id" />
         <many-to-many class="MyProducts.Product,MyProducts" />
      </set>
   </class>
</hibernate-mapping>



How do I include order_index from the many-to-many table in both mappings? Or do I need to create a third class for the many-to-many table?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 23, 2005 11:29 am 
Senior
Senior

Joined: Wed Jun 15, 2005 4:17 am
Posts: 156
I have encountered a similar problem. I solved it by declaring a component which maps the many-to-many table. I am curious too if there is any other solution to this problem.

Cheers,
Radu


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 23, 2005 6:27 pm 
Newbie

Joined: Mon Nov 21, 2005 10:21 pm
Posts: 2
So by creating the many-to-many tables as a class it would look like this?
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
   <class name="MyProducts.ProductProductGroup,MyProducts" table="ProductProductGroup">
      <composite-id>
            <key-property column="product_id" type="String" name="ProductId" length="40" />
            <key-property column="product_group_id" type="String" name="ProductGroupId" length="40" />
      </composite-id>
      <property name="OrderIndex" column="order_index" type="String"  />
      <set name="ProductList" inverse="true" lazy="true" >
         <key column="product_id" />
         <many-to-many class="MyProducts.Product,MyProducts" />
      </set>
      <set name="ProductGroupList" inverse="true" lazy="true" >
         <key column="product_group_id" />
         <many-to-many class="MyProducts.ProductGroup,MyProducts" />
      </set>
   </class>
</hibernate-mapping>

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
   <class name="MyProducts.Product,MyProducts" table="Product">
      <id name="ProductId" column="product_id" type="String">
         <generator class="assigned"/>
      </id>
      <property name="ProductName" column="product_name" type="String"  />
      <set name="ProductProductGroupList" inverse="true" lazy="true" table="ProductProductGroup" >
         <key column="product_id" />
         <many-to-many class="MyProducts.ProductProductGroup,MyProducts" />
      </set>
   </class>
</hibernate-mapping>


<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
   <class name="MyProducts.ProductGroup,MyProducts" table="ProductGroup">
      <id name="ProductGroupId" column="product_group_id" type="String">
         <generator class="assigned"/>
      </id>
      <property name="ProductGroupName" column="product_group_name" type="String"  />
      <set name="ProductProductGroupList" inverse="true" lazy="true" table="ProductProductGroup" >
         <key column="product_group_id" />
         <many-to-many class="MyProducts.ProductProductGroup,MyProducts" />
      </set>
   </class>
</hibernate-mapping>


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