第一种方式:新建主键类,主键类添加 @Embeddable 注解,在实体类的主键类属性上添加 @Id 注解
package com.hibernate.entity;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
// 教师类
@Entity
@Table(name = "t_teacher")
public class Teacher implements Serializable {
private static final long serialVersionUID = 3543286868313801941L;
@Id
private TeacherPK teacherPK;
private String title;
public TeacherPK getTeacherPK() {
return teacherPK;
}
public String getTitle() {
return title;
}
public void setTeacherPK(TeacherPK teacherPK) {
this.teacherPK = teacherPK;
}
public void setTitle(String title) {
this.title = title;
}
}
package com.hibernate.entity;
import java.io.Serializable;
import javax.persistence.Embeddable;
// 主键类
@Embeddable
public class TeacherPK implements Serializable {
private static final long serialVersionUID = 6939546618791078447L;
private Long id;
private String name;
@Override
public boolean equals(Object obj) {
if (obj instanceof TeacherPK) {
TeacherPK teacherPK = (TeacherPK) obj;
if (this.id == teacherPK.getId() && this.name.equals(teacherPK.getName())) {
return true;
}
}
return false;
}
public Long getId() {
return id;
}
public String getName() {
return name;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
public void setId(Long id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
}
第二种方式:新建主键类,实体类的主键类属性上添加 @EmbaddedId 注解
package com.hibernate.entity;
import java.io.Serializable;
// 主键类
public class TeacherPK implements Serializable {
private static final long serialVersionUID = 6939546618791078447L;
private Long id;
private String name;
@Override
public boolean equals(Object obj) {
if (obj instanceof TeacherPK) {
TeacherPK teacherPK = (TeacherPK) obj;
if (this.id == teacherPK.getId() && this.name.equals(teacherPK.getName())) {
return true;
}
}
return false;
}
public Long getId() {
return id;
}
public String getName() {
return name;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
public void setId(Long id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
}
package com.hibernate.entity;
import java.io.Serializable;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Table;
// 教师类
@Entity
@Table(name = "t_teacher")
public class Teacher implements Serializable {
private static final long serialVersionUID = 3543286868313801941L;
@EmbeddedId
private TeacherPK teacherPK;
private String title;
public TeacherPK getTeacherPK() {
return teacherPK;
}
public String getTitle() {
return title;
}
public void setTeacherPK(TeacherPK teacherPK) {
this.teacherPK = teacherPK;
}
public void setTitle(String title) {
this.title = title;
}
}
第三种方式:新建主键类,实体类添加 @IdClass 注解指向主键类,实体类的主键属性添加 @Id 注解
package com.hibernate.entity;
import java.io.Serializable;
// 主键类
public class TeacherPK implements Serializable {
private static final long serialVersionUID = 6939546618791078447L;
private Long id;
private String name;
@Override
public boolean equals(Object obj) {
if (obj instanceof TeacherPK) {
TeacherPK teacherPK = (TeacherPK) obj;
if (this.id == teacherPK.getId() && this.name.equals(teacherPK.getName())) {
return true;
}
}
return false;
}
public Long getId() {
return id;
}
public String getName() {
return name;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
public void setId(Long id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
}
package com.hibernate.entity;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.Table;
// 教师类
@Entity
@Table(name = "t_teacher")
@IdClass(value = TeacherPK.class)
public class Teacher implements Serializable {
private static final long serialVersionUID = 3543286868313801941L;
@Id
private Long id;
@Id
private String name;
private String title;
public Long getId() {
return id;
}
public String getName() {
return name;
}
public String getTitle() {
return title;
}
public void setId(Long id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setTitle(String title) {
this.title = title;
}
}