Hibernate 继承关系映射

hibernate对类的继承关系的三种策略。以动物(Animal、Pig、Bird)为例

一、所有类的属性(字段)放入在一张表中

特点,数据库查询效率高,但粒度比较粗,会有冗余字段,配置特点是在配置文件中添加了discriminator-value标签属性。

java类
 

package test.hibernate.spring.model;

public class Animal {
	private int id;
	private String name;
	private String sex;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public Animal() {
		super();
	}
	@Override
	public String toString() {
		return "Animal [id=" + id + ", name=" + name + ", sex=" + sex + "]";
	}
	

}
package test.hibernate.spring.model;

public class Bird extends Animal {
	private int height;

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public Bird() {
		super();
	}

	@Override
	public String toString() {
		return "Bird [height=" + height + "]";
	}
	

}
package test.hibernate.spring.model;

public class Pig extends Animal {
	private int weight;

	public int getWeight() {
		return weight;
	}

	public void setWeight(int weight) {
		this.weight = weight;
	}

	public Pig() {
		super();
	}

	@Override
	public String toString() {
		return "Pig [weight=" + weight + "]";
	}
	
	

}

配置文件

<?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="test.hibernate.spring.model">
    <class name="Animal" table="t_animal" discriminator-value="animal">
        <id name="id" type="int">
            <column name="id" />
            <generator class="native" />
        </id>
        <!-- 辩别字段,必须放在id后面 -->
        <discriminator column="type" type="string"></discriminator>
        <property name="name" type="java.lang.String">
            <column name="name" />
        </property>
        <property name="sex" type="java.lang.String">
            <column name="sex" />
        </property>
      <!--  根据类插入 辩别字段中的值 -->
        <subclass name="Pig" discriminator-value="pig">
        <property name="weight"></property>
        </subclass>
        <subclass name="Bird" discriminator-value="bird">
        <property name="height"></property>
        </subclass>
    </class>
</hibernate-mapping>

测试

package test.hibernate.spring;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.MetadataSources;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import test.hibernate.spring.model.Bird;
import test.hibernate.spring.model.Pig;

public class TestSession {
	SessionFactory sessionFactory = null;
	Session session=null;
	Transaction ts=null;
	@Before
	public void beforP() {
		System.out.println("begin....");
		/* hibernate规定,所有的配置或服务,必须配置或注册到一个服务注册类中 */
		  Configuration configuration = new Configuration().configure();
		  ServiceRegistry sr=configuration.getStandardServiceRegistryBuilder().build();
		/* 从注册类中获得工厂类 */
		  sessionFactory=new MetadataSources(sr).buildMetadata().buildSessionFactory();
		/* 通过工厂类开启Session */
		  session=sessionFactory.openSession();
			/* 开启事务 */
		  ts=session.beginTransaction();
					}
	@After
	public void endP() {
		System.out.println("end....");
		/* 提交事务 */
		ts.commit();
		/* 关闭Session */
		session.close();
		/* 关闭工厂 */
		sessionFactory.close();
		}
	@Test
	public void add() {
		Pig p=new Pig();
		p.setName("ls");
		p.setSex("公");
		p.setWeight(300);
		
		Bird b=new Bird();
		b.setName("zs");
		b.setSex("母");
		b.setHeight(500);
		session.save(b);
		
	}
}

二、每个类一张表,用joined-subclass标签来配置,java类没有变化。

配置

<?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="test.hibernate.spring.model">
	<class name="Animal" table="t_animal">
		<id name="id" type="int">
			<column name="id" />
			<generator class="native" />
		</id>
		<property name="name" type="java.lang.String">
			<column name="name" />
		</property>
		<property name="sex" type="java.lang.String">
			<column name="sex" />
		</property>
		<!-- 子类单独有一张表 -->
		<joined-subclass name="Pig" table="t_pig">
			<key column="p_id"></key>
			<property name="weight" type="int" column="height"></property>
		</joined-subclass>
		<joined-subclass name="Bird" table="t_bird">
			<key column="b_id"></key>
			<property name="height" type="int" column="height"></property>
		</joined-subclass>
	</class>
</hibernate-mapping>

测试

与第一种相同
 

三、每个具体类一张表(每个子类一张表,每张表都有自己所有的属性字段,包括父类的公共字段)使用<union-subclass>标签

java类,父类的id改成String类型,子类不变
 

/**
*Description:
*author: ljd
*@date 2024年8月2日 
*@version 1.0 
*/
package test.hibernate.spring.model;

public class Animal {
	private String id;
	private String name;
	private String sex;
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public Animal() {
		super();
	}
	@Override
	public String toString() {
		return "Animal [id=" + id + ", name=" + name + ", sex=" + sex + "]";
	}
	

}

配置
 

<?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="test.hibernate.spring.model">
	<!-- 这种配置方式,t_animal表不会有任何数据插入,加入abstract="true"属性,则可以避免生成t_animal这张表 -->
	<class abstract="true" name="Animal" table="t_animal">
		<id name="id" type="string">
			<column name="id" />
			<!-- id不能再使用int类型,要使用uuid自动生成方式,使t_pig和t_bird的主键值不会相同 -->
			<generator class="uuid2" />
		</id>
		<property name="name" type="java.lang.String">
			<column name="name" />
		</property>
		<property name="sex" type="java.lang.String">
			<column name="sex" />
		</property>
		<!-- 子类单独有一张表 -->
		<union-subclass name="Pig" table="t_pig">
			<property name="weight" type="int" column="height"></property>
		</union-subclass>
		<union-subclass name="Bird" table="t_bird">
			<property name="height" type="int" column="height"></property>
		</union-subclass>
	</class>
</hibernate-mapping>

测试

与第一种相同
 

总结:

如果需要经常查询数据,且子类较多,则建议使用第一种方案,即每棵生成数映射成一张表,这也是最常用的方法,效率较高。如果追求颗粒度,且子类的数量不多,则可用后面两种方法,即每人个类映射一张表或每个具体类映射成一张表。

补充:

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值