配置映射结构由三种方式:
1.整个继承结构使用一张表 <subclass...>
<?xml version="1.0" encoding="gbk"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!--
This mapping demonstrates
(1) a table-per-subclass mapping strategy
(2) a simple component mapping
(3) recursive associations withing an inheritance tree
-->
<hibernate-mapping package="demoextends">
<class name="Article" table="t_article" discriminator-value="Article">
<id name="id" >
<generator class="native"> <!--表示自动增长-->
</generator>
</id>
<!--用于鉴别是什么哪个类的列 鉴别值-->
<discriminator type="string" column="class_"></discriminator>
<property name="title" type="string" />
<property name="content" type="text" length="10000" />
<property name="posttime" type="timestamp" />
<subclass name="Topic" discriminator-value="Topic">
<property name="type"> </property>
</subclass>
<subclass name="Reply" discriminator-value="Reply">
<property name="floor" > </property>
</subclass>
</class>
</hibernate-mapping>
2.每个类对应一张表,抽象类也对应表,每个表中只有当前类的属性。<joined-subclass....>
<?xml version="1.0" encoding="gbk"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!--
This mapping demonstrates
(1) a table-per-subclass mapping strategy
(2) a simple component mapping
(3) recursive associations withing an inheritance tree
-->
<hibernate-mapping package="extends2">
<class name="Article" table="t_article" >
<!--采用每个类对应一张表的方式-->
<id name="id" >
<generator class="native"> <!--表示自动增长-->
</generator>
</id>
<property name="title" type="string" />
<property name="content" type="text" length="10000" />
<property name="posttime" type="timestamp" />
<joined-subclass name="Topic" table="t_topic">
<key column="id"></key>
<property name="type"> </property>
</joined-subclass>
<joined-subclass name="Reply" table="t_reply">
<key column="id"></key>
<property name="floor" > </property>
</joined-subclass>
</class>
</hibernate-mapping>
3.每个具体类对应一张表,抽象类不对应表,每个类中都有全部的信息,包括继承过来的信息。<union-subclass..>
<?xml version="1.0" encoding="gbk"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!--
This mapping demonstrates
(1) a table-per-subclass mapping strategy
(2) a simple component mapping
(3) recursive associations withing an inheritance tree
-->
<hibernate-mapping package="extends3">
<!--采用每个具体类对应一张表的方式,抽象类不对应表。
abstract默认为false,设为true,表示不对应表。
会忽略table属性
-->
<class name="Article" abstract="true">
<id name="id" >
<!--采用每个具体类对应一张表的方式,不能设置主键生成侧率为native
因为在整个继承结构中,主键值是不能重复的。
-->
<generator class="hilo">
<param name="table">hi_value</param>
<param name="column">next_value</param>
<param name="max_lo">100</param>
</generator>
</id>
<property name="title" type="string" />
<property name="content" type="text" length="10000" />
<property name="posttime" type="timestamp" />
<union-subclass name="Topic" table="t_topic3">
<property name="type"> </property>
</union-subclass>
<union-subclass name="Reply" table="t_reply3">
<property name="floor" > </property>
</union-subclass>
</class>
</hibernate-mapping>