hibernate笔记-008-联合主键注解配置

第一种方式:新建主键类,主键类添加 @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;
	}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值