Hibernate中用到聯合主鍵的使用方法,為何要序列化,為何要重寫hashcode 和 equals 方法
第一、將聯合主鍵的欄位單獨放在一個類中,該類需要實現java.io.Serializable介面並重寫equals和hascode,再將該類註解為@Embeddable,最後在主類中(該類不包含聯合主鍵類中的欄位)儲存該聯合主鍵類的一個引用,並生成set和get方法,並將該引用註解為@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;
}
}
第二、將聯合主鍵的欄位單獨放在一個類中,該類需要實現java.io.Serializable介面並重寫equals和hascode,最後在主類中(該類不包含聯合主鍵類中的欄位)儲存該聯合主鍵類的一個引用,並生成set和get方法,並將該引用註解為@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;
}
}
第三、將聯合主鍵的欄位單獨放在一個類中,該類需要實現java.io.Serializable介面並要重寫equals和hashcode.最後在主類中(該類包含聯合主鍵類中的欄位)將聯合主鍵欄位都註解為@Id,並在該類上方將上這樣的註解:@IdClass(聯合主鍵類.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;
}
}
序列化原因 :如果多個該類物件同時放入記憶體中,在一個集群系統中,
其中一臺伺服器當機了,需要將記憶體中物件寫到其它伺服器。
同時,如果該伺服器記憶體以滿,需要用虛擬記憶體,這就需要序列化後才能寫到硬碟上
重寫hashCode()和equal()方法 原因 : 這是為了保證物件唯一性的。
將許多物件放在記憶體中,他們之間用什麼區分呢?資料庫中用那個主鍵來區分的,
因此在這兒應該重寫hashCode()和equal()方法