When a primary key in a table cannot uniquely identify a record, the federated primary key is required, and the following is the code that implements the federated primary key using the JPA annotations
1 You first need to create a composite primary key class to hold the attributes that require the production of a federated primary key, which needs to be serialized.
Package com.ericsson.adp.entity.cons;
import java.io.Serializable;
Public class CONSUMERGROUPMAPPK Implements serializable{
Private String msisdn;//Phone Number
Private Long taggroupid;//(10) Tag Group ID
Public String getmsisdn () {
return msisdn;
}
Public void setmsisdn (String msisdn) {
this. msisdn = MSISDN;
}
Public Long Gettaggroupid () {
return taggroupid;
}
Public void settaggroupid (Long taggroupid) {
this. taggroupid = Taggroupid;
}
}
Then write a class that corresponds to a table in the database that requires the use of @idclass (CONSUMERGROUPMAPPK) in this class. Class) introduces the composite primary key class written above
At the same time, the attribute that needs to be the Union primary key plus @id indicates that the attribute is the primary key.
Package com.ericsson.adp.entity.cons;
Import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Id;
import Javax.persistence.IdClass;
import javax.persistence.Table;
import Org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
@Entity
@IdClass (CONSUMERGROUPMAPPK. Class)
@Table (name= "T_cons_consumer_group_map")
@Cache (usage=cacheconcurrencystrategy. Read_write)
Public class consumergroupmap{
@Id
Private String msisdn;//Phone Number
@Id
Private Long taggroupid;//(10) Tag Group ID
Public Consumergroupmap () {
Super ();
TODO auto-generated constructor stub
}
Public Consumergroupmap (String msisdn, Long taggroupid) {
Super ();
this. msisdn = MSISDN;
this. taggroupid = Taggroupid;
}
Public String getmsisdn () {
return msisdn;
}
Public void setmsisdn (String msisdn) {
this. msisdn = MSISDN;
}
Public Long Gettaggroupid () {
return taggroupid;
}
Public void settaggroupid (Long taggroupid) {
this. taggroupid = Taggroupid;
}
}
JPA annotations Implement Federated primary keys