第九章 关系映射 继承关联映射

1discriminator鉴别器

同一张表中表示不同的类型


employee表中定义一个type字段,表示员工的类型

name是所有的员工都有的属性。

sale_count是销售员特有的属性。

skiller是技术人员特有的属性。



三个实体类:

Employee:

package cn.framelife.mvc.entity;

import java.io.Serializable;

public class Employee implements Serializable {
	private Integer id;
	private String name;

	public Employee() {
	}

	public Integer getId() {
		return id;
	}

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

	public String getName() {
		return name;
	}

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

Sales:

package cn.framelife.mvc.entity;

public class Sales extends Employee {
	private Integer saleCount;

	public Integer getSaleCount() {
		return saleCount;
	}

	public void setSaleCount(Integer saleCount) {
		this.saleCount = saleCount;
	}

}

Skiller:

package cn.framelife.mvc.entity;

public class Skiller extends Employee {
	private String skiller;

	public String getSkiller() {
		return skiller;
	}

	public void setSkiller(String skiller) {
		this.skiller = skiller;
	}

}



Employee.hbm.xml:

<hibernate-mapping>
	<!-- discriminator-value="0"鉴别器父类Employee默认的类型为0 -->
    <class name="cn.framelife.hibernate.entity.Employee" table="employee" catalog="hibernate" discriminator-value="0">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="native" />
        </id>
        <!-- 配置鉴别器,配置表中的类型字段 -->
        <discriminator column="type"></discriminator>
        <property name="name" type="java.lang.String">
            <column name="name" length="45" not-null="true" />
        </property>
        <!-- 配置Employee的子类,并且设定类型的值 discriminator-value-->
        <subclass name="cn.framelife.hibernate.entity.Sales" discriminator-value="1">
        	<property name="saleCount" column="sale_count"></property>
        </subclass>
        <subclass name="cn.framelife.hibernate.entity.Skiller" discriminator-value="2">
        	<property name="skiller" column="skiller"></property>
        </subclass>
    </class>
    
</hibernate-mapping>



增加操作:

 tx = session.beginTransaction();
			//1、增加一个Employee
//			Employee employee = new Employee();
//			employee.setName("a");
//			session.save(employee);
			
			//2、增加一个Skiller
//			Skiller skiller = new Skiller();
//			skiller.setName("b");
//			skiller.setSkiller("kill somebody");
//			session.save(skiller);
			
			//3、增加一个Sales
			Sales sales = new Sales();
			sales.setName("c");
			sales.setSaleCount(100);
			session.save(sales);
			tx.commit();





2joined-subclass内连接

一张表表示一种类型







实体类与discriminator鉴别器的一样。



Employee.hbm.xml:


<hibernate-mapping>
	<class name="cn.framelife.hibernate.entity.Employee" table="employee"
		catalog="hibernate">
		<id name="id" type="java.lang.Integer">
			<column name="id" />
			<generator class="native" />
		</id>

		<property name="name" type="java.lang.String">
			<column name="name" length="45" not-null="true" />
		</property>
		
		<!-- 这里是关键配置 -->
		<joined-subclass name="cn.framelife.hibernate.entity.Skiller"
			table="skiller">
			<key column="id"></key>
			<property name="skiller" column="skiller"></property>
		</joined-subclass>
		<joined-subclass name="cn.framelife.hibernate.entity.Sales"
			table="sales">
			<key column="id"></key>
			<property name="saleCount" column="sale_count"></property>
		</joined-subclass>
	</class>
</hibernate-mapping>



增加操作与discriminator鉴别器的一样。



3union-subclass
这种方法在表设计的时候不需要employee表,只需要子表。





实体类与discriminator鉴别器的一样。



Employee.hbm.xml:

<class name="cn.framelife.hibernate.entity.Employee"		catalog="hibernate">
		<!-- 这里的id生成策略不能使用数据库自增长(indentity,或者是像native,如果你使用的是数据库自增长的话).可以使用hilo或者increment -->
		<id name="id" type="java.lang.Integer">
			<column name="id" />
			<generator class="increment" />
		</id>

		<property name="name" type="java.lang.String">
			<column name="name" length="45" not-null="true" />
		</property>
		
		<!-- 这里是关键配置 -->
		<union-subclass name="cn.framelife.hibernate.entity.Skiller" table="skiller">
			<property name="skiller" column="skiller"></property>
		</union-subclass>
		<union-subclass name="cn.framelife.hibernate.entity.Sales" table="sales">
			<property name="saleCount" column="sale_count"></property>
		</union-subclass>
	</class>

</hibernate-mapping>


增加操作与discriminator鉴别器的一样。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值