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

简介: <h4 class="cjk"><span style="font-size:18px"><span style="font-family:Cambria,serif">1</span>、<span style="font-family:Cambria,serif"><span style="font-family:Courier New,serif">discriminator</spa

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鉴别器的一样。


目录
相关文章
|
8天前
|
云安全 监控 安全
|
13天前
|
机器学习/深度学习 人工智能 自然语言处理
Z-Image:冲击体验上限的下一代图像生成模型
通义实验室推出全新文生图模型Z-Image,以6B参数实现“快、稳、轻、准”突破。Turbo版本仅需8步亚秒级生成,支持16GB显存设备,中英双语理解与文字渲染尤为出色,真实感和美学表现媲美国际顶尖模型,被誉为“最值得关注的开源生图模型之一”。
1460 8
|
7天前
|
人工智能 安全 前端开发
AgentScope Java v1.0 发布,让 Java 开发者轻松构建企业级 Agentic 应用
AgentScope 重磅发布 Java 版本,拥抱企业开发主流技术栈。
486 11
|
19天前
|
人工智能 Java API
Java 正式进入 Agentic AI 时代:Spring AI Alibaba 1.1 发布背后的技术演进
Spring AI Alibaba 1.1 正式发布,提供极简方式构建企业级AI智能体。基于ReactAgent核心,支持多智能体协作、上下文工程与生产级管控,助力开发者快速打造可靠、可扩展的智能应用。
1267 43
|
19天前
|
人工智能 前端开发 算法
大厂CIO独家分享:AI如何重塑开发者未来十年
在 AI 时代,若你还在紧盯代码量、执着于全栈工程师的招聘,或者仅凭技术贡献率来评判价值,执着于业务提效的比例而忽略产研价值,你很可能已经被所谓的“常识”困住了脚步。
1170 88
大厂CIO独家分享:AI如何重塑开发者未来十年
|
2天前
|
存储 弹性计算 容灾
阿里云服务器ECS自定义购买流程:超详细新手入门教程
本文详细介绍阿里云服务器ECS自定义购买全流程,涵盖付费模式、地域选择、网络配置、实例规格、镜像系统、存储、公网IP、带宽计费及安全组设置等关键步骤,适合新手入门参考,助你轻松完成云服务器选购与部署。
196 121