`

Hibernate继承映射(annotation)

 
阅读更多

三种方式:

1.一张总表SINGLE_TABLE

2.每个类分别一张表TABLE_PER_CLASS

3.每个子类一张表JOINED

第一种:只有一张总表,表中有个字段区分每条记录属于哪个子类

Person.java

package com.test.hiberenate.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.java

package com.test.hiberenate.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.java

package com.test.hiberenate.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;
	}

}

 

生成的表:PERSON



 
 

第二种:每个类分别一张表,各个子类的记录之间没有重复的主键

Person.java

package com.test.hiberenate.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)
@TableGenerator(
		name = "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.java

package com.test.hiberenate.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.java

package com.test.hiberenate.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;
	}

}

 

生成的表:

PERSON



 

 

T_GEN_TABLE(生成主键的表)



 

 

STUDENT



 

 

TEACHER



 

 

第三种:每个子类一张表

Person.java

package com.test.hiberenate.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.javaTeacher.java同上

生成的表:

PERSON

 

 

STUDENT

 

 

TEACHER

 

 


 

  • 大小: 3.4 KB
  • 大小: 1.7 KB
  • 大小: 626 Bytes
  • 大小: 2.1 KB
  • 大小: 1003 Bytes
  • 大小: 2.2 KB
  • 大小: 1.1 KB
  • 大小: 1.9 KB
  • 大小: 1 KB
  • 大小: 1.7 KB
  • 大小: 1.1 KB
  • 大小: 1.4 KB
  • 大小: 775 Bytes
  • 大小: 1.8 KB
  • 大小: 857 Bytes
  • 大小: 2.1 KB
分享到:
评论

相关推荐

    hibernate annotation 中文文档

    2.2.4. 映射继承关系 2.2.4.1. 每个类一张表 2.2.4.2. 每个类层次结构一张表 2.2.4.3. 连接的子类 2.2.4.4. 从父类继承的属性 2.2.5. 映射实体Bean的关联关系 2.2.5.1. 一对一(One-to-one) 2.2.5.2. 多对一(Many-to-...

    精通Java Web整合开发(第2版)

    第12章 基于annotation注解技术的ssh 2整合开发 ...12.4.11 继承关系映射的annotation注解实现549 12.4.12 hibernate集合映射的annotation注解实现552 12.5 基于annotation的ssh 2整合开发554 12.6 小结563

    hibernate annotation帮助文档

    2.2.4. 映射继承关系 2.2.4.1. 每个类一张表 2.2.4.2. 每个类层次结构一张表 2.2.4.3. 连接的子类 2.2.4.4. 从父类继承的属性 2.2.5. 映射实体Bean的关联关系 2.2.5.1. 一对一(One-to-one) 2.2.5.2. 多对一...

    Hibernate+中文文档

    9.1. 继承映射特性(Features of inheritance mappings) 16.1. 别名注射(alias injection names) 19.1. 缓存策略提供商(Cache Providers) 19.2. 各种缓存提供商对缓存并发策略的支持情况(Cache Concurrency ...

    Hibernate4_Inheritance_Annotation:该程序演示了如何使用 Annotations 在 Hibernate 中使用继承

    Hibernate4_Inheritance_Annotation 该程序演示了如何使用 Annotations 在 Hibernate 中使用继承。 Hibernate 支持 3 种类型的继承 每个具体类一个表 - 每个子类都有一个表,该表也具有超类的所有属性。 (TABLE_PER...

    hibernate3.2中文文档(chm格式)

    9.1. 继承映射特性(Features of inheritance mappings) 16.1. 别名注射(alias injection names) 19.1. 缓存策略提供商(Cache Providers) 19.2. 各种缓存提供商对缓存并发策略的支持情况(Cache Concurrency ...

    HibernateAPI中文版.chm

    9.1. 继承映射特性(Features of inheritance mappings) 16.1. 别名注射(alias injection names) 19.1. 缓存策略提供商(Cache Providers) 19.2. 各种缓存提供商对缓存并发策略的支持情况(Cache Concurrency ...

    Hibernate注解.docx

    在Hibernate中使用注解,主要是为了替代映射文件,完成“类到表,属性到字段”的映射。 JPA提供了一套功能强大的注解。Hibernate直接使用了JPA的这套注解。当然,对于JPA中的一些不足,Hibernate又开发了一些自己的...

    Hibernate中文详细学习文档

    9.1.7. 隐式多态和其他继承映射混合使用 9.2. 限制 10. 与对象共事 10.1. Hibernate对象状态(object states) 10.2. 使对象持久化 10.3. 装载对象 10.4. 查询 10.4.1. 执行查询 10.4.2. 过滤集合 10.4.3. ...

    Hibernate 中文 html 帮助文档

    9.1.7. 隐式多态和其他继承映射混合使用 9.2. 限制 10. 与对象共事 10.1. Hibernate对象状态(object states) 10.2. 使对象持久化 10.3. 装载对象 10.4. 查询 10.4.1. 执行查询 10.4.1.1. 迭代式获取结果(Iterating ...

    Hibernate_3.2.0_符合Java习惯的关系数据库持久化

    9.1. 继承映射特性(Features of inheritance mappings) 16.1. 别名注射(alias injection names) 19.1. 缓存策略提供商(Cache Providers) 19.2. 各种缓存提供商对缓存并发策略的支持情况(Cache Concurrency ...

    最全Hibernate 参考文档

    9.1.7. 隐式多态和其他继承映射混合使用 9.2. 限制 10. 与对象共事 10.1. Hibernate对象状态(object states) 10.2. 使对象持久化 10.3. 装载对象 10.4. 查询 10.4.1. 执行查询 10.4.1.1. 迭代式获取结果(Iterating ...

    hibernate 体系结构与配置 参考文档(html)

    9. 继承映射(Inheritance Mappings) 9.1. 三种策略 9.1.1. 每个类分层结构一张表(Table per class hierarchy) 9.1.2. 每个子类一张表(Table per subclass) 9.1.3. 每个子类一张表(Table per subclass),使用辨别...

    Hibernate教程

    10.1.7. 隐式多态和其他继承映射混合使用 10.2. 限制 11. 与对象共事 11.1. Hibernate对象状态(object states) 11.2. 使对象持久化 11.3. 装载对象 11.4. 查询 11.4.1. 执行查询 11.4.1.1. 迭代式获取结果...

    Hibernate3的帮助文档

    10.1.7. 隐式多态和其他继承映射混合使用 10.2. 限制 11. 与对象共事 11.1. Hibernate对象状态(object states) 11.2. 使对象持久化 11.3. 装载对象 11.4. 查询 11.4.1. 执行查询 11.4.1.1. 迭代式获取结果...

    Hibernate注释大全收藏

    继承父类的一些属性,但不用父类作为映射实体,这时候需要 @MappedSuperclass 注解。 上述实体映射到数据库中的时候对应 Order 实体Bean, 其具有 id, lastUpdate, lastUpdater 三个属性。如果没有@MappedSuperclass ...

    hibernate3.04中文文档.chm

    10.1.7. 隐式多态和其他继承映射混合使用 10.2. 限制 11. 与对象共事 11.1. Hibernate对象状态(object states) 11.2. 使对象持久化 11.3. 装载对象 11.4. 查询 11.4.1. 执行查询 11.4.1.1. 迭代式获取结果...

Global site tag (gtag.js) - Google Analytics