another column as foreign key in Hibernate


EJB programming & troubleshooting: another column as foreign key in Hibernate

  1. another column as foreign key in Hibernate (4 messages)

    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" />
    <there is a Set in class User>

    hibernate use id_user to join table users with roles

    pls help me....

    Threaded Messages (4)

  2. It can't be done[ Go to top ]

    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).
  3. 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
  4. XDoclet tags[ Go to top ]

    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?
  5. property-ref does not always work[ Go to top ]

    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.