JPA中的继承映射

这是一个Java Project,演示的是JPA中的继承映射

首先是部门表的实体类

package com.jadyer.model; import java.util.Set; import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.OneToMany; /** * 部门表的实体 * @see 三个属性的setter和getter略 */ @Entity public class Department { @Id @GeneratedValue private int id; private String name; @OneToMany(targetEntity=Employee.class, fetch=FetchType.LAZY,mappedBy="depart", cascade={CascadeType.MERGE, CascadeType.PERSIST}) private Set<Employee> emps; }

然后是员工表的实体类

package com.jadyer.model; import javax.persistence.DiscriminatorColumn; import javax.persistence.DiscriminatorValue; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Inheritance; import javax.persistence.InheritanceType; import javax.persistence.ManyToOne; /** * 员工表的实体 * @see 三个属性的setter和getter略 * @see InheritanceType.JOINED------------>每个子类一张表 * @see InheritanceType.SINGLE_TABLE------>单表,一张表映射一个继承树 * @see InheritanceType.TABLE_PER_CLASS--->每个具体类一张表 */ @Entity @Inheritance(strategy=InheritanceType.SINGLE_TABLE) //指定继承策略 @DiscriminatorColumn(name="type") //指定鉴别器的列 @DiscriminatorValue("1") //指定employee表的鉴别器的值 public class Employee { @Id @GeneratedValue private int id; private String name; //@Column(name="depart_id") //JPA缺省就会生成depart_id字段,这要比Hibernate好一些,而Hibernate缺省的属性名和字段名是相同的 @ManyToOne() //关于它的targetEntity属性,通过反射就会自动找到,所以我们不用理会 private Department depart; @Override public String toString() { return "id=" + this.id + "/tname=" + this.name; } }

用来代表工资的员工表的子类

package com.jadyer.model; import javax.persistence.DiscriminatorValue; import javax.persistence.Entity; /** * 工资 */ @Entity @DiscriminatorValue("2") //指定sales表的鉴别器的值 public class Sales extends Employee { private int sell; public int getSell() { return sell; } public void setSell(int sell) { this.sell = sell; } }

用来代表技能的员工表的子类

package com.jadyer.model; import javax.persistence.DiscriminatorValue; import javax.persistence.Entity; /** * 技能 */ @Entity @DiscriminatorValue("3") //指定skiller表的鉴别器的值 public class Skiller extends Employee { private String skill; public String getSkill() { return skill; } public void setSkill(String skill) { this.skill = skill; } }

用到的位于src下的META-INF目录中的persistence.xml文件

<?xml version="1.0" encoding="UTF-8"?> <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0"> <persistence-unit name="JPAExtendDemo" transaction-type="RESOURCE_LOCAL"> <!-- 指定实现JPA的提供者 --> <provider>org.hibernate.ejb.HibernatePersistence</provider> <properties> <property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect" /> <property name="hibernate.show_sql" value="true"/> <property name="hibernate.format_sql" value="true"/> <property name="hibernate.hbm2ddl.auto" value="update" /> <property name="hibernate.connection.driver_class" value="oracle.jdbc.OracleDriver" /> <property name="hibernate.connection.username" value="scott" /> <property name="hibernate.connection.password" value="jadyer" /> <property name="hibernate.connection.url" value="jdbc:oracle:thin:@127.0.0.1:1521:jadyer" /> </properties> </persistence-unit> </persistence>

最后是用到的JUnit4单元测试类ExtendTest.java

package com.jadyer.junit; import java.util.HashSet; import java.util.Set; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; import org.junit.Test; import com.jadyer.model.Department; import com.jadyer.model.Employee; import com.jadyer.model.Sales; import com.jadyer.model.Skiller; public class ExtendTest { @Test public void save(){ EntityManagerFactory factory = Persistence.createEntityManagerFactory("JPAExtendDemo"); EntityManager em = factory.createEntityManager(); em.getTransaction().begin(); Department depart = new Department(); depart.setName("古龙武侠"); Employee emp11 = new Employee(); emp11.setDepart(depart); emp11.setName("沈浪"); Skiller emp22 = new Skiller(); emp22.setDepart(depart); emp22.setName("王怜花"); emp22.setSkill("skill"); Sales emp33 = new Sales(); emp33.setDepart(depart); emp33.setName("李寻欢"); emp33.setSell(88); Set<Employee> emps = new HashSet<Employee>(); emps.add(emp22); emps.add(emp11); emps.add(emp33); depart.setEmps(emps);//将三名员工加入到部门中 em.persist(depart); em.getTransaction().commit(); em.close(); factory.close(); } }

你可能感兴趣的:(Hibernate,String,jpa,Class,inheritance,getter)