Hibernate关联关系映射之继承映射

php中文网
发布: 2016-06-07 16:03:42
原创
1539人浏览过

首先有一个文章类(article)类中有id、title、content、posttime等属性。 package entity;import java.util.Date;public class Article {private Integer id;private String title;private String content;private Date postTime;public Integer getId() {r

首先有一个文章类(article)类中有id、title、content、posttime等属性。

package entity;
import java.util.Date;
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;
	}	
}
登录后复制

然后看他的子类,Topic类和Reply类。他们除了父类的属性外还有自己独特的属性。

package entity;

public class Topic extends Article{
	private int type;
	public int getType() {
		return type;
	}
	public void setType(int type) {
		this.type = type;
	}
}
登录后复制
package entity;

public class Reply extends Article{
	private int floor;

	public int getFloor() {
		return floor;
	}

	public void setFloor(int floor) {
		this.floor = floor;
	}
	
}
登录后复制

继承映射的方式有三种方式,我们一个个看:

方式一:将父类和子类的信息存放在同一个表中,然后在该数据表中有个字段用来表示该条记录的类型,其中子类独有的属性允许为空。我们看一下映射配置文件。

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="entity">
    <class name="entity.Article" table="article" schema="MYHR" discriminator-value="Article">
        <id name="id" type="int">
            <column name="ID" />
            <generator class="assigned" />
        </id>
        
        <!-- 用于鉴别是什么类型的一个列 -->
        <discriminator type="string" column="t_class"/>
        <property name="title"/>
        <property name="content" type="clob" length="5000"/>
        <property name="postTime" type="timestamp"/>
        <subclass name="Topic" discriminator-value="Topic">
            <property name="type" type="int"/>
        </subclass>
        <subclass name="Reply" discriminator-value="Reply">
            <property name="floor" type="int"/>
        </subclass>
    </class>
</hibernate-mapping>
登录后复制

方式二:父类和子类不在同一张表中,且每一个类一张表,抽象类对应一张表,这是配置子类使用joined-subclass

Motiff
Motiff

Motiff是由猿辅导旗下的一款界面设计工具,定位为“AI时代设计工具”

Motiff 148
查看详情 Motiff
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="entity">
	<class name="entity.Article" table="article" schema="MYHR">
		<id name="id" type="int">
			<column name="ID" />
			<generator class="assigned" />
		</id>
		<property name="title" />
		<property name="content" type="clob" length="5000" />
		<property name="postTime" type="timestamp" />

		<joined-subclass name="Topic" table="topic">
			<key column="id" />
			<property name="type" type="int" />
		</joined-subclass>
		<joined-subclass name="Reply" table="reply">
			<key column="id" />
			<property name="floor" type="int" />
		</joined-subclass>
	</class>
</hibernate-mapping>
登录后复制

方式三:每一个类单独一张表,并且抽象类不对应一张表,子类对应的数据库表中对应全部的属性,包括从父类继承的信息。

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="entity">
    <!-- 采用每个具体类一张表,抽象类不对应表,abstract默认为false,设为true表示为抽象的不对应表 -->
	<class name="entity.Article" abstract="true" schema="MYHR">
		<id name="id" type="int">
			<column name="ID" />
			<generator class="assigned" />
		</id>
		<property name="title" />
		<property name="content" type="clob" length="5000" />
		<property name="postTime" type="timestamp" />

		<union-subclass name="Topic" table="topic">
			<property name="type" type="int" />
		</union-subclass>
		<union-subclass name="Reply" table="reply">
			<property name="floor" type="int" />
		</union-subclass>
	</class>
</hibernate-mapping>
登录后复制

然后看一下测试类,三种方式测试类相同,只有方式三的save方法中因为抽象类没有单独的数据表因此不能存储他们父类的信息。

package test;

import org.hibernate.Session;
import org.hibernate.Transaction;

import entity.Article;
import entity.Reply;
import entity.Topic;
import factory.HibernateSessionFactory;

public class Test {

	private Session session = null;
	private Transaction tran = null;
	@org.junit.Test
	public void save() {
		session = HibernateSessionFactory.getSession();
		tran = session.beginTransaction();
		try{
			Article article = new Article();
			article.setId(1);
			article.setTitle("这是一个Article");
			
			Topic topic = new Topic();
			topic.setId(2);
			topic.setTitle("这是一个Topic");
			
			Reply reply = new Reply();
			reply.setId(3);
			reply.setTitle("这是一个reply");
			
			session.save(article);
			session.save(topic);
			session.save(reply);
			
			tran.commit();
		}catch(Exception e){
			tran.rollback();
		}
	}

	@org.junit.Test
	public void Get() {
		session = HibernateSessionFactory.getSession();
		tran = session.beginTransaction();
		try{
			Article a = (Article)session.get(Article.class, 1);
			Topic t = (Topic)session.get(Topic.class, 2);
			Reply r = (Reply)session.get(Reply.class, 3);
			
			System.out.println(a.getTitle());
			System.out.println(t.getTitle());
			System.out.println(r.getTitle());
			tran.commit();
		}catch(Exception e){
			tran.rollback();
		}
	}
}
登录后复制


最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号