码迷,mamicode.com
首页 > 系统相关 > 详细

Hibernate_11_继承实例_多表

时间:2014-08-07 13:11:20      阅读:285      评论:0      收藏:0      [点我收藏+]

标签:hibernate   java   session   实例   继承   

<1>每个类都建立一张表,抽象类也建立一张表,各张表中只包含自  己的属性,子类继承的属性不用在子类中显示。

   父类 Article,子类Topic 、Reply 、Session生成类、持久化层 、主文件配置 (与10中相同)

Article.hmb.xml文件的配置:
<?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="extends_2">

	<!--每个类对应一张表,抽象类也对应一张表,
		每个表只含有本类所对应的属性 
		每个类中都对应一个表名
		每个子类都有外键约束
	-->
	<class name="Article" table="article">
		<id name="id">
			<generator class="native" />
		</id>

		<property name="title" />
		<property name="content" type="text" length="10000" />
		<property name="postTime" type="timestamp" />


		<!-- 子类:Topic -->
		<joined-subclass name="Topic" table="topic">
			<key column="t_id"></key>
			<property name="type"></property>
		</joined-subclass>


		<!-- 子类:Reply -->
		<joined-subclass name="Reply" table="reply">
			<key column="r_id"></key>
			<property name="floor"></property>
		</joined-subclass>

	</class>

</hibernate-mapping>

<2>每个实体类都对应一张表,抽象类不建立表,各张表中包含自  己所有的属性。

  父类 Article,子类Topic 、Reply 、Session生成类、

  主文件配置 (与14中相同)

持久化层的代码:

public class ExtendsDao {

	/**
	 * save 方法
	 */
	@Test
	public void testSave() {
		Session session = SessionFactoryTools.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();

			// ==========================================

			// 新建对象并设置属性
			Topic topic = new Topic();
			topic.setTitle("topic");

			Reply reply = new Reply();
			reply.setTitle("reply");

			// 保存对象
			session.save(topic);
			session.save(reply);

			// ============================================

			tx.commit();
		} catch (RuntimeException e) {
			if (tx != null) {
				tx.rollback();
			}
			throw e;
		} finally {
			session.close();
		}
	}

	/**
	 * getById方法
	 */
	@Test
	public void testGetById() {
		Session session = SessionFactoryTools.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
 
			// ========================================================
			
			// 读取数据并输出
			Article topic = (Article) session.get(Topic.class, 0);
			System.out.println(topic);

			Article reply = (Article) session.get(Reply.class, 1);
			System.out.println(reply);

			// ==========================================================

			tx.commit();
		} catch (RuntimeException e) {
			if (tx != null) {
				tx.rollback();
			}
			throw e;
		} finally {
			session.close();
		}
	}
}

Article.hbm.xml 文件的配置:

<?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="extends_3">

	<!--每个实体类对应一张表,抽象类不对应表,每个表含有本类所对应全部的属性 
	-->
	<class name="Article" abstract="true">
		<id name="id">
			<!-- 当每个实例都有一张表时,主键生成策略不能使用indentity,
			     因为在整个集成结构中,主键值是不能重复的。
			 -->
			<generator class="hilo" >
				<param name="table">hilo_value</param>
				<param name="column">next_value</param>
				<param name="max_lo">0</param>
			</generator>
		</id>

		<property name="title" />
		<property name="content" type="text" length="10000" />
		<property name="postTime" type="timestamp" />


		<!-- 子类:Topic -->
		<union-subclass name="Topic" table="topic">
			<property name="type"></property>
		</union-subclass>


		<!-- 子类:Reply -->
		<union-subclass name="Reply" table="reply">
			<property name="floor"></property>
		</union-subclass>

	</class>

</hibernate-mapping>





Hibernate_11_继承实例_多表,布布扣,bubuko.com

Hibernate_11_继承实例_多表

标签:hibernate   java   session   实例   继承   

原文地址:http://blog.csdn.net/u012963457/article/details/38415649

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!