简体   繁体   English

如何在Databse中使用2个主键映射JPA实体

[英]How to map JPA Entity with 2 primary keys in Databse

In my database model I have 2 primary keys. 在我的数据库模型中,我有2个主键。 I am using JPA with hibernate. 我在休眠状态下使用JPA。

My database model is like 我的数据库模型就像


USR_INFO_TBL USR_INFO_TBL

COMPANY_ID COMPANY_ID
PERSONAL_ID 个人身份证件
FIRST_NAME 名字
LAST_NAME
EMAIL 电子邮件
USR_INFO_TBL_PK USR_INFO_TBL_PK


Now my JPA entity is like below 现在我的JPA实体如下

 @Entity 
 @Table("USR_INFO_TBL")
 public class UerInfo{
@ID(name ="COMPANY_ID")
private String companyID;

private String personalID;   //what should i write here
@COLUMN(name="firstName") 
private String firstName;

@COLUMN(name="lastName ")
private String lastName;

@COLUMN(name="email")
private String email;

//setters and getters
}

How the 2 primary keys can be mapped to a JPA Entity Can you plz modify the above code.I gone through some posts as i was new to JPA AND HIBERNATE concepts.I was in a confusion 如何将这两个主键映射到JPA实体您可以修改上面的代码吗?由于我是JPA AND HIBERNATE概念的新手,所以我经历了一些帖子。

Thanks 谢谢

You should create a class annotated with @Embeddable containing the components of the composite key, and in your current, User entity you should create a field with this new type. 您应该创建一个用@Embeddable注释的类, @Embeddable包含组合键的组件,并且在当前的User实体中,您应该使用此新类型创建一个字段。

@Entity 
@Table("INFO_USR_TBL")
public class User {
    @EmbeddedId
    private UserId id;

    @Column(name="firstName") 
    private String firstName;

    @Column(name="lastName")
    private String lastName;

    @Column(name="email")
    private String email;

    //setters and getters
}

@Embeddable
public class UserId {
    @Column(name="COMPANY_ID")
    String companyId;
    @Column(name="PERSONAL_ID")
    String personalId;
}

In the JPA specification, it states "A composite primary key must either be represented and mapped as an embeddable class or must be represented as an id class and mapped to multiple fields or properties of the entity class". 在JPA规范中,它声明“复合主键必须要么表示并映射为可嵌入类,要么必须表示为id类并映射到实体类的多个字段或属性”。

You may change your code to below: 您可以将代码更改为以下代码:

public class UserId {
    String companyId;
    String personalId;
}

@Entity 
@Table(name="INFO_USR_TBL")
@IdClass(UserId.class)
public class User {

    @Id
    @Column(name ="COMPANY_ID")
    private String companyID;
    @Id
    @Column(name ="PERSION_ID")
    private String personalID;

    @Column(name="FIRST_NAME") 
    private String firstName;

    @Column(name="LAST_NAME")
    private String lastName;

    @Column(name="EMAIL")
    private String email;

    //setters and getters
}

Please, check this article JPA primary keys , especially "Composite Primary Key" and "Embedded Primary Key" paragraphs. 请检查本文的JPA主键 ,尤其是“复合主键”和“嵌入式主键”段落。 I think, your case is "Embedded Primary Key" when you need to define class, that will contain all primary key parts you actually need to have. 我认为,当您需要定义类时,您的案例就是“嵌入式主键”,它将包含您实际需要的所有主键部分。

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

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