Hibernate:类继承的实现方式(三)--鉴别器与内连接相结合

当子类中有的有很多自己特有的属性,而部分子类有较少的自己的属性。这时候应该将两种方式结合起来。

domain对象和测试代码同(一)

映射文件如下:

<?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.hibernate.inherit3">

	<class name="Employee" table="employee" discriminator-value="0">
		<id name="id" unsaved-value="-1">
			<generator class="native" />
		</id>
		<discriminator column="type" type="string"/>
		<property name="name"/>

		<subclass name="Skiller" discriminator-value="1">
			<property name="skill"/>
		</subclass>
		
		<subclass name="Driver" discriminator-value="2">
			<join table="dirver">
				<key column="d_id"/>
				<property name="driverYears"/>
			</join>
		</subclass>
		
	</class>
</hibernate-mapping>



执行的sql语句:

Hibernate: insert into employee (name, type) values (?, '0')
Hibernate: insert into employee (name, skill, type) values (?, ?, '1')
Hibernate: insert into employee (name, type) values (?, '2')
Hibernate: insert into dirver (driverYears, d_id) values (?, ?)



通过sql语句可以看到,hibernate自动检测到插入的对象的存放格式,就是利用映射文件中的鉴别器discriminator的值,而且插入时会自动检测到应该插入的鉴别器的值



你可能感兴趣的:(java,sql,Hibernate,.net,xml)