0

I have two MySQL tables: User (with an auto-incremented pk id) and WaitingUser which is a list of a subset of users. WaitingUsers table cannot contain more than one occurrence of a user so the column userId is PK.

public class User implements Serializable{
   private int id;
   private String userName;
....

TABLE USER:
id               int(10) unsigned PK
userName         varchar(45)

public class WaitingUser implements Serializable{
   private User userId;
   private String otherinfo;
....

TABLE WAITING_USER:
userId               int(10) unsigned PK
otherinfo            varchar(45)

Now I would like to map but I don't know how to proceed.

The problem seems similar to the one reported here but I don't use annotation:

Using an Entity (and their Primary Key) as another Entity's Id

How can I define the PK of WaitingUser is the PK of User in the WaitingUser.xbm.xml file?

Community
  • 1
  • 1
maxqua72
  • 391
  • 1
  • 9
  • 25

1 Answers1

1

From the hibernate docs:

You can map the id of a referenced class (primary key of a referenced table) to the id of the referencing class (primary key of the referencing table) with hbm.xml like this:

<class name="WaitingUser">
  <id name="id">
    <generator class="foreign">
      <param name="property">userId</param>
    </generator>
  </id>
  <one-to-one name="userId" class="User" constrained="true"/>
</class>
tscho
  • 2,024
  • 15
  • 15