-->
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.  [ 8 posts ] 
Author Message
 Post subject: primary key with two primary keys
PostPosted: Mon Dec 18, 2006 5:03 pm 
Regular
Regular

Joined: Fri Nov 03, 2006 4:57 pm
Posts: 60
how do you create a composite primary key relationship mapping in you hibernate xml files. I have two tables and i want to run the follow sql:

select sale.storeName
from sale sale, storelocation sc
where sale.store_id = sc.store_id
and sale.storeLocId = sc.storeLocId


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 18, 2006 6:05 pm 
Beginner
Beginner

Joined: Tue Aug 29, 2006 8:13 pm
Posts: 32
Location: Spain (GU)
I don't understand, your problem, is create a composite primary key, isn't it?

You can use the composite-id:

<class name="Foo" table="foo">
<composite-id name="id" class="Person">
<key-property name="name" type="string" column="name"/>
<key-many-to-one name="address" class="Address" column="addr_id"/>
</composite-id>
<property name="age" column="age" type="string"/>
</class>


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 18, 2006 6:17 pm 
Regular
Regular

Joined: Fri Nov 03, 2006 4:57 pm
Posts: 60
my issue is how to make the join :

in my mapping:

where sale.store_id = sc.store_id
and sale.storeLocId = sc.storeLocId


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 19, 2006 5:15 am 
Beginner
Beginner

Joined: Tue Aug 29, 2006 8:13 pm
Posts: 32
Location: Spain (GU)
It tells about joins, in section 11.3. Associations and joins:

http://www.hibernate.org/hib_docs/refer ... l-examples

You should use a plugin to test the HQL querys more quickly. You can download the best plugin to test your querys from Hibernate page.

Best regards.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 20, 2006 3:57 pm 
Regular
Regular

Joined: Fri Nov 03, 2006 4:57 pm
Posts: 60
i want to do it in the mappings can i do it like this:

but it get errors since the mapping is to the same class


<many-to-one name="store_id "
column="store_id"
class="com..dao.hibernate.sale"
not-null="true" />

<many-to-one name="storeLocId "
column="storeLocId "
class="com..dao.hibernate.sale"
not-null="true" />


Last edited by suzie on Thu Dec 21, 2006 1:43 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 20, 2006 8:03 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Yes this is fully supported. The order is important. You name the columns using multiple nested column tag instead of a column attribute.

Often this question can be easily answered if you use the tools to reverse a simple schema and see what it generates.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 21, 2006 1:42 pm 
Regular
Regular

Joined: Fri Nov 03, 2006 4:57 pm
Posts: 60
Below is my composite key in my StoreLocation class

StoreLocation Class contains --->
Code:
    <composite-id name="id" class="StoreLocation">
        <key-property name="store_id"/>
        <key-property name="storeLocId"/>
    </composite-id>

and I added a getter and setter for "id" in my StoreLocation java bean



and my Sales Class I have the below since this is joining to the StoreLocation class with the composite key

Store Class contains--->
Code:
<many-to-one name="storelocation" class="StoreLocation">
    <column name="store_id"/>
    <column name="storeLocId"/>
</many-to-one>



and I added a getter and setter for "storelocation" attribute in my

but i get an error:


org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [com.dao.StoreLocation#com.dao.StoreLoaction@1f2c60d]


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 23, 2006 7:25 pm 
Regular
Regular

Joined: Fri Nov 03, 2006 4:57 pm
Posts: 60
if i have one table that has a composite key and the other one has a primary key and i join the two do I need a third link table that contains the primary key or can I just join them the two tables...

the join mapping i want to do is below:


select sale.storeName
from sale sale, storelocation sc
where sale.store_id = sc.store_id
and sale.storeLocId = sc.storeLocId

where where store location can have many sales


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