JPA学习记录三(使用jpa映射关联和继承)

xiaoxiao2021-02-28  85

这次进行学习:使用jpa映射关联和继承

还是在上次的代码上进行编写:

首先添加Employee.java

package cn.zl.jpa.domain; 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.JoinColumn; import javax.persistence.ManyToOne; @Entity //选择继承策略 //strategy=TABLE_PER_CLASS :设置为该选项表示每个类使用一个表; //strategy=SINGLE_TABLE :设置为该选项表示所有类及其子类共用一个表; //strategy =JOINED : 设置为该选项表示每个类使用子表保存子类比父类多出的属性 @Inheritance(strategy=InheritanceType.SINGLE_TABLE) @DiscriminatorColumn(name="type")//鉴别器 @DiscriminatorValue("0") public class Employee { @Id @GeneratedValue private int id; private String name; @ManyToOne() @JoinColumn(name="depart_id")//可以不用定义,直接默认depart private Department depart; 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; } public Department getDepart() { return depart; } public void setDepart(Department depart) { this.depart = depart; } public String toString(){ return "id="+this.id+"name="+this.name; } } 添加Sales.java.继承 Employee

package cn.zl.jpa.domain; import javax.persistence.DiscriminatorValue; import javax.persistence.Entity; @Entity @DiscriminatorValue("3") public class Sales extends Employee { private int sell; public int getSell() { return sell; } public void setSell(int sell) { this.sell = sell; } } 编写Department.java

package cn.zl.jpa.domain; 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; @Entity public class Department { @Id @GeneratedValue private int id; private String name; //不是范型,需要加上目标实体 //级联设置:级联类型的方法(执行MERGE和PERSIST方法时,会进行级联),抓取方式,通过mappedBy映射来找到对应的 @OneToMany(targetEntity=Employee.class,cascade={CascadeType.MERGE,CascadeType.PERSIST},fetch=FetchType.LAZY,mappedBy="depart") private Set<Employee> emps; 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; } public Set<Employee> getEmps() { return emps; } public void setEmps(Set<Employee> emps) { this.emps = emps; } } 测试:

     

static void addDepart(){ EntityManager em=null; try { em = JpaUtil.getEntityManager(); EntityTransaction tx = em.getTransaction(); tx.begin(); Department depart=new Department(); depart.setName("depart name"); Employee emp1=new Employee(); emp1.setDepart(depart); emp1.setName("emp name1"); Sales emp3=new Sales(); emp3.setDepart(depart); emp3.setName("emp name3"); emp3.setSell(100); Set<Employee> emps = new HashSet<Employee>(); emps.add(emp1); emps.add(emp3); depart.setEmps(emps); em.persist(depart); tx.commit(); } finally { if(em!=null) em.close(); } }

截图:

     代码下载: http://download.csdn.net/detail/u013030488/9835319

转载请注明原文地址: https://www.6miu.com/read-61396.html

最新回复(0)