【问题标题】:How to create a PK class which has a relation to a composite Id如何创建与复合 ID 相关的 PK 类
【发布时间】:2015-06-17 00:30:18
【问题描述】:

我将实体 A 的 ID 更改为复合 ID。实体 B 也有一个 PK 类,关系的一侧是实体 A。我应该如何更改我的 PK 类代码?我应该使用 IdClass 代替 Id 吗?

public class BPK implements Serializable{

    // I need to change this single Id to a composite Id to include 2 columns from Entity A instead of only one id attribute 
    @Id
    @Column(name = "aid")
    private Integer a;

    @Id
    @Column(name = "bid")
    private Integer b;

}

A级PK

public class APK implements Serializable{

    @Id
    @Column(name = "id")
    private Integer id;

    @Id
    @Column(name = "project_mode")
    private Integer project;
 }

【问题讨论】:

    标签: java jpa many-to-many eclipselink


    【解决方案1】:
    public class BPK implements Serializable{
        private APK a;
        private Integer b;
    
    }
    

    JPA 中的主键类不需要注释,因为它们已经直接映射到实体中。派生 ID 是规范允许在主键中嵌套的一个地方,因为嵌套类总是深入到基本类型(相对于实体)。

    【讨论】:

      猜你喜欢
      • 2020-03-17
      • 1970-01-01
      • 2021-11-15
      • 2013-05-02
      • 2018-11-28
      • 2012-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多