Hibernate学习笔记(十四):继承映射 之 每个子类写一个映射文件

需求:动物 和 猫、猴子;猫 继承于 动物;猴子 也继承于 动物;

项目结构:

动物实体类 Animal.java:

package com.hibernate.extend;

/**
 * 动物类:父类
 */
public class Animal {
    private int id;
    private String name;

    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;
    }
}

猫实体类 Cat.java:

package com.hibernate.extend;

/**
 * 猫
 */
public class Cat extends Animal {
    // 抓老鼠
    private String catchMouse;

    public String getCatchMouse() {
        return catchMouse;
    }

    public void setCatchMouse(String catchMouse) {
        this.catchMouse = catchMouse;
    }
}

猫映射配置 Cat.hbm.xml:

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

<!-- 简单继承映射 -->
<hibernate-mapping package="com.hibernate.extend">
	<class name="Cat" table="t_cat">
		<!-- 简单继承映射:父类的属性可以直接写 -->
		<id name="id">
			<generator class="native"/>
		</id>

		<!-- 普通字段映射 -->
		<property name="name"/>			<!-- 父类的属性 -->
		<property name="catchMouse"/>	<!-- 子类的属性 -->
	</class>
</hibernate-mapping>

猴子实体类 Monkey.java:

package com.hibernate.extend;

/**
 * 猴子实体类
 */
public class Monkey extends Animal {
    private String eatBanana; // 吃香蕉

    public String getEatBanana() {
        return eatBanana;
    }

    public void setEatBanana(String eatBanana) {
        this.eatBanana = eatBanana;
    }
}

猴子映射文件 Monkey.hbm.xml:

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

<!-- 简单继承映射 -->
<hibernate-mapping package="com.hibernate.extend">
	<class name="Monkey" table="t_monkey">
		<!-- 简单继承映射:父类的属性可以直接写 -->
		<id name="id">
			<generator class="native"/>
		</id>

		<!-- 普通字段映射 -->
		<property name="name"/>			<!-- 父类的属性 -->
		<property name="eatBanana"/>	<!-- 子类的属性 -->
	</class>
</hibernate-mapping>

主配置 hibernate.cfg.xml:

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

<hibernate-configuration>
    <session-factory>
        <property name="connection.url">jdbc:mysql://localhost:3306/hib_demo</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>

        <property name="show_sql">true</property>
        <property name="hbm2ddl.auto">update</property> <!-- 自动建表 -->
    </session-factory>
</hibernate-configuration>

测试程序 Demo.java:

package com.hibernate.extend;

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

import java.util.List;

public class Demo {

    private static SessionFactory sessionFactory;
    static {
        sessionFactory = new Configuration()
                .configure()
                .addClass(Cat.class)  // 自动加载配置文件
                .addClass(Monkey.class)
                .buildSessionFactory();
    }

    /**
     * 测试保存数据
     */
    @Test
    public  void testSave(){
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();

        // 创建 Cat 对象
        Cat cat = new Cat();
        cat.setName("大花猫");
        cat.setCatchMouse("抓老鼠");

        // 创建 Monkey 对象
        Monkey monkey = new Monkey();
        monkey.setName("大马猴");
        monkey.setEatBanana("吃香蕉");

        // 保存
        session.save(cat);
        session.save(monkey);

        transaction.commit();
        session.close();
    }

    /**
     * 测试获取数据
     */
    @Test
    public  void testGet(){
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();

        // 获取数据:通过子类获取,获取到的只是 猫类
        Query query1 = session.createQuery("from Cat");
        List list1 = query1.list();
        System.out.println(list1);

        /**
         * 获取数据:通过父类获取
         * 注意:
         *  1、通过父类获取数据 的时候必须要写 父类的全名(含包名)
         *  2、通过父类获取到的是 所有的子类对象
         */
        Query query2 = session.createQuery("from com.hibernate.extend.Animal");
        List list2 = query2.list();
        System.out.println(list2);

        transaction.commit();
        session.close();
    }
}

生成的表结构:

特点:

1、有多少个子类,写多少个映射文件;

2、映射文件中父类的属性可以直接写;

3、获取数据时,可以通过父类获取到 所有的子类对象;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值