hibernate继承关系映射的JPA方式实现 ——TABLE_PER_CLASS

每个类一张表
package entity;

import javax.persistence.*;

@Entity

//每个类一张表
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@TableGenerator(
name = "t_gen",
table = "t_gen_table",
pkColumnName = "t_pk",
valueColumnName = "t_value",//生成主键下一个值
pkColumnValue = "person_pk",//
initialValue = 0,
allocationSize = 1

)

public class Person {
private int id;
private String name;
@Id
//single table
@GeneratedValue(strategy = GenerationType.IDENTITY)
//用一行维护ID
@GeneratedValue(generator = "t_gen",strategy = GenerationType.TABLE)
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;
}

}

package entity;

import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;

@Entity
public class Teacher extends Person{

private String tittle;

public String getTittle() {
    return tittle;
}

public void setTittle(String tittle) {
    this.tittle = tittle;
}

}

package entity;

import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;

@Entity
public class Student extends Person {
private int score;

public int getScore() {
    return score;
}

public void setScore(int score) {
    this.score = score;
}

}

你可能感兴趣的:(hibernate继承关系映射的JPA方式实现 ——TABLE_PER_CLASS)