hibernate的@EmbeddedId嵌入式主鍵詳解


一.前言

   在我們的日常開發中,有時候會用到數據庫進行設計的時候,采用了復合主鍵來來保證唯一性,下面介紹一下采用hibernate的@EmbeddedId嵌入式主鍵。

二.說明

  設計一個學生類,包含了三個字段

create tbale student(
    stu_no      char(11) not null,
    stu_name varchar2(30) not null,
    stu_class   varchar2(20)
)

stu_no : 學號、 stu_name : 姓名 、 stu_class : 班級
把stu_no、stu_name作為復合主鍵

三.studetn 學生類復合主鍵

package com.shine.account.composite.holdportfolio.po;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Embeddable;

/**
 * 類說明:studetn 學生類復合主鍵.
 * 
 * NOTE : 符合條件:
 *                 1.必須實現Serializable
 *                 2.必須有默認的 public無參數的構造方法、必須覆蓋 equals和 hashCode 方法,這些要求與使用復合主鍵的要求相同
 *                 3.將嵌入式主鍵類使用 @Embeddable 標注,表示這個是一個嵌入式類。
 *
 */
@Embeddable
@SuppressWarnings("serial")
public class StudentId  implements Serializable{
	
	/**學號*/
	private String stuNo;
	
	/**姓名*/
	private String stuName;
	
	public StudentId() {
		super();
	}
    
	@Column(name = "STU_NO", nullable = false, precision = 11)
	public String getStuNo() {
		return stuNo;
	}

	public void setStuNo(String stuNo) {
		this.stuNo = stuNo;
	}
    
	@Column(name = "STU_NAME", nullable = false)
	public String getStuName() {
		return stuName;
	}

	public void setStuName(String stuName) {
		this.stuName = stuName;
	}

	@Override
	public boolean equals(Object other) {
        if(this == other)
        	return true;
        if(other == null)
        	return false;
        if(!(other instanceof StudentId))
        	return false;
        
        StudentId otherStudentId = (StudentId) other;
        
		return this.getStuNo() == otherStudentId.stuNo &&
			   this.getStuName() == otherStudentId.stuNo;
	}
	
	@Override
	public int hashCode() {
		return this.stuNo.hashCode() + this.stuName.hashCode();
	}
}

四.po對象(采用java的注解的方式,來注解po)

package com.shine.account.composite.holdportfolio.po;

import java.io.Serializable;

import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Table;


/**
 * 類說明:學習實體類.
 */
@Entity
@Table(name = "STUDENT")
@SuppressWarnings("serial")
public class Student implements Serializable{
	
	/**復合主鍵類*/
	private StudentId studentId;
	
	/**學生對應班級*/
	private String stuClass;
    
	/**
	 * 	@EmbeddedId   復合主鍵id
	 *  @AttributeOverrides 屬性映射
	 */
	@EmbeddedId
	@AttributeOverrides({
		@AttributeOverride(name = "stuNo", column =  @Column(name = "STU_NO", nullable = false, precision = 11)),
		@AttributeOverride(name = "stuName", column =  @Column(name = "STU_NAME", nullable = false))
	})
	public StudentId getStudentId() {
		return studentId;
	}

	public void setStudentId(StudentId studentId) {
		this.studentId = studentId;
	}
    
	@Column(name = "STU_CLASS", nullable = false)
	public String getStuClass() {
		return stuClass;
	}

	public void setStuClass(String stuClass) {
		this.stuClass = stuClass;
	}
}   


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM