标签:
今日要点:
1、继承关系映射
一个表对应一个映射树(subclass)
一个子类一个表,子类只有父类没有的字段(joined-subclass)
鉴别器和内连接结合使用(subclass join)
一个类对应一个表(union-subclass)
1、j继承映射
1、一张表映射一个继承树。
Employee.hbm.xml的主要代码:
<class name="Employee" discriminator-value="0"> <!-- 鉴别属性值为0表示为普通雇员 --> <id name="id" > <generator class="native"/> </id> <discriminator column="type" type="int"/><!-- 鉴别器,缺省类型是字符串类型 ,鉴别器的位置放置在property前面,不写值的话表示属性值为类名的字符串形式--> <property name="name"/> <many-to-one name="depart" column="depart_id" cascade="save-update"/> <subclass name="Skiller" discriminator-value="1"> <!-- sub:替代品 ,表示子类 --> <property name="skill"/> </subclass> <subclass name="Sales" discriminator-value="2"> <property name="sell"/> </subclass> </class>
效率比较高
增加子类就会修改表结构。增加字段
特有字段都不能加上非空约束,应该可以为空,关系型来说不是很好的设计
package cn.itcast.domain; import java.util.Set; public class Department { private int id; private String name; private Set<Employee> employees; public Set<Employee> getEmployees() { return employees; } public void setEmployees(Set<Employee> employees) { this.employees = employees; } 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() { super(); } public Department(int id, String name) { super(); this.id = id; this.name = name; } }
package cn.itcast.domain; public class Employee { private int id; private String name; 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 Employee() { super(); } public Employee(int id, String name, Department depart) { super(); this.id = id; this.name = name; this.depart = depart; } @Override public String toString() { return "Employee [id=" + id + ", name=" + name + ", depart=" + depart + "]"; } }
package cn.itcast.domain; public class Sales extends Employee{//销售 private int sell; public void setSell(int sell) { this.sell = sell; } public int getSell() { return sell; } }
package cn.itcast.domain; public class Skiller extends Employee{//技术 private String skill; public void setSkill(String skill) { this.skill = skill; } public String getSkill() { return skill; } }
<?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="cn.itcast.domain"> <class name="Department"> <id name="id"> <generator class="native"/> </id> <property name="name"/> <set name="employees" inverse="true"> <key column="depart_id" /> <one-to-many class="Employee" /> </set> </class> </hibernate-mapping>
2、每一个子类一张表,通过外键关联
其他地方不用改变,只需要改变下employee.hbm.xml的代码(之后也是):
<class name="Employee"> <!-- 鉴别属性值为0表示为普通雇员 --> <id name="id" > <generator class="native"/> </id> <property name="name"/> <many-to-one name="depart" column="depart_id" cascade="save-update"/> <joined-subclass name="Skiller" table="skiller"><!-- join表示数据库表中使用连接与子类关联 --> <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>
表结构合理,子类和主类差距比较的的时候使用。
多态方式查询的话关联表过多效率低。不使用多态查询的话就比较优秀
3、鉴别器和内连接相结合
适用情况:雇员中销售的字段比较多但是技术的字段比较少
<class name="Employee" discriminator-value="0"> <!-- 鉴别属性值为0表示为普通雇员,注意:鉴别器的值如果不给的话,默认认为是类名的全名称 --> <id name="id" > <generator class="native"/> </id> <discriminator column="type" type="int"/> <!-- 鉴别器,缺省类型是字符串类型 ,鉴别器的位置放置在property前面--> <property name="name"/> <many-to-one name="depart" column="depart_id" cascade="save-update"/> <subclass name="Skiller" discriminator-value="1"> <property name="skill"/> </subclass> <subclass name="Sales" discriminator-value="2"> <join table="sales"> <key column="emp_id"/> <property name="sell"/> </join> </subclass> </class>
4、每一个类对应一个表,且每一个表中都具有完整信息,如果父类不是抽象的,也会有父类表
<?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="cn.itcast.domain"> <class name="Employee"><!-- discriminator-value="0" --> <!-- 鉴别属性值为0表示为普通雇员,注意:鉴别器的值如果不给的话,默认认为是类名的全名称 --> <id name="id" > <!-- <generator class="native"/>--> <!-- mysql,选取nativehibernate帮你选择的主键生成器是identity --> <generator class="hilo"/> </id> <!-- <discriminator column="type" type="int"/> --> <!-- 鉴别器,缺省类型是字符串类型 ,鉴别器的位置放置在property前面--> <property name="name"/> <many-to-one name="depart" column="depart_id" cascade="save-update"/> <!-- <subclass name="Skiller" discriminator-value="1"> <property name="skill"/> </subclass> <subclass name="Sales" discriminator-value="2"> <property name="sell"/> </subclass> --><!-- sub:替代品 ,表示子类 --> <!-- <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> --> <!-- join表示数据库表中使用连接与子类关联 --><!-- 连接使用的外键列 --> <!-- <subclass name="Skiller" discriminator-value="1"> <property name="skill"/> </subclass> <subclass name="Sales" discriminator-value="2"> <join table="sales"> <key column="emp_id"/> <property name="sell"/> </join> </subclass> --> <union-subclass name="Skiller" table="skiller"> <!-- union表示结合的意思 --> <property name="skill"/> </union-subclass> <union-subclass name="Sales" table="sales"> <!-- union表示结合的意思 --> <property name="sell"/> </union-subclass> </class> </hibernate-mapping>
注意:父类和子类表的id是不能重复的,所以我们的主键生成策略不能再时native或者identity这种自增长了。
标签:
原文地址:http://www.cnblogs.com/aigeileshei/p/5802627.html