Hibernate_10_继承的例子_单表

时间:2021-02-05 14:45:02

 只是建一个表,所有属性都包括在此表。使用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)主文件配置略。

版权声明:本文博客原创文章,博客,未经同意,不得转载。