Hi Experts
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....
-
another column as foreign key in Hibernate (4 messages)
- Posted by: Mohammad Norouzi
- Posted on: October 08 2005 02:00 EDT
Threaded Messages (4)
- It can't be done by Andrew Louth on October 18 2006 19:45 EDT
- use Property-ref to solve this problem by Pingbo Ni on November 28 2006 22:54 EST
- XDoclet tags by Jia Li on May 23 2007 09:37 EDT
- property-ref does not always work by Paul Egaru on July 02 2009 02:25 EDT
- XDoclet tags by Jia Li on May 23 2007 09:37 EDT
-
It can't be done[ Go to top ]
- Posted by: Andrew Louth
- Posted on: October 18 2006 19:45 EDT
- in response to Mohammad Norouzi
It is my understanding that this can't be done. Searching around the net shows that many people want to do the same thing but there is a notable silence from the Hibernate people in responding to this issue (even to tell people it can't be done). -
use Property-ref to solve this problem[ Go to top ]
- Posted by: Pingbo Ni
- Posted on: November 28 2006 22:54 EST
- in response to Mohammad Norouzi
Hello I hope this ticket is not too late. Actually this issue can be solved easily. You can just put an attribe named property-ref in your key element. In your case, the hbm.xml file should be like following: ..... And in many-to-one mapping, you should also specify the property-ref attribute. After you complete these steps. The hibernate will use the foreign key you specified instead of primary key.You can get more details in hibernate reference document. Pingbo Ni -
XDoclet tags[ Go to top ]
- Posted by: Jia Li
- Posted on: May 23 2007 09:37 EDT
- in response to Pingbo Ni
We use XDoclet (v1.2) to generate the mapping files. Our search for doing this with XDoclet tags didn't find anything. Seems like the property-ref tag in XDoclet is only available for "to-one" relationships, but not "to-many". Do you know how to do this? -
property-ref does not always work[ Go to top ]
- Posted by: Paul Egaru
- Posted on: July 02 2009 14:25 EDT
- in response to Jia Li
Discovered that property-ref can be a real pain when you have lazy loading, giving a message stating session is already closed. So better to use many-to-many instead, saves you a lot of hassle.