-->
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.  [ 7 posts ] 
Author Message
 Post subject: Maping a 4-table relationship in Hibernate 3.x
PostPosted: Fri Jul 29, 2005 2:30 pm 
Beginner
Beginner

Joined: Fri Jul 29, 2005 2:11 pm
Posts: 21
How do I map the following association in Hibernate (v3.x)?

Image


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 29, 2005 2:31 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Well, you can use either annotations or an XML mapping file. Any exceptions you get?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 29, 2005 3:32 pm 
Beginner
Beginner

Joined: Fri Jul 29, 2005 2:11 pm
Posts: 21
I was actually wondering how to write the specific XML to map this in hibernate.cfg.xml since it is a complex association/relationship.

I currently have (Hibernate) JavaBeans for the following tables and have been able to set them up (e.g. Part.hbm.xml) and do CRUD operation on each table individually using Hibernate:
1. Part
2. Manufacturer
3. Category

However, now I need to put together RecommendedParts.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 29, 2005 3:35 pm 
Beginner
Beginner

Joined: Fri Jul 29, 2005 2:11 pm
Posts: 21
To further elaborate, I'm wondering if:

1. Part is the Entity with the "primary key"?

2. RecommendedParts is a collection tied to Part using a foreign key (collections column key)? Do I use <bag> here?

3. Manufacturer and Category are values (<set>)?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 29, 2005 3:55 pm 
Newbie

Joined: Tue Jul 05, 2005 11:47 pm
Posts: 15
Location: Argentina
if you give the association a surrogate id, you can map it with something like:

Code:
<hibernate-mapping>
    <class name="Part" table="part">
        <id name="id" column="id">
            <generator class="sequence"\>
        </id>
        <set name="RecommendParts" cascade="all" inverse="true">
            <key column="part_id"/>
            <one-to-many class="RecommendPart"/>
        </set>       
    </class>

    <class name="Manufacturer" table="manufacturer">
        <id name="id" column="id">
            <generator class="sequence"\>
        </id>
        <set name="RecommendParts" cascade="all" inverse="true">
            <key column="manufacturer_id"/>
            <one-to-many class="RecommendPart"/>
        </set>       
    </class>

    <class name="Category" table="category">
        <id name="id" column="id">
            <generator class="sequence"\>
        </id>
        <set name="RecommendParts" cascade="all" inverse="true">
            <key column="category_id"/>
            <one-to-many class="RecommendPart"/>
        </set>       
    </class>

    <class name="RecommendPart" table="RecommendParts">
        <id name="id" column="id">
            <generator class="sequence"\>
        </id>
        <many-to-one name="manufacturer" class="Manufacturer" column="manufacturer_id"/>   
        <many-to-one name="part" class="Part" column="part_id"/>   
        <many-to-one name="category" class="Category" column="category_id"/>   
    </class>
</hibernate-mapping> 


If you prefer to use as the PK of the association the key composed with the FK of the other three tables, you can use a <composite_id>.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 29, 2005 6:13 pm 
Beginner
Beginner

Joined: Fri Jul 29, 2005 2:11 pm
Posts: 21
Thank you. I had a couple of questions:

1. If I want to use a List instead of a Set (easier to bind it to Spring MVC), should I simply use <bag> instead of <set>?

2. If I'm using "assigned" generators, does that cause a problem?

3. At this point, I'm mainly concerned with using the "RecommendedParts" class for it's corresponding screen (using Spring MVC).

Thanks again.


<hibernate-mapping>
<class name="Part" table="part">
<id name="id" column="id">
<generator class="assigned"\>
</id>
<set name="RecommendParts" cascade="all" inverse="true">
<key column="part_id"/>
<one-to-many class="RecommendPart"/>
</set>
</class>

<class name="Manufacturer" table="manufacturer">
<id name="id" column="id">
<generator class="assigned"\>
</id>
<set name="RecommendParts" cascade="all" inverse="true">
<key column="manufacturer_id"/>
<one-to-many class="RecommendPart"/>
</set>
</class>

<class name="Category" table="category">
<id name="id" column="id">
<generator class="assigned"\>
</id>
<set name="RecommendParts" cascade="all" inverse="true">
<key column="category_id"/>
<one-to-many class="RecommendPart"/>
</set>
</class>

<class name="RecommendPart" table="RecommendParts">
<id name="id" column="id">
<generator class="sequence"\>
</id>
<many-to-one name="manufacturer" class="Manufacturer" column="manufacturer_id"/>
<many-to-one name="part" class="Part" column="part_id"/>
<many-to-one name="category" class="Category" column="category_id"/>
</class>
</hibernate-mapping> [/quote]


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 29, 2005 7:30 pm 
Newbie

Joined: Tue Jul 05, 2005 11:47 pm
Posts: 15
Location: Argentina
1. If you don't need to retain the order of the List, you can map it to a <bag>. If you need to retain the order, map it to a <list>. Remember that if you use a <list> mapping, you need to map de index of the list using the <list-index> property.

2. No problem. I used sequence as a generic example.

3. I have never used Spring MVC, so I cannot advice you here.


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