Hibernate继承映射的三种方式

单表设计(SINGLE_TABLE)

将所有的表都放入父类的一张表中, 需要一个DiscriminatorColumn来区别不同的子类。
创建Person,Student,Teacher类Student和Teacher都继承Person。

Person类中使用@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
指定表的设计模式
@DiscriminatorColumn(name=“discriminator”,
discriminatorType=DiscriminatorType.STRING)

package com.hibernate.model;

import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;


@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="discriminator", discriminatorType=DiscriminatorType.STRING)
@DiscriminatorValue("person")
public class Person {
	private int id;
	private String name;
	
	@Id
	@GeneratedValue
	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;
	}

}

Student类

package com.hibernate.model;

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

@Entity
@DiscriminatorValue("student")
public class Student extends Person {
	
	private int score;

	public int getScore() {
		return score;
	}

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

Teacher

package com.hibernate.model;

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

@Entity
@DiscriminatorValue("teacher")
public class Teacher extends Person {
	private String title;

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	
}

三张独立的表(TABLE_PER_CLASS)

三张独立的表,id不能相同
person类

package com.hibernate.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.TableGenerator;

@Entity
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
//t_gen_table用来记录id值。保证不重复
@TableGenerator(
		name="t_gen",//此处的名字要和下面generator="t_gen"值一样
		table="t_gen_table",//表名
		pkColumnName="t_pk",//表里用来保存主键名字的字段
		valueColumnName="t_value",//表里用来保存主键值的字段
		pkColumnValue="person_pk",//表里名字字段对应的值
		initialValue=1,//初始值
		allocationSize=1//自动增长值
		)
public class Person {
	private int id;
	private String name;
	
	@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;
	}

}

Student类

package com.hibernate.model;

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;
	}
	
}

Teacher类

package com.hibernate.model;

import javax.persistence.Entity;

@Entity
public class Teacher extends Person {
	private String title;

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	
}

JOINED模式

创建两张表,子表保存自己的功能。父表保存子表的id和子表创建的名字。
Person类

package com.hibernate.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;

@Entity
@Inheritance(strategy=InheritanceType.JOINED)

public class Person {
	private int id;
	private String name;
	
	@Id
	@GeneratedValue
	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;
	}

}

student类

package com.hibernate.model;

import javax.persistence.Entity;

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

	public int getScore() {
		return score;
	} //Person p = Person(load(1));

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

Teacher类

package com.hibernate.model;

import javax.persistence.Entity;

@Entity
public class Teacher extends Person {
	private String title;

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值