用于定义复合主键
方法1:
(1)编写一个嵌入式主键的类testPK
作为嵌入式主键类,要满足以下几点要求。
1.必须实现Serializable接口、必须有默认的public无参数的构造方法、必须覆盖equals 和hashCode方法,这些要求与使用复合主键的要求相同。
2.将嵌入式主键类使用@Embeddable标注,表示这个是一个嵌入式类。
@Embeddable
public class TestPK {
private String firstName;
private String lastName;
public TestPK() {
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public boolean equals(Object o) {
testPK other = (testPK) obj;
EqualsBuilder builder = new EqualsBuilder();
builder.append(firstName, other.firstName).append(lastName,other.lastName);
boolean equals = builder.isEquals();
return equals;
}
@Override
public int hashCode() {
HashCodeBuilder builder = new HashCodeBuilder();
builder.append(firstName).append(lastName);
int code = builder.hashCode();
return code;
}
}
(2)通过@EmbeddedId注释标注实体中的嵌入式主键
注意:
1.必须要实现serializable接口
2.需要有无参的构造函数
3.@Embeddable注释,表示此类可以被插入某个entity中
4.两个class和Column的列名都必需与表里的列名一致,建立sql时,会自动选择TestPK里的列名
@Entity
@Table(name = "schemaname.tablename")
public class Test {
@EmbeddedId
private TestPK id;
private String name;
public Test() {
}
public int getId() {
return id.getId();
}
@Column(name = "firstname")
public String getFirstName(){
return id.getFirstName()
}
@Column(name = "lastname")
public String getLastName(){
return id.getLastName()
}
@Column(name = "name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
方法2:
(3)不想在TestPK中定义列名,可以用以下方法,用于公共复合主键
通过@AttributeOverride注释来指定Test类的firstName,lastName与数据库中表的first_name,last_name进行映射.
以下代码在class Test
@EmbeddedId
@AttributeOverrides( {
@AttributeOverride(name = firstName, column = @Column(name = first_name)),
@AttributeOverride(name = lastName, column = @Column(name = last_name)) })
不需要getFirstName,getLastName
方法1:
(1)编写一个嵌入式主键的类testPK
作为嵌入式主键类,要满足以下几点要求。
1.必须实现Serializable接口、必须有默认的public无参数的构造方法、必须覆盖equals 和hashCode方法,这些要求与使用复合主键的要求相同。
2.将嵌入式主键类使用@Embeddable标注,表示这个是一个嵌入式类。
@Embeddable
public class TestPK {
private String firstName;
private String lastName;
public TestPK() {
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public boolean equals(Object o) {
testPK other = (testPK) obj;
EqualsBuilder builder = new EqualsBuilder();
builder.append(firstName, other.firstName).append(lastName,other.lastName);
boolean equals = builder.isEquals();
return equals;
}
@Override
public int hashCode() {
HashCodeBuilder builder = new HashCodeBuilder();
builder.append(firstName).append(lastName);
int code = builder.hashCode();
return code;
}
}
(2)通过@EmbeddedId注释标注实体中的嵌入式主键
注意:
1.必须要实现serializable接口
2.需要有无参的构造函数
3.@Embeddable注释,表示此类可以被插入某个entity中
4.两个class和Column的列名都必需与表里的列名一致,建立sql时,会自动选择TestPK里的列名
@Entity
@Table(name = "schemaname.tablename")
public class Test {
@EmbeddedId
private TestPK id;
private String name;
public Test() {
}
public int getId() {
return id.getId();
}
@Column(name = "firstname")
public String getFirstName(){
return id.getFirstName()
}
@Column(name = "lastname")
public String getLastName(){
return id.getLastName()
}
@Column(name = "name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
方法2:
(3)不想在TestPK中定义列名,可以用以下方法,用于公共复合主键
通过@AttributeOverride注释来指定Test类的firstName,lastName与数据库中表的first_name,last_name进行映射.
以下代码在class Test
@EmbeddedId
@AttributeOverrides( {
@AttributeOverride(name = firstName, column = @Column(name = first_name)),
@AttributeOverride(name = lastName, column = @Column(name = last_name)) })
不需要getFirstName,getLastName