Three ways of Hibernate inheritance mapping

Single table design

Put all the tables into one table of the parent class. A DiscriminatorColumn is needed to distinguish different subclasses.
Create Person, Student, Teacher class Student and Teacher all inherit Person.

Use @ inheritance in Person class (strategy = inheritancetype. Single_table)
Specify the design pattern for the 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;
	}

}

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

	
}

Three independent tables (table per class)

Three independent tables, id cannot be the same
Class 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 is used to record the id value. Guarantee not to repeat
@TableGenerator(
		name="t_gen",//The name here should be the same as the following generator="t_gen" value
		table="t_gen_table",//Table name
		pkColumnName="t_pk",//The field used to save the primary key name in the table
		valueColumnName="t_value",//The field used to save the primary key value in the table
		pkColumnValue="person_pk",//The value corresponding to the name field in the table
		initialValue=1,//Initial value
		allocationSize=1//Auto growth value
		)
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;
	}

}

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

Class 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 mode

Create two tables and save their own functions. The parent table holds the id of the child table and the name of the child table creation.
Class 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;
	}

}

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

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

	
}

Published 9 original articles, won 0 praise and 30 visitors
Private letter follow

Keywords: Hibernate

Added by kkessler on Wed, 29 Jan 2020 17:23:49 +0200