美文网首页
hibernate继承关系映射的JPA方式实现 ——TABLE_

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

作者: 我我我我我_a70b | 来源:发表于2019-05-06 21:37 被阅读0次

每个类一张表
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_

      本文链接:https://www.haomeiwen.com/subject/phjjoqtx.html