CREATE TABLE user ( id INT(11) NOT NULL auto_increment PRIMARY KEY, name VARCHAR(100) NOT NULL default '' ); CREATE TABLE defaultuser ( id INT(11) NOT NULL PRIMARY KEY, someProperty VARCHAR(100) ); CREATE TABLE poweruser ( id INT(11) NOT NULL PRIMARY KEY, otherProperty VARCHAR(100) );在映射文件上,如下定义:
<?xml version="1.0" encoding="utf-8"?><joined-subclass>指明了子类别与所对应的表格,<key column>指明子类别的对应表格中,哪一个字段要与父类别的主键一致。
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="onlyfun.caterpillar.User" table="user">
<id name="id" column="id" type="java.lang.Integer">
<generator class="native"/>
</id>
<property name="name" column="name" type="java.lang.String"/>
<joined-subclass name="onlyfun.caterpillar.DefaultUser"
table="defaultuser">
<key column="id"/>
<property name="someProperty"
column="someProperty"
type="java.lang.String"/>
</joined-subclass>
<joined-subclass name="onlyfun.caterpillar.PowerUser"
table="poweruser">
<key column="id"/>
<property name="otherProperty"
column="otherProperty"
type="java.lang.String"/>
</joined-subclass>
</class>
</hibernate-mapping>
mysql> select * from user; +-----+-------------+ | id | name | +-----+-------------+ | 1 | caterpillar | | 2 | Bush | +-----+-------------+ 2 rows in set (0.00 sec) mysql> select * from defaultuser; +----+-------------------+ | id | someProperty | +----+-------------------+ | 2 | hu....hu... | +----+------------------+ 1 row in set (0.00 sec) mysql> select * from poweruser; +----+-------------------+ | id | otherProperty | +----+-------------------+ | 1 | Bla...Bla... | +----+-------------------+ 1 row in set (0.00 sec) |