0
点赞
收藏
分享

微信扫一扫

Hibernate_映射_继承结构映射1_使用一张表的方式

Java旺 2022-07-27 阅读 86


<?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="cn.itcast.j_hbm_extends">
<!--
discriminator-value属性:
用于鉴别是哪个类的一个值,表示这个值就是这个类。
如果不写,默认为类的全限定名。
-->
<class name="Article" table="t_article" discriminator-value="Article">
<id name="id" type="integer" column="id_">
<generator class="native" />
</id>
<!-- 用于鉴别是什么类型的一个列 -->
<discriminator type="string" column="class_"></discriminator>

<property name="title" type="string" column="title_" />
<property name="content" type="text" length="10000" column="content_"></property>
<property name="postTime" type="timestamp" column="postTime_"></property>

<!-- 子类:Topic -->
<subclass name="Topic" discriminator-value="Topic">
<property name="type" />
</subclass>

<!-- 子类:Reply -->
<subclass name="Reply" discriminator-value="Reply">
<property name="floor" />
</subclass>
</class>
</hibernate-mapping>


package cn.itcast.j_hbm_extends;

import java.util.Date;

/**
* 文章
*
* @author 风清杨
* @version V1.0
*/
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 content) {
this.content = content;
}

public Date getPostTime() {
return postTime;
}

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

}


package cn.itcast.j_hbm_extends;

/**
* 主题
*
* @author 风清杨
* @version V1.0
*/
public class Topic extends Article {
private Integer type;// 精华、置顶...

public Integer getType() {
return type;
}

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

}


package cn.itcast.j_hbm_extends;

/**
* 回贴
*
* @author 风清杨
* @version V1.0
*/
public class Reply extends Article {
private Integer floor;// 楼层

public Integer getFloor() {
return floor;
}

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

}


package cn.itcast.j_hbm_extends;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;

/**
* 应用程序操作类
*
* @author 风清杨
* @version V1.0
*
*/
public class App {
private static SessionFactory sessionFactory = new Configuration()//
.configure()//
.addClass(Article.class)//
.buildSessionFactory();

// 保存,有关联关系
@Test
public void testSave() throws Exception {
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
// ------------------------------------

// 新键对象
Article article = new Article();
article.setTitle("这是一个Article");
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) {
tx.rollback();
throw e;
} finally {
session.close();
}
}

// 获取,可以获取到关联的对方
@Test
public void testGet() throws Exception {
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
// ------------------------------------
// 获取
Article article = (Article) session.get(Article.class, 1);
Topic topic = (Topic) session.get(Topic.class, 2);
Reply reply = (Reply) session.get(Reply.class, 3);

System.out.println(article);
System.out.println(topic);
System.out.println(reply);
System.out.println();

Article article1 = (Article) session.get(Article.class, 2);
Article article2 = (Article) session.get(Article.class, 3);
System.out.println(article1);
System.out.println(article2);
// ------------------------------------
tx.commit();
} catch (RuntimeException e) {
tx.rollback();
throw e;
} finally {
session.close();
}
}

}




举报

相关推荐

0 条评论