JPA annotations Implement Federated primary keys

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.