Hibernate_映射_继承结构映射2_另外两种映射方式
2017-05-10 12:46
381 查看
<?xml version="1.0"?> 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(); } } }
方式1
<?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_extends2">
<!-- 采用每个类一张表的方式,抽象类也对应表 -->
<class name="Article" table="t_article">
<id name="id" type="integer" column="id_">
<generator class="native" />
</id>
<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 -->
<joined-subclass name="Topic" table="t_topic">
<key column="articleId"></key>
<property name="type" type="integer" column="type_"/>
</joined-subclass>
<!-- 子类:Reply -->
<joined-subclass name="Reply" table="t_reply">
<key column="articleId"></key>
<property name="floor" type="integer" column="floor_"/>
</joined-subclass>
</class>
</hibernate-mapping>
方式2
<?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_extends3">
<!-- 采用每个具体类一张表的方式,抽象类不对应表
abstract默认为false,设为true表示本类不对应表(类可以不是abstract的),这时就会忽略table属性。
-->
<class name="Article" abstract="false">
<id name="id" type="integer" column="id_">
<!--
当使用每个具体类一张表的方式时,主键生成策略不能是identity。
因为在整个继承结构中,主键值是不能重复的。
-->
<generator class="hilo">
<param name="table">hi_value</param>
<param name="column">next_value</param>
<param name="max_lo">100</param>
</generator>
</id>
<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 -->
<union-subclass name="Topic" table="t_topic">
<property name="type" type="integer" column="type_"/>
</union-subclass>
<!-- 子类:Reply -->
<union-subclass name="Reply" table="t_reply">
<property name="floor" type="integer" column="floor_"/>
</union-subclass>
</class>
</hibernate-mapping>
相关文章推荐
- Hibernate_映射_继承结构映射1_使用一张表的方式
- Hibernate学习笔记:继承映射之每个类继承结构一张表
- Hibernate继承映射方式一
- hibernate 一对一映射的两种方式
- Hibernate之关系映射的两种方式(xml映射、annotation注解映射)
- Hibernate 简单使用(七)继承映射的几种方式
- hibernate中映射文件的两种书写方式
- 4、Hibernate 实体关系映射(一、单边一对多关系 注解、xml两种方式)
- hibernate笔记--继承映射关系的三种实现方式
- hibernate继承结构映射
- Hibernate三种继承映射方式的比较
- Hibernate继承映射:每个子类一张表(joined-subclass)表结构
- Hibernate映射类继承之每个类层次结构一张表(父子类共用一张表,一个映射文件)
- hibernate映射视图的两种方式
- Hibernate映射集合属性的两种方式(非关联映射)--基础
- Hibernate继承映射的三种方式
- hibernate继承映射[映射单独一个tablexml版本]实例【与19相同,方式不同,未验证】(二十)
- Hibernate继承映射之使用多子表方式实例
- Hibernate两种方式进行映射配置
- 框架 day33 Hibernate,组件映射,继承映射,抓取(检索)策略-优化,检索方式总结