How to use the federated primary key in Hibernate, why serialize, why rewrite the hashcode and Equals methods

Source: Internet
Author: User

There are three main ways of using Hibernate annotations for federated primary keys:

First, the field of the Federated primary key is placed in a single class, which needs to implement the Java.io.Serializable interface and override Equals and Hascode, then annotate the class as @embeddable, and finally in the main class (the class does not contain fields from the Federated primary Key Class) Save a reference to the Federated primary key class and generate the set and get methods, and annotate the reference as @id

Package com.test.entity;

Import java.io.Serializable;

Import Javax.persistence.Column;
Import javax.persistence.Embeddable;

@Embeddable
public class TESTPK implements serializable{
Private static final long serialversionuid = 1L;
@Column (name= "id")
private int id;
@Column (name= "Sid")
Private String SID;

public int getId () {
return ID;
}
public void setId (int id) {
This.id = ID;
}
Public String GetSID () {
return SID;
}
public void Setsid (String sid) {
This.sid = SID;
}
}

Package com.test.entity;

Import Javax.persistence.Column;
Import javax.persistence.Entity;
Import Javax.persistence.Id;
Import javax.persistence.Table;
@Table (name= "test")
@Entity
public class Test1 {
@Column (name= "date")
Private String date;
@Id
Private TESTPK TESTPK;
Public String getDate () {
return date;
}
public void SetDate (String date) {
This.date = date;
}
Public TESTPK GETTESTPK () {
return TESTPK;
}
public void Settestpk (TESTPK testpk) {
THIS.TESTPK = TESTPK;
}


}





Second, the field of the Federated primary key is placed in a single class, which needs to implement the Java.io.Serializable interface and override Equals and Hascode, and finally save a reference to the Federated primary key class in the main class (the class does not contain a field in the Federated primary Key Class). and generate the set and get methods, and annotate the reference as @embeddedid

Package com.test.entity;

Import java.io.Serializable;

Import Javax.persistence.Column;

public class TESTPK implements serializable{
Private static final long serialversionuid = 1L;
@Column (name= "id")
private int id;
@Column (name= "Sid")
Private String SID;

public int getId () {
return ID;
}
public void setId (int id) {
This.id = ID;
}
Public String GetSID () {
return SID;
}
public void Setsid (String sid) {
This.sid = SID;
}
}
Package com.test.entity;
Import Java.util.Date;

Import Javax.persistence.Column;
Import Javax.persistence.EmbeddedId;
Import javax.persistence.Entity;
Import javax.persistence.Table;
@Table (name= "test")
@Entity
public class Test1 {
@Column (name= "date")
private date date;
@EmbeddedId
Private TESTPK TESTPK;
Public Date getDate () {
return date;
}
public void SetDate (date date) {
This.date = date;
}
Public TESTPK GETTESTPK () {
return TESTPK;
}
public void Settestpk (TESTPK testpk) {
THIS.TESTPK = TESTPK;
}


}



Third, place the fields of the Federated primary key in a single class, which requires implementing the Java.io.Serializable interface and overriding Equals and hashcode. Finally, in the main class, which contains the fields in the Federated primary Key class, the Federated primary key field is annotated as @id. And above this class will be such annotations: @IdClass (Union primary key class. Class)

Package com.test.entity;

Import java.io.Serializable;

Import Javax.persistence.Column;

public class TESTPK implements serializable{
Private static final long serialversionuid = 1L;
@Column (name= "id")
private int id;
@Column (name= "Sid")
Private String SID;

public int getId () {
return ID;
}
public void setId (int id) {
This.id = ID;
}
Public String GetSID () {
return SID;
}
public void Setsid (String sid) {
This.sid = SID;
}
}
Package com.test.entity;
Import Java.util.Date;

Import Javax.persistence.Column;
Import javax.persistence.Entity;
Import Javax.persistence.Id;
Import Javax.persistence.IdClass;
Import javax.persistence.Table;
@Table (name= "test")
@IdClass (Testpk.class)
@Entity
public class Test1 {
public int getId () {
return ID;
}
public void setId (int id) {
This.id = ID;
}
Public String GetSID () {
return SID;
}
public void Setsid (String sid) {
This.sid = SID;
}
@Column (name= "date")
private date date;
@Id
private int id;
@Id
Private String SID;
Public Date getDate () {
return date;
}
public void SetDate (date date) {
This.date = date;
}



}



Serialization reason: If more than one such object is put into memory at the same time, in a clustered system,

When one of the servers is down, the objects in memory need to be written to other servers.

Also, if the server's memory is full and virtual memory is required, it needs to be serialized before it can be written to the hard disk

Overriding the Hashcode () and equal () method reason: This is to guarantee the uniqueness of the object.

Put a lot of objects in memory, what is the difference between them? The database is distinguished by that primary key,

Therefore, the hashcode () and equal () methods should be rewritten here

Transfer from csdn:52302463

How to use the federated primary key in Hibernate, why serialize, why rewrite the hashcode and Equals methods

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.