关联表的JPA映射,其中一个实体具有复合键

时间:2015-07-09 17:18:11

标签: java hibernate jpa

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

  • 商家(merchant_id,...)。

  • MerchantType(id1,id2,...)

  • MerchantMerchantTypeAssociationTable(merchant_id,id1,id2)

我无法弄清楚如何映射关联表。映射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”无法重复且不接受多个值时,如何为这种实体制作映射?

1 个答案:

答案 0 :(得分:1)

您没有包含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节中讨论。