第一、将联合主键的字段单独放在一个类中,该类需要实现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() 方法