-->
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: another column as foreign key in Hibernate
PostPosted: Sat Oct 08, 2005 2:18 am 
Beginner
Beginner

Joined: Sat Oct 08, 2005 2:13 am
Posts: 47
Hi
I am using Hibernate3
I have a table named "users" with columns :
Id_user int
username varchar
password varchar

the id_user is primary key and incremental, but there is another table named "roles"
I want to relation two table with column "username" but Hibernate use my primary key to join two table. how can I join two table with another column in a table, i dont want to join with primary key column.

my mapping in user.hbm.xml is:
.....
<set name="roles" lazy="false" inverse="false" table="role">
<key column="userName" />
<one-to-many class="com.project.conf.value.Role" />
</set>
.....
<there is a Set in class User>

hibernate use id_user to join table users with roles

pls help me....

thank u in advance


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 08, 2005 10:08 am 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
see description fro many-to-one and one-to-many in documentation - yuo can specify column property in association


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 08, 2005 11:28 am 
Beginner
Beginner

Joined: Sat Oct 08, 2005 2:13 am
Posts: 47
No u cant do this bcaz as I said my column is not a primary key.
the <one-to-many> only use primary key to join two tables , I want using another field of the table


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 08, 2005 11:35 pm 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
It is recommended, but you can make relation in hibernate without primary or foreign key

try, you can do it


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 09, 2005 2:25 am 
Beginner
Beginner

Joined: Sat Oct 08, 2005 2:13 am
Posts: 47
ok I tried before and if u know how to do this plz help me and if not, plz dont waste my time with your reply.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 11, 2005 11:06 am 
Newbie

Joined: Tue Oct 11, 2005 11:03 am
Posts: 4
Hello,
I think you can use property-ref attribute of the key to point to the property you want to use in join condition.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 12, 2005 4:39 am 
Beginner
Beginner

Joined: Sat Oct 08, 2005 2:13 am
Posts: 47
Hi

I used property-ref as following

<set name="roles" lazy="false" inverse="false" table="role">
<key property-ref="userName" />
<one-to-many class="com.project.conf.value.Role" />
</set>


but it seems that hibernate use "userName" of class "com.project.conf.value.Role" to join with userId of the user table but I want to use userName of user table and join it with userName of Role table


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 12, 2005 8:54 am 
Newbie

Joined: Tue Oct 11, 2005 11:03 am
Posts: 4
Hi

Try this

<set name="roles" lazy="false" inverse="false" table="role">
<key column="userName" property-ref="userName" />
<one-to-many class="com.project.conf.value.Role" />
</set>

Aswin


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.