繁体   English   中英

JPA关联表映射,其中实体之一具有组合键

[英]JPA Mapping of Association table where one of the entities has composite key

我有以下模型需要使用JPA进行注释:

  • 商家(merchant_id,...)。

  • MerchantType(id1,id2,...)

  • MerchantMerchantTypeAssociationTable(merchant_id,id1,id2)

我不知道如何映射关联表。 Mapping Merchant非常困难,因此我将其保留在映射之外。 其他映射如下:

MerchantType

@Entity
class MerchantType {

    @EmbeddedId
    @AttributeOverrides({
         @AttributeOverride(name = "e1_id", column=@Column(name="e1_id")),
         @AttributeOverride(name = "another_id", column=@Column(name="another_id"))
    })        
    MerchantTypePk id;

    @ManyToOne
    @JoinColumn(name = "e1_id", referencedColumnName = "e1_id", insertable = false, nullable = false)
    @MapsId("e1_id")
    AnotherEntity1 e1;


    @Column(name = "another_id", referencedColumnName = "another_id", insertable = false, nullable = false)
    Long anotherId;

    //Two other local fields irrelevant to the discussion here

    public MerchantType(){
        this.id = new MerchantTypePk();
    }

    //Getters and setters here.
}

//MerchantTypePk is a simple Embeddable class here below with two Long fields:
//e1_id and another_id

MerchantMerchantTypeAssociation

@Entity
class MerchantMerchantTypeAssociation {

     @EmbeddedId
     @AttributeOverrides({
           @AttributeOverride(name = "e1_id", column = @Column(name = "e1_id")),
           @AttributeOverride(name = "another_id", column = @Column(name = "another_id"))
           @AttributeOverride(name = "offer_id", column = @Column(name = "merchant_id"))
     })
     private MerchantMerchantTypeAssociationPk id;
     //******** HERE IS THE QUESTION
     //******** HERE IS THE QUESTION
     //******** HERE IS THE QUESTION
     @ManyToOne
     @JoinColumns({
          @JoinColumn(name = "e1_id", referencedColumnName = "e1_id", insertable = false, updatable = false),
          @JoinColumn(name = "another_id", referencedColumnName = "another_id", insertable = false, updatable = false)
     })
     @MapsId("e1_id")
     @MapsId("another_id")
     private MerchantType merchantType;

     //Similar mapping to the one above, but with only one Join Column  
     private Merchant merchant;

     //One more local field that is irrelevant to the mapping
     //but is the one that is forcing me to map a many - to - many relationship
     //in this way.

}

//MerchantMerchantTypeAssociationPk as a simple embeddable

问题:当注释“ @MapsId”不能重复且不接受多个值时,如何为此类实体进行映射?

您没有包括MerchantMerchantTypeAssociationPk的代码,但是我猜测它看起来像这样:

@Embeddable
public class MerchantMerchantTypeAssociationPk {
    public MerchantPk merchantPK;
    public MerchantTypePk merchantTypePK;
}

@MapsId用来指明该关系属性对应,而不是列的复合关键字中的属性 因此, MerchantMerchantTypeAssociation应该如下所示:

@Entity class MerchantMerchantTypeAssociation {

    @EmbeddedId
    private MerchantMerchantTypeAssociationPk id;

    @ManyToOne
    @JoinColumns({
         @JoinColumn(name = "e1_id", referencedColumnName = "e1_id",...),
         @JoinColumn(name = "e2_id", referencedColumnName = "e2_id",...)
    })
    @MapsId("merchantTypePK") // <<< *attribute* in Embeddable
    private MerchantType merchantType;

    @ManyToOne
    @JoinColumn(name = "m_id", referencedColumnName = "merchant_id",...)
    @MapsId("merchantPK") // <<< *attribute* in Embeddable
    private Merchant merchant;
}

JPA 2.1规范的第2.4.1节中讨论了派生身份。

暂无
暂无

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

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