667481 members! Sign up to stay informed.

Sponsored Links


Resources

Enterprise Java
Research Library

Get Java white papers, product information, case studies and webcasts

EJB programming & troubleshooting EJB programming & troubleshooting EJB programming & troubleshooting Messages: 4 Messages: 4 Messages: 4 Printer friendly Printer friendly Printer friendly Post reply Post reply Post reply XML XML XML

another column as foreign key in Hibernate

Posted by: mohammad norouzee on October 08, 2005 DIGG
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....

Threaded replies

·  another column as foreign key in Hibernate by mohammad norouzee on Sat Oct 08 02:00:57 EDT 2005
  ·  It can't be done by Andrew Louth on Wed Oct 18 19:45:03 EDT 2006
  ·  use Property-ref to solve this problem by Raymond Ni on Tue Nov 28 22:54:13 EST 2006
    ·  XDoclet tags by Jia Li on Wed May 23 09:37:22 EDT 2007
      ·  property-ref does not always work by Paul Egaru on Thu Jul 02 14:25:48 EDT 2009
  Message #220603 Post reply Post reply Post reply Go to top Go to top Go to top

It can't be done

Posted by: Andrew Louth on October 18, 2006 in response to Message #187332
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).

  Message #222981 Post reply Post reply Post reply Go to top Go to top Go to top

use Property-ref to solve this problem

Posted by: Raymond Ni on November 28, 2006 in response to Message #187332
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:
<set name="roles" lazy="false" inverse="false" table="role">
<key column="userName" propery-ref="you specify name"/>
<one-to-many class="com.project.conf.value.Role" />
</set>
.....
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

  Message #233343 Post reply Post reply Post reply Go to top Go to top Go to top

XDoclet tags

Posted by: Jia Li on May 23, 2007 in response to Message #222981
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?

  Message #310640 Post reply Post reply Post reply Go to top Go to top Go to top

property-ref does not always work

Posted by: Paul Egaru on July 02, 2009 in response to Message #233343
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.

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

Recent active threads Recent active threads Recent active threads More More More
IPhone App Development with JSF
Web as the Platform: Day 1 at the Ajax Experience
Need help for login page using java servlet
Stateful Webservice in java
SAP Asks Sun/Oracle to Let Java Be Free
Registration for TheServerSide Java Symposium Las Vegas now open
Use Sun SPOTs as your build canary
AspectJ In Action Published; Sample Chapters Posted on TSS
WSO2 goes all RESTy
Return to previous view after user action
More active threads »
Top posters of the weekTop posters of the weekTop posters of the week
This list contains the members who have made the most posts in all forums over the last 7 days:
  1. Dan Evans
  2. James Watson
  3. William Louth
  4. sara foster
  5. Chief Thrall
Hot threads Hot threads Hot threads More hot threads More hot threads More hot threads

Object pooling is now a serious performance loss

Brian Goetz continues to lift the lid and peak into the inner workings of Java in Java Urban Performance Legends. In this article he exposes the fallacy behind some of the more common performance myths found in the annals of the JVM.
(93 comments, last posted February 06, 2009)

Beyond Java

Bruce Tate, author of Better, Faster Lighter Java and Bitter EJB has come out with a new book called Beyond Java. Bruce has an epiphany about the future of software development. Does it include Java?
(770 comments, last posted September 23, 2009)

Three forms of AJAX: solid, liquid and gas.

Looks like today AJAX concept have several interpretations. We can distinguish different approaches of AJAX integration. Can they co-exist within the same application? Can we talk about layered AJAX integration?
(68 comments, last posted May 08, 2008)

Design-Time API Promises to make Java more like VB

Artima has published a short article describing the Design-Time API for JavaBeans, which was recently approved as JSR 273. This API promises to bring VB-like ease to Java development, but may face a cultural bias among Java developers who tend to think more in terms of class libraries than components.
(225 comments, last posted November 19, 2009)

Will Sun be that target of a management buyout?

There is plenty of speculation today regarding a potential buyout of Sun Microsystems by Scott McNealy and Silver Lake Partners. How would privatization of Sun affect Java?
(16 comments, last posted May 15, 2009)
More hot threads »

News | Blogs | Discussions | Tech talks | Patterns | Reviews | White Papers | Downloads | Articles | Media kit | About
Java Solutions
All Content Copyright ©2007 TheServerSide Privacy Policy      Powered by JIVE
Site Map