-->
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: How to map relation to a class with a composite id?
PostPosted: Wed Nov 22, 2006 5:33 pm 
Newbie

Joined: Fri Aug 11, 2006 12:31 pm
Posts: 2
Location: La Plata, Argentina
Hibernate version:3.1

I want to map a class A with a class B that have a composite id. The scenario is like that:

--- CLASS A ---------
<class name="A" table="A_TABLE">
<id name="id" column="ID" type="integer">
<generator class="assigned"/>
</id>
.....
</class>

--- CLASS B ---------

<class name="B" table="B_TABLE">
<composite-id name="pk" class="B_PK">
<key-property name="ID_A" column="KEY_1" type="int" />
<key-property name="seq_B" column="KEY_"" type="int" />
</composite-id>

.....
</class>

In A_TABLE I have columns for ID_A (obvious) and sec_B, but How I say that map the columns in A to get the B instance?

I try using one-to-one and many-to-one, but I had not luck.

I try using a PK object too, but again, how say map the columns in A to get the right instance of B.

Regards,
Ernesto.


Top
 Profile  
 
 Post subject: Hopefully a similar case
PostPosted: Tue Dec 12, 2006 7:33 pm 
Beginner
Beginner

Joined: Wed Jun 15, 2005 7:14 pm
Posts: 28
I had something like this to do on our product database with things like features, specifications, addons, and warranties, and a single Category.

In my Product class, I have lists of features, specifications, addons, warranties, and a single Category instance.

I explicitly define even the more primitive types, but I hope it's readable and useful.

I did the following with the addons:

In the Product.hbm.xml mapping I included this bit:

<bag name="addons" table="addon">
<key column="product_id" />
<many-to-many column="addon_product_id" class="com.mycompany.business.product.Product" />
</bag>

For its own Addon.hbm.xml mapping I do this:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping default-lazy="false">

<class name="com.mycompany.business.product.Addon" table="addon">

<composite-id unsaved-value="none">
<key-many-to-one class="com.mycompany.business.product.Product" name="productId" column="product_id"/>
<key-many-to-one class="com.mycompany.business.product.Product" name="addonProductId" column="addon_product_id"/>
</composite-id>
</class>

</hibernate-mapping>

If it's only a single value, you might try something like I used for a product's category:


In the Product.hbm.xml mapping:

<many-to-one name="category" column="category_id" class="com.mycompany.business.category.Category" />

In the Category.hbm.xml mapping:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping default-lazy="false">

<class name="com.mycompany.business.category.Category" table="category">
<id name="id" column="id" type="java.lang.Integer">
<generator class="native" />
</id>

<property name="name" column="name" type="java.lang.String" />
<property name="code" column="code" type="java.lang.String" />
</class>

</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 13, 2006 12:14 am 
Newbie

Joined: Fri Aug 11, 2006 12:31 pm
Posts: 2
Location: La Plata, Argentina
Thanks Jeremy for your answer.

I didn't have problem with a composite map many-to-one, my problem is with the mapping one-to-one with a composite key.

In your case, is like you have a Category class with a composite id, and want to map Product -> Category.

Thanks again, I guess I didn't explain very well in my first post.
Ernesto.


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.