Powered By Blogger

Tuesday, December 3, 2013

Hibernate Composite Key Mapping (JPA)

   It is sometimes necessary to have composite keys in a database table. In such a scenario, it would be necessary to get hibernate to cater this requirement. This is an illustration of how it could be done.
   Consider an entity, BankFacilityAgent which has percentage and isKeyFacilitator as attributes and another attribute, key of type BankFacilityAgentKey. The class is annotated with @Table and @Entity. The class is bound to a database table, BANK_FAC_AGENT. Getters of attributes percentage and isKeyFacilitator are annotated with @javax.persistence.Column and @Basic. Note that getter of 'key' attribute is annotated with @Id.

===============================================================

package com.shyarmal.persistance.domain;

import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Table(name = "BANK_FAC_AGENT", catalog = "")
@Entity
public class BankFacilityAgent {

    private BankFacilityAgentKey key;
    private float percentage;
    private String isKeyFacilitator;

    @Id
    public BankFacilityAgentKey getKey() {
        return key;
    }

    public void setKey(BankFacilityAgentKey key) {
        this.key = key;
    }

    @javax.persistence.Column(name = "PERCENTAGE")
    @Basic
    public float getPercentage() {
        return percentage;
    }

    public void setPercentage(float percentage) {
        this.percentage = percentage;
    }

    @javax.persistence.Column(name = "KEY_FACILITATOR")
    @Basic
    public String getKeyFacilitator() {
        return isKeyFacilitator;
    }

    public void setKeyFacilitator(String keyFacilitator) {
        isKeyFacilitator = keyFacilitator;
    }
}


===============================================================


   BankFacilityAgentKey represents the composite key, which should implement Serializable and is annotated with @Embeddable. This class holds attributes that comprise the composite key (bankId, facilityAgentId). The getters of the attributes are annotated the same way as in the class above.

===============================================================

package com.shyarmal.persistance.domain;

import javax.persistence.Basic;
import javax.persistence.Embeddable;
import java.io.Serializable;

@Embeddable
public class BankFacilityAgentKey implements Serializable {

    private int bankId;
    private int facilityAgentId;

    public BankFacilityAgentKey(int bankId, int facilityAgentId) {
        this.bankId = bankId;
        this.facilityAgentId = facilityAgentId;
    }

    public BankFacilityAgentKey() {

    }

    @javax.persistence.Column(name = "BANK_ID")
    @Basic
    public int getBankId() {
        return bankId;
    }

    public void setBankId(int bankId) {
        this.bankId = bankId;
    }

    @javax.persistence.Column(name = "FACILITY_AGENT_ID")
    @Basic
    public int getFacilityAgentId() {
        return facilityAgentId;
    }

    public void setFacilityAgentId(int facilityAgentId) {
        this.facilityAgentId = facilityAgentId;
    }
}

===============================================================

   The database table mapped is BANK_FAC_AGENT with columns PERCENTAGE, KEY_FACILITATOR, BANK_ID and FACILITY_AGENT_ID where BANK_ID and FACILITY_AGENT_ID are the composite key.


thanks,
Shyarmal.