继承映射之二具体表映射

继承映射有3种情况:

1.单表继承:每颗继承树使用一个表 table per class hierarchy

2.具体表继承:每个子类一个表 table per subclass

3.类表继承:每个具体类一个表 table per concrete class 有部分限制,

以下,具体表继承:

1.Animal类:

package com.cnblogs.hibernate_first;

public class Animal {
    
    private int id;
    private String name;
    private boolean 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 boolean isSex() {
        return sex;
    }

    public void setSex(boolean sex) {
        this.sex = sex;
    }

}
View Code

2.Bird类:

package com.cnblogs.hibernate_first;

public class Bird extends Animal {
    
    private int height;

    public int getHeight() {
        return height;
    }

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

3.Pig类

package com.cnblogs.hibernate_first;

public class Pig extends Animal {
    
    private int weight;

    public int getWeight() {
        return weight;
    }

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

4.extends.hbm.xml

<?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="com.cnblogs.hibernate_first">

<!-- class的lazy属性,不设置默认为ture 支持延时加载, -->
    <class name="Animal" table="t_animal">
        <id name="id">    
            <generator class="native"></generator>
        </id>
        <property name="name"></property>
        <property name="sex"></property>
        <!-- 标签为join-subclass 加入Pig -->
        <joined-subclass name="Pig" table="t_pig">
        <!-- 其中t_pig表中加入一个字段pig 和 t_animal 中的ID对应 -->
            <key column="pid"></key>
            <property name="weight"></property>
        </joined-subclass>
        <!-- 标签为join-subclass 加入Bird -->
        <joined-subclass name="Bird" table="t_bird">
        <!-- 其中t_pig表中加入一个字段big 和 t_animal 中的ID对应 -->
        <key column="bid"></key>
        <property name="height"></property>
        </joined-subclass>    
    </class>    
</hibernate-mapping>
View Code

5.hibernate.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<!-- CREATE DATABASE hibernate_extends_1 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci -->
    <session-factory>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/hibernate_extends_2?useUnicode=true&amp;characterEncoding=UTF8</property>
        <property name="connection.username">root</property>
        <property name="connection.password"></property>
        <!-- 每次从数据库中取出并放到JDBC的Statement中的记录条数。Fetch Size设的越大,读数据库的次数越少,速度越快,Fetch 
            Size越小,读数据库的次数越多,速度越慢 -->
        <property name="jdbc.fetch_size">50 </property>
        <!--批量插入,删除和更新时每次操作的记录数。Batch Size越大,批量操作的向数据库发送Sql的次数越少,速度就越快,同样耗用内存就越大 -->
        <property name="jdbc.batch_size">23 </property>
        <!-- SQL 方言 -->
        <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>
        <!-- 在控制台输出sql语句 -->
        <property name="show_sql">true</property>
        <!-- 在启动时根据配置更新数据库 -->
        <property name="hbm2ddl.auto">update</property>
        <mapping resource="com/cnblogs/hibernate_first/Extends.hbm.xml" /><!-- 注册我们的实体映射类 -->
    </session-factory>
</hibernate-configuration>
View Code

转载于:https://www.cnblogs.com/Juli/p/8360328.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值