Hibernate_11_繼承實例_多表

<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>





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章