简体   繁体   English

将数据成员声明为休眠中的主键

[英]Declare data member as primary key in hibernate

I have 2 Entity classes User and Address 我有2个实体类User和Address

@Entity
class User
{
    int id;
    String name;
}

@Entity
class Vehicle
{
    @OneToOne
    User user;
    String vehName; 
}

I want to declare "user" data member in Vehicle class as primary key..can anyone tell me the solution? 我想在Vehicle类中声明“用户”数据成员作为主键。.有人可以告诉我解决方案吗?

As explained in the documentation : 文档所述

Finally, you can ask Hibernate to copy the identifier from another associated entity. 最后,您可以要求Hibernate复制另一个关联实体的标识符。 In the Hibernate jargon, it is known as a foreign generator but the JPA mapping reads better and is encouraged. 在Hibernate术语中,它被称为外来生成器,但JPA映射读起来更好,值得鼓励。

@Entity
class MedicalHistory implements Serializable {
  @Id @OneToOne
  @JoinColumn(name = "person_id")
  Person patient;
}

@Entity
public class Person implements Serializable {
  @Id @GeneratedValue Integer id;
}

Or alternatively 或者

@Entity
class MedicalHistory implements Serializable {
  @Id Integer id;

  @MapsId @OneToOne
  @JoinColumn(name = "patient_id")
  Person patient;
}

@Entity
class Person {
  @Id @GeneratedValue Integer id;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM