欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

4.hibernate联合主键,jpa映射

程序员文章站 2022-04-24 22:25:47
...

package com.bjsxt.hibernate;

public enum Gender {
MALE, FEMALE
}




package com.bjsxt.hibernate;

import javax.persistence.Embeddable;


public class TeacherPK implements java.io.Serializable{
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

@Override
public boolean equals(Object o) {
if(o instanceof TeacherPK) {
TeacherPK pk = (TeacherPK)o;
if(this.id == pk.getId() && this.name.equals(pk.getName())) {
return true;
}
}
return false;
}

@Override
public int hashCode() {
return this.name.hashCode();
}
}




package com.bjsxt.hibernate;

import java.util.Date;

import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.IdClass;
import javax.persistence.SequenceGenerator;

import javax.persistence.Id;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

@Entity
@javax.persistence.TableGenerator(
name="Teacher_GEN",
table="GENERATOR_TABLE",
pkColumnName = "pk_key",
valueColumnName = "pk_value",
pkColumnValue="Teacher",
allocationSize=1
)

@SequenceGenerator(name="teacherSEQ", sequenceName="teacherSEQ_DB")
@IdClass(TeacherPK.class)
public class Teacher {
//private TeacherPK pk;
private int id;
private String name;
private String title;
private String yourWifeName;
private Date birthDate;
private boolean good;
private Gender gender;

@Enumerated(EnumType.STRING)
public Gender getGender() {
return gender;
}
public void setGender(Gender gender) {
this.gender = gender;
}
public boolean isGood() {
return good;
}
public void setGood(boolean good) {
this.good = good;
}
@Transient
public String getYourWifeName() {
return yourWifeName;
}
public void setYourWifeName(String yourWifeName) {
this.yourWifeName = yourWifeName;
}
@Id
@GeneratedValue(strategy=GenerationType.TABLE, generator="Teacher_GEN")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}

@Id
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Temporal(TemporalType.TIME)
public Date getBirthDate() {
return birthDate;
}
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}

/*@EmbeddedId
public TeacherPK getPk() {
return pk;
}
public void setPk(TeacherPK pk) {
this.pk = pk;
}*/
}