JPA 继承映射

package com.jvwl.model;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
/**
 * 基类
 * @author Jerval
 *
 */
@MappedSuperclass
public class BaseBean{
	private int id;
	private String name;

	@Id @GeneratedValue
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	@Column(length=10)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}

 

package com.jvwl.model;

import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.ManyToOne;
/**
 * 通用员工类
 * @author Jerval
 *
 */
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type", length = 10, discriminatorType = DiscriminatorType.INTEGER)
@DiscriminatorValue("0")
public class Employee extends BaseBean{
	/**
	 * 所属部门
	 */
	private Department department;

	@ManyToOne
	public Department getDepartment() {
		return department;
	}

	public void setDepartment(Department department) {
		this.department = department;
	}

}

 

package com.jvwl.model;

import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
/**
 * 销售员工类
 * @author Jerval
 *
 */
@Entity
@DiscriminatorValue("2")
public class Sells extends Employee {
	/**
	 * 销售数量
	 */
	private int sell;

	public int getSell() {
		return sell;
	}

	public void setSell(int sell) {
		this.sell = sell;
	}

}

 

package com.jvwl.model;

import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
/**
 * 技术员工类
 * @author Jerval
 *
 */
@Entity
@DiscriminatorValue("3")
public class Skiller extends Employee {
	/**
	 * 所具技能
	 */
	private String skill;

	public String getSkill() {
		return skill;
	}

	public void setSkill(String skill) {
		this.skill = skill;
	}

}

 

package com.jvwl.model;

import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
/**
 * 部门类
 * @author Jerval
 *
 */
@Entity
public class Department {
	/**
	 * 部门Id
	 */
	private int id;
	/**
	 * 部门名称
	 */
	private String name;
	/**
	 * 部门员工
	 */
	private Set<Employee> employees;

	@Id @GeneratedValue
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	@Column(length = 10)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@OneToMany(cascade=CascadeType.ALL,fetch=FetchType.LAZY,
			mappedBy="department",targetEntity=Employee.class)
			//targetEntity=Employee.class主要是对在Set中没有使用范型时来使用
	public Set<Employee> getEmployees() {
		return employees;
	}

	public void setEmployees(Set<Employee> employees) {
		this.employees = employees;
	}

}

 

package junit.test;

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.jvwl.model.Department;
import com.jvwl.model.Employee;
import com.jvwl.model.Sells;
import com.jvwl.model.Skiller;

public class JPATest {
@Test public void testInheritance(){
	EntityManagerFactory factory = Persistence.createEntityManagerFactory("jvwl");
	EntityManager manager = factory.createEntityManager();
	manager.getTransaction().begin();
	
	Department department = new Department();
	department.setName("department");
	
	Employee emp1 = new Employee();
	emp1.setName("emp1");
	emp1.setDepartment(department);
	
	Sells emp2 = new Sells();
	emp2.setName("emp2");
	emp2.setDepartment(department);
	emp2.setSell(2222);
	
	Skiller emp3 = new Skiller();
	emp3.setName("emp3");
	emp3.setDepartment(department);
	emp3.setSkill("skill");
	
	Set<Employee> employees = new HashSet<Employee>();
	employees.add(emp1);
	employees.add(emp2);
	employees.add(emp3);
	department.setEmployees(employees);
	
	manager.persist(department);
	manager.getTransaction().commit();
}
}

 

你可能感兴趣的:(jpa,JUnit)