【问题标题】:Creating JPA model class for a table which does not have primary key defined but has unique index created为未定义主键但已创建唯一索引的表创建 JPA 模型类
【发布时间】:2021-10-10 05:36:25
【问题描述】:

为未定义主键但已创建唯一索引的表创建 JPA 模型类。

例如,有一个表 popular_item 表,其中包含 itemEntityId、itemType、itCode、quantity、status、createdDate 等列。 其中,如果列 itemEntityId、itemType、itCode、quantity 是唯一索引的一部分。那么如何创建Model类呢?

【问题讨论】:

    标签: java spring jpa spring-data-jpa


    【解决方案1】:

    JPA 模型类可以如下创建,其 ID 为 PopularItemId,它是作为唯一索引一部分的列的组合

    @Table(name="popular_item")
    @Entity
    @NoArgConstructor
    @AllArgCOnstructor
    PopularItem{
    @EmbeddedId
    @AtrributeOverride(name="itemEntityId",column=@Column(name=""))
    @AtrributeOverride(name="itemType",column=@Column(name="itemType"))
    @AtrributeOverride(name="itCode",column=@Column(name="itCode"))
    @AtrributeOverride(name="quantity",column=@Column(name="quantity"))
    private PopularItemId popularItemId;
    @Column(name="status")
    private boolean status;
    @Column(name="created_date")
    private LocalDate createdDate;
    }
    
    @AllArgsConstructor
    @NoArgConstrucotor
    @Embeddable
    public class PopularItemId {
    private String itemEntityId;
    private String itemType;
    private String itCode;
    private Double quantity;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-19
      • 1970-01-01
      • 1970-01-01
      • 2021-12-04
      • 2021-07-06
      • 2014-03-24
      相关资源
      最近更新 更多