2011-12-07 74 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>