2011-12-07 45 views
0

我有两个MySQL表:用户(具有自动递增PK ID)和WaitingUser这是一个用户子集的列表。 WaitingUsers表不能包含多于一个用户,因此列userId是PK。休眠映射主键使用另一个类的ID

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) 

现在我想映射,但我不知道如何继续。

这个问题似乎是一个类似于此报告,但我不使用注释:

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

我如何定义WaitingUser的PK是用户在WaitingUser.xbm.xml的PK文件?

回答

1

the hibernate docs

可以映射引用类的ID(引用表的主键)到引用类(引用表的主键),像这样的hbm.xml的ID:

<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>