Sunday, October 27, 2013

Composite primary key -JPA

Composite Primary Key in JPA:-

Composite Primary Key :- Having more than one column as primary key in a data base table is called composite primary key

If a database table has composite primary key then how to represent it in JPA Entity bean?

@Id represents a primary key field

There are two ways of declaring composite primary key
  1. @IdClass
  2. @EmbeddedId with @Embeddable

Suppose we have a table where we depart_name and coll_name are as composite primary key

@IdClass :- using this approach we need to create a separate class,which holds the primary key variables.This class will be referred by entity using @IdClass(CollegeDeptPK.class)

public class CollegeDeptPK implements Serializable{

/**
*
*/
private static final long serialVersionUID = 1L;

private String collname;

private String deptname;

// getter and setters are generated here
}


@Entity
@IdClass(CollegeDeptPK.class)
public class Collegedept implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private String collname;
@Id
private String deptname;

public Collegedept() {
}

public String getCollname() {
return this.collname;
}

public void setCollname(String collname) {
this.collname = collname;
}

public String getDeptname() {
return this.deptname;
}

public void setDeptname(String deptname) {
this.deptname = deptname;
}

}

In the above example Collegedept refers CollegeDeptPK .
CollegeDeptPk has two variables collname and deptname which are composite priamry key
the same variables are declared in our entity class using @Id annotation

The primary key class must contain the fields that match the priamry key attributes in the entity in both name and type

Collegedept collegedept=em.find(Collegedept.class,"Primary Key");
So we need to give a primary key here ,so then how can we proceed in this scenario?
Create an object of CollegeDeptPK ,set the variable values and pass this object reference to find method.

CollegeDeptPK collegedeptpk=new CollegeDeptPK();
collegedeptpk.setCollname("GPREC");
collegedeptpk.setDeptname("EEE");

Collegedept collegedept=em.find(Collegedept.class,collegedeptpk);
Drawbacks :- If composite primary key is implemented using @IdClass then there is duplication of declaration of primary keys in both the cases.
Using @EmbeddedId will solve the above problem

@EmbeddedId :-
  • The priamry key class declaration is annotated @Embeddable and it has all the primary key declarations
  • In entity class instead of using @Id,@EmbeddedId is used.It will be annotated on the top of reference variable
    @EmbeddedId
    priavte CollegeDeptPK collegeDeptPk;
  • The Embeddable priamry key class must be configured in persistence.xml file similar to Entity class
    <class>com.CollegeDeptPK </class>

Primary Key class :-
@Embeddable
public class CollegeDeptPK implements Serializable{

/**
*
*/
private static final long serialVersionUID = 1L;

private String collname;

private String deptname;

public String getDeptname() {
return deptname;
}
//setter and getters
}

Entity class :-
@Entity
public class Collegedept implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
private CollegeDeptPK collegedeptpk;

public Collegedept() {
}
//getter and setters

}


Take away points :-
  • There are two ways of declaring composite primary keys
    @IdClass
    @EmbeddedId with @Embeddable
  • @EmbeddedId is preferred over @IdClass
  • Using @IdClass will have duplication of primary declarations

No comments:

Post a Comment

Like and Share