I have a table named "users" with columns :
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" />
<there is a Set in class User>
hibernate use id_user to join table users with roles
pls help me....
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).
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.
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?
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.