Added by on 2013-07-05

Project.java

@Entity
@Inheritance(strategy=InheritanceType.THEINHERITANCE)
public class Project{
	@Id
	@generatedValue
	private int projectId;
	private String projectName;
}

Module.java

@Entity
public class Module extends Project{
	private String moduleName;
}

Task.java

@Entity
public class Task extends Module{
	private String taskName;
}

MainTest.java

	Project p = new Project();
	p.setProjectName("Hibernate Lesson");
	session.save(p);
	
	Module m = new Module();
	m.setProjectName("Spring Lesson");
	m.setModuleName("AOP");
	session.save(m);
	
	Task t = new Task();
	t.setProjectName("Java Lesson");
	t.setModuleName("Collections");
	t.setTaskName("ArrayList");
	session.save(t);

1. THEINHERITANCE = SINGLE_TABLE
The result will be one table called “Project”:

projectId projectName moduleName taskName
1 Hibernate Lesson Null Null
2 Spring Lesson AOP Null
3 Java Lesson Collection ArrayList

2. THEINHERITANCE = JOINED
The result will be 3 tables

Project
projectId projectName
1 Hibernate Lesson
2 Spring Lesson
3 Java Lesson
Module
projectId moduleName
1 AOP
2 Collections
Task
projectId taskName
3 ArrayList

3. THEINHERITANCE = TABLE_PER_CLASS

Project
projectId projectName
1 Hibernate Lesson
Module
projectId projectName moduleName
2 Spring Lesson AOP
Task
projectId projectName moduleName taskName
3 Java Lesson Collections ArrayList

Category:

Hibernate

Leave a Reply

Your email address will not be published. Required fields are marked *

*


*