Hibernate_10_继承实例_单表

 只建立一张表,所有的属性都包含在这张表中。用discriminator 来区分父类和子类。

1)父类(Article):

public class Article {
	private Integer id;
	private String title;
	private String content;
	private Date postTime;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String string) {
		this.content = string;
	}

	public Date getPostTime() {
		return postTime;
	}

	public void setPostTime(Date postTime) {
		this.postTime = postTime;
	}

	@Override
	public String toString() {
		return "Article [id=" + id + ", title=" + title + ", content="
				+ content + ", postTime=" + postTime + "]";
	}

}

2)子类(Topic):

public class Topic extends Article {
	private Integer type;

	public Integer getType() {
		return type;
	}

	public void setType(Integer type) {
		this.type = type;
	}

	@Override
	public String toString() {
		return "Topic [id=" + getId() + ", title=" + getTitle() + 
 ", content=" + getContent() + ", postTime=" + 
 getPostTime() + ", type=" + type + "]";
	}
}

3)子类(Reply):

public class Reply extends Article {
	private Integer floor;

	public Integer getFloor() {
		return floor;
	}

	public void setFloor(Integer floor) {
		this.floor = floor;
	}

	@Override
	public String toString() {
		return "Reply [id=" + getId() + ", title=" + getTitle() + 
 <span style="white-space:pre">		</span>", content=" + getContent() + ", postTime=" + 	 
<span style="white-space:pre">		</span> getPostTime() + ", floor=" + floor + "]";
	}
}

4)持久化层:

package extends_1;

import java.sql.Date;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;

public class ExtendsDao {

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

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

			// 新建对象并设置属性
			Article article = new Article();
			article.setTitle("article");
			article.setContent("你是我的玫瑰!");
			article.setPostTime(new Date(20140731));

			Topic topic = new Topic();
			topic.setTitle("topic");

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

			// 保存对象
			session.save(article);
			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 article = (Article) session.get(Article.class, 1);
			System.out.println(article);

			Article topic = (Article) session.get(Topic.class, 2);
			System.out.println(topic);

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

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

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

5)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_1">
	
	<!-- 
		discriminator-value属性:
		用于鉴别是哪个类的一个值,表示这个值就是这个类。
		如果不写,默认为类的全限定名。
	 -->
<class name="Article" table="article discriminator-value="Article">
		<id name="id">
        	<generator class="native"/>
		</id>
		
		<!-- 用于鉴别是什么类型的一个类 -->
		<discriminator type="string" column="class_">
   </discriminator>
		
		<property name="title"/>
		<property name="content" type="text" length="10000"/>
		<property name="postTime" type="timestamp"/>
		
		
		<!-- 子类:Topic -->
		<subclass name="Topic" discriminator-value="Topic">
			<property name="type"></property>
		</subclass>
		
		
		<!-- 子类:Reply -->
		<subclass name="Reply" discriminator-value="Reply">
			<property name="floor"></property>
		</subclass>
		
	</class>
	
</hibernate-mapping>
6)主文件配置略。

 







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值