整个继承树映射到一张表_每个类映射到一张表_鉴别器与内连接相结合(混合)_每个具体类映射一张独立表
整个继承树映射到一张表
描述:
在父类的配置文件中
1、添加<discriminator column="字段名字" type="字段类型" />鉴别器标签
column="type"给Hibernate使用,type属性设置column的类型,默认String
2、配置子类
<subclass name="Skiller" discriminator-value="1"> name="类名" discriminator-value="类对应的鉴别字段的数值"
<property name="skill" />
</subclass>
3、父类也要添加鉴别器值
<class name="Employee" discriminator-value="0">
表结构
实例代码
Sales.java继承自Employee
package com.dwt1220; public class Sales extends Employee { private String sell; public String getSell() { return sell; } public void setSell(String sell) { this.sell = sell; } }
Skiller.java继承自Employee
package com.dwt1220; public class Skiller extends Employee{ private String skill; public String getSkill() { return skill; } public void setSkill(String skill) { this.skill = skill; } }
Employee.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.dwt1220"> <class name="Employee" discriminator-value="0"> <id name="id"> <generator class="native" /> </id> <!--鉴别器column="type"给Hibernate使用,type属性默认String --> <discriminator column="type" type="int" /> <property name="name" /> <many-to-one name="department" column="depart_id" /> <subclass name="Skiller" discriminator-value="1"> <property name="skill" /> </subclass> <subclass name="Sales" discriminator-value="2"> <property name="sell" /> </subclass> </class> </hibernate-mapping>
Test.java
package com.dwt1220; import java.util.HashSet; import java.util.Set; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.Transaction; public class Test { public static void main(String[] args) { Employee employee1 = new Employee(); employee1.setName("employee1_D1"); Employee employee2 = new Employee(); employee2.setName("employee2_D1"); /********* Employee子类 *************/ Sales sales = new Sales(); sales.setName("sales01"); sales.setSell("sales_sub"); Skiller skiller = new Skiller(); skiller.setName("skiller01"); skiller.setSkill("skill_sub"); /**********************************/ Department department1 = new Department(); department1.setName("Department1"); add(department1); Set<Employee> employees = new HashSet<Employee>(); employees.add(employee1); employees.add(employee2); /********* 添加子类 ************/ employees.add(sales); employees.add(skiller); /**********************************/ department1.setEmployee(employees); add(employee1); add(employee2); /********* 添加子类到数据库 ************/ add(sales); add(skiller); /**********************************/ getEmployee(2); } static void add(Object object) { Session session = null; Transaction tx = null; try { session = HibernateUtil.getSession(); tx = session.beginTransaction(); session.save(object); tx.commit(); } catch (HibernateException e) { if (tx != null) { tx.rollback(); } throw e; } finally { if (session != null) { session.close(); } } } static Department getDepartment(int id) { Session session = null; Department department = null; try { session = HibernateUtil.getSession(); department = (Department) session.get(Department.class, id); /** 懒加载,所以要在Session关闭前使用。 */ System.out.println("department:id=" + department.getId() + " department:name=" + department.getName()); Set<Employee> employeeSet = (Set<Employee>) department .getEmployee(); for (Employee employee : employeeSet) { System.out.println("employee:id=" + employee.getId() + " employee:name=" + employee.getName()); } } finally { if (session != null) { session.close(); } } return department; } static Employee getEmployee(int id) { Session session = null; Employee employee = null; try { session = HibernateUtil.getSession(); employee = (Employee) session.get(Employee.class, id); /** 懒加载,所以要在Session关闭前使用。 */ System.out.println(employee); } finally { if (session != null) { session.close(); } } return employee; } }
每个类映射到一张表(只需修改配置文件)
多态查找时,需要查找所有子类表。效率低。查询时最好,直接指定子类的类型,尽量不要用多态超找
描述
在父类配置文件中,添加子类信息
<joined-subclass name="Skiller" table="skiller">
<key column="emp_id" /> emp_id,字段名,与父类的主键值相同。可以随意设置,hibernate管理
<property name="skill" />
</joined-subclass>
Employee.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.dwt1220"> <class name="Employee"> <id name="id"> <generator class="native" /> </id> <property name="name" /> <many-to-one name="department" column="depart_id" /> <joined-subclass name="Skiller" table="skiller"> <key column="emp_id" /> <property name="skill" /> </joined-subclass> <joined-subclass name="Sales" table="sales"> <key column="emp_id" /> <property name="sell" /> </joined-subclass> </class> </hibernate-mapping>
鉴别器与内连接相结合
※先把相关的表删除,不然可能出错
子类和父类公用一张表设置
1、添加<discriminator column="字段名字" type="字段类型" />鉴别器标签
column="type"给Hibernate使用,type属性设置column的类型,默认String
2、配置子类
<subclass name="Skiller" discriminator-value="1"> name="类名" discriminator-value="类对应的鉴别字段的数值"
<property name="skill" />
</subclass>
3、父类也要添加鉴别器值(混合)
<class name="Employee" discriminator-value="0">
单独表设置
<!--discriminator-value="2",不设置,默认值为表名,类型字符串 -->
<subclass name="Sales" discriminator-value="2">
<join table="sales">
<key column="emp_id" />
<property name="sell" />
</join>
</subclass>
Employee.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.dwt1220"> <class name="Employee" discriminator-value="0"> <id name="id"> <generator class="native" /> </id> <discriminator column="type" type="int" /> <property name="name" /> <many-to-one name="department" column="depart_id" /> <subclass name="Skiller" discriminator-value="1"> <property name="skill" /> </subclass> <!--discriminator-value="2",不设置,默认值为表名,类型字符串 --> <subclass name="Sales" discriminator-value="2"> <join table="sales"> <key column="emp_id" /> <property name="sell" /> </join> </subclass> </class> </hibernate-mapping>
每个具体类映射一张独立表
1、父类的id生成方式需要修改 <generator class="hilo" />
2、配置子类
<union-subclass name="Skiller" table="skiller">
<property name="skill"></property>
</union-subclass>
Employee.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.dwt1220"> <class name="Employee" > <id name="id"> <generator class="hilo" /> </id> <property name="name" /> <many-to-one name="department" column="depart_id" /> <union-subclass name="Skiller" table="skiller"> <property name="skill"></property> </union-subclass> <union-subclass name="Sales" table="sales"> <property name="sell"></property> </union-subclass> </class> </hibernate-mapping>