继承映射之三类表继承

继承映射有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 支持延时加载,abstract="true"的意思是说明这个表示抽象的,在数据库生成表的时候就不需要生成该表了 -->
    <class name="Animal" table="t_animal" abstract="true">
    <!-- id生成策略如果为native的话,则会报错,应该使用uuid,如果使用uuid的话那么POJO类的ID应该为String,
    或者assigend 如使用assigend则需要在存储的时候指定ID,避免因为默认值为0报错 -->
        <!-- <id name="id">
            <generator class="native"></generator>
        </id>
        -->
        <id name="id">
        <generator class="assigned"></generator>
        </id>
        <property name="name"></property>
        <property name="sex"></property>
        <!-- 标签为join-subclass 加入Pig -->
        <union-subclass name="Pig" table="t_pig">
        <property name="weight"></property>
        </union-subclass>
        <union-subclass name="Bird" table="t_bird">
        <property name="height"></property>
        </union-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_3?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

6testExtends:3种继承映射只改变关系,不改变存储,所以存储和查询都是用的之前的test。

package com.cnblogs.hibernate_first;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.persistence.EntityTransaction;

import org.hibernate.Session;
import org.hibernate.cfg.Configuration;

import junit.framework.TestCase;

public class extensTest extends TestCase {

    public void testSave1() {
        Session session = null;
        try {
            session = HibernateUtils.getSession();
            session.beginTransaction();
            
            Pig pig1 = new Pig();
            pig1.setId(1);
            pig1.setName("小猪猪");
            pig1.setSex(true);
            pig1.setWeight(200);
            session.save(pig1);
            
            Bird bird1 = new Bird();
            bird1.setId(2);
            bird1.setName("小鸟鸟");
            bird1.setSex(false);
            bird1.setHeight(100);
            session.save(bird1);

            session.getTransaction().commit();

        } catch (Exception e) {
            e.printStackTrace();
            session.getTransaction().rollback();
        } finally {
            HibernateUtils.closeSession(session);
        }

    }
    //加载查询
    /*
     * 采用load查询,通过pig查询
     */
    public void testLoad1() {
        Session session = null;
        try {
            session = HibernateUtils.getSession();
            session.beginTransaction();
            Pig pig  = (Pig)session.load(Pig.class, 1);
            System.out.println(pig.getName());
            session.getTransaction().commit();
            
        } catch (Exception e) {
            e.printStackTrace();
            session.getTransaction().rollback();
        } finally {
            HibernateUtils.closeSession(session);
        }

    }
    
    /*
     * 采用load查询,通过Animal查询
     */
    public void testLoad2() {
        Session session = null;
        try {
            session = HibernateUtils.getSession();
            session.beginTransaction();
            Animal a  = (Animal)session.load(Animal.class, 1);
            System.out.println(a.getName());
            session.getTransaction().commit();
            
        } catch (Exception e) {
            e.printStackTrace();
            session.getTransaction().rollback();
        } finally {
            HibernateUtils.closeSession(session);
        }

    }
    /*
     * 使用load查询,不能鉴别真正类型,以下示例输出的原因是:load是延时加载,返回的是animal的一个代理对象,与pig bird同级,
     * 所以instance为否。load在这种情况下不支持多态查询。
     * 多态查询:在hibernate加载数据的时候,能够采用instanceof鉴别出其真正的类型。
     */
    public void testLoad3() {
        Session session = null;
        try {
            session = HibernateUtils.getSession();
            session.beginTransaction();
            Animal a  = (Animal)session.load(Animal.class, 1);
            if(a instanceof Pig){
                System.out.println(a.getName());
            }else{
                System.out.println("不是猪");
            }
            session.getTransaction().commit();
            
        } catch (Exception e) {
            e.printStackTrace();
            session.getTransaction().rollback();
        } finally {
            HibernateUtils.closeSession(session);
        }

    }
    
    /*
     * 采用load查询,通过animal查询,
     * 将<class>标签中的lazy设置成为false,返回的不是代理类,二十真正的类型,所以用instanceof 可以正确判断出其真正的类型,
     */
    
    public void testLoad4() {
        Session session = null;
        try {
            session = HibernateUtils.getSession();
            session.beginTransaction();
            Animal a  = (Animal)session.load(Animal.class, 1);
            if(a instanceof Pig){
                System.out.println(a.getName());
            }else{
                System.out.println("不是猪");
            }
            session.getTransaction().commit();
            
        } catch (Exception e) {
            e.printStackTrace();
            session.getTransaction().rollback();
        } finally {
            HibernateUtils.closeSession(session);
        }

    }
    
    
    /*
     * 采用get查询,通过animal查询,因为get返回的就是具体类,所以get是支持多台查询的
     */
    
    public void testLoad5() {
        Session session = null;
        try {
            session = HibernateUtils.getSession();
            session.beginTransaction();
            Animal a  = (Animal)session.get(Animal.class, 1);
            if(a instanceof Pig){
                System.out.println(a.getName());
            }else{
                System.out.println("不是猪");
            }
            session.getTransaction().commit();
            
        } catch (Exception e) {
            e.printStackTrace();
            session.getTransaction().rollback();
        } finally {
            HibernateUtils.closeSession(session);
        }

    }
    /*
     * 采用hql查询,通过animal查询.返回的是真正类型,所以hql是支持多态查询的
     */
    
    @SuppressWarnings("unchecked")
    public void testLoad6() {
        Session session = null;
        try {
            session = HibernateUtils.getSession();
            session.beginTransaction();
            List animalList = session.createQuery("from Animal").list();
            for(Iterator iter = animalList.iterator();iter.hasNext();){
                Animal a = (Animal)iter.next();
                if(a instanceof Pig){
                    System.out.println(a.getName());
                }else if(a instanceof Bird){
                    System.out.println(a.getName());
                }
            }
            session.getTransaction().commit();
            
        } catch (Exception e) {
            e.printStackTrace();
            session.getTransaction().rollback();
        } finally {
            HibernateUtils.closeSession(session);
        }

    }
    
    /*
     * 采用hql查询,通过animal查询.session.createQuery("from java.lang.Object")相当于show tables
     * 因为所有类都是object  
     */
    
    @SuppressWarnings("unchecked")
    public void testLoad7() {
        Session session = null;
        try {
            session = HibernateUtils.getSession();
            session.beginTransaction();
            List animalList = session.createQuery("from java.lang.Object").list();
            for(Iterator iter = animalList.iterator();iter.hasNext();){
                Object obj = iter.next();
                if(obj instanceof Pig){
                    System.out.println(obj);
                }else if(obj instanceof Bird){
                    System.out.println(obj);
                }
            }
            session.getTransaction().commit();
            
        } catch (Exception e) {
            e.printStackTrace();
            session.getTransaction().rollback();
        } finally {
            HibernateUtils.closeSession(session);
        }

    }
    
    public void testLoad12() {
        Session session = null;
        try {
            session = HibernateUtils.getSession();
            session.beginTransaction();
            
        } catch (Exception e) {
            e.printStackTrace();
            session.getTransaction().rollback();
        } finally {
            HibernateUtils.closeSession(session);
        }

    }
}
View Code

 

posted @ 2018-01-26 16:51  错莫难瞒  阅读(281)  评论(0编辑  收藏  举报