JOINED
SINGLE_TABLE
TABLE_PER_CLASS
Project.java
package com.test; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Inheritance; import javax.persistence.InheritanceType; @Entity @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) public class Project { private int projectId; private String projectName; @Id @GeneratedValue public int getProjectId() { return projectId; } public void setProjectId(int projectId) { this.projectId = projectId; } public String getProjectName() { return projectName; } public void setProjectName(String projectName) { this.projectName = projectName; } }
Module.java
package com.test; import javax.persistence.Entity; @Entity public class Module extends Project { private String moduleName; public String getModuleName() { return moduleName; } public void setModuleName(String moduleName) { this.moduleName = moduleName; } }
Task.java
package com.test; import javax.persistence.Entity; @Entity public class Task extends Module { private String tasknName; public String getTasknName() { return tasknName; } public void setTasknName(String tasknName) { this.tasknName = tasknName; } }
ProjectTest.java
package com.test; import static org.junit.Assert.*; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; import org.hibernate.engine.spi.EntityEntry; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; public class ProjectTest { static EntityManagerFactory entityManagerFactory; @BeforeClass public static void setUpBeforeClass() throws Exception { entityManagerFactory = Persistence .createEntityManagerFactory("org.hibernate.tutorial.jpa"); } @AfterClass public static void tearDownAfterClass() throws Exception { entityManagerFactory.close(); } @Before public void setUp() throws Exception { } @After public void tearDown() throws Exception { } @Test public void test() { EntityManager entityManager = entityManagerFactory .createEntityManager(); entityManager.getTransaction().begin(); try { Project project = new Project(); project.setProjectName("project1"); Module module = new Module(); module.setProjectName("project2"); module.setModuleName("module1"); Task task = new Task(); task.setProjectName("project3"); task.setModuleName("module3"); task.setTasknName("task1"); entityManager.persist(project); entityManager.persist(module); entityManager.persist(task); entityManager.getTransaction().commit(); } catch (Exception e) { entityManager.getTransaction().rollback(); } } }
Project.sql
create table Project ( projectId int4 not null, projectName varchar(255), primary key (projectId) )
projectid | projectname |
1 | project1 |
Module.sql
create table Module ( projectId int4 not null, projectName varchar(255), moduleName varchar(255), primary key (projectId) )
projectid | projectname | modulename |
2 | project2 | module1 |
Task.sql
create table Task ( projectId int4 not null, projectName varchar(255), moduleName varchar(255), tasknName varchar(255), primary key (projectId) )
projectid | projectname | modulename | tasknname |
3 | project3 | module3 | task1 |
0 件のコメント:
コメントを投稿