配置People.hbm.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping>
- <class name="com.suxiaolei.hibernate.pojos.People" table="PEOPLE">
- <id name="id" type="string">
- <column name="ID"></column>
- <generator class="uuid"></generator>
- </id>
- <discriminator column="PEOPLETYPE" type="string"></discriminator>
- <property name="name" column="NAME" type="string"></property>
- <property name="sex" column="SEX" type="string"></property>
- <property name="age" column="AGE" type="string"></property>
- <property name="birthday" column="BIRTHDAY" type="timestamp"></property>
- <subclass name="com.suxiaolei.hibernate.pojos.Student" discriminator-value="student">
- <property name="cardId" column="CARDID" type="string"></property>
- </subclass>
- <subclass name="com.suxiaolei.hibernate.pojos.Teacher" discriminator-value="teacher">
- <property name="salary" column="SALARY" type="string"></property>
- </subclass>
- </class>
- </hibernate-mapping>
<discriminator>标签用于在表中创建一个标识列,其"column"属性指定标识列的列名,"type"指定了标识列的类型。<subclass>标签用于指定该HBM文件代表类的子类,有多少子类就有多少个该标签,其"name"属性指定子类的名称,"discriminator-value"属性指定该子类的数据的标识列的值是什么,其"extends"属性与<union-subclass>的"extends"属性用法一致。
根据People.hbm.xml生成表结构:
- drop table if exists PEOPLE
- create table PEOPLE (
- ID varchar(255) not null,
- PEOPLETYPE varchar(255) not null,
- NAME varchar(255),
- SEX varchar(255),
- AGE varchar(255),
- BIRTHDAY datetime,
- CARDID varchar(255),
- SALARY varchar(255),
- primary key (ID)
- )
可以看到一张表将继承体系下的所有信息都包含了,其中"PEOPLETYPE"为标识列。
