Hibernate_10_继承实例_单表

来源:互联网 发布:windows 7模拟器手机版 编辑:程序博客网 时间:2024/06/03 22:52

 只建立一张表,所有的属性都包含在这张表中。用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;}@Overridepublic 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;}@Overridepublic 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;}@Overridepublic 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 方法 */@Testpublic 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方法 */@Testpublic 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)主文件配置略。

 







3 0
原创粉丝点击