配置继承结构

配置映射结构由三种方式:

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>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值