Search code examples
javahibernatejpahibernate-mapping

Hibernate Composite Key - add another key from @JoinColumns


So, I have the following entity:

@Entity
public class EntityOne{

    @EmbeddedId
    private EntityOneIdentity entityOneIdentity;


    @ManyToOne
    @JoinColumns(value = {
            @JoinColumn(name = "MerchantID", referencedColumnName = "MerchantID"),
            @JoinColumn(name = "TenantID", referencedColumnName = "TenantID")})
    private Merchant merchant;
 .......
}

With Identity (Composite key):

@Embeddable
public class EntityOneIdentity implements Serializable {

    @Column(name = "EntityID")
    private String entityID;

    @Column(name = "TenantID")
    private String tenantID;
....
}

My challenge is that I need the @JoinColumn("TenantID") that comes from Merchant Entity to also be a PK in my EntityOne (just tenantId! The "MerchantId" will be left as it is). The only thing I found was the @MapId adnotation, but this adds both FK mentioned in @JoinColumns to my wanted Primary Keys.

Thank you in advance!


Solution

  • Solved it by marking @JoinColumns with insertable/updatebla false and by creating an extra merchantId field which value I set in the merchant setter