【问题标题】:Hibernate mapping primary key using the id of another classHibernate 使用另一个类的 id 映射主键
【发布时间】:2011-12-07 20:08:00
【问题描述】:

我有两个 MySQL 表:用户(具有自动递增的 pk id)和 WaitingUser,它是用户子集的列表。 WaitingUsers 表不能包含多次出现的用户,因此 userId 列是 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)

现在我想绘制地图,但不知道如何进行。

问题似乎与此处报告的问题相似,但我不使用注释:

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

如何在 WaitingUser.xbm.xml 文件中定义 WaitingUser 的 PK 为 User 的 PK?

【问题讨论】:

    标签: hibernate foreign-keys primary-key


    【解决方案1】:

    来自the hibernate docs

    您可以使用 hbm.xml 将引用类的 id(引用表的主键)映射到引用类的 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>
    

    【讨论】:

    • 能否请您提供基于注释的示例?
    猜你喜欢
    • 2015-04-03
    • 1970-01-01
    • 2014-11-22
    • 1970-01-01
    • 2010-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-09
    相关资源
    最近更新 更多