---
title: "Hibernate学习笔记(四)——继承关系配置&缓存"
author: "llc"
date: "2009-11-13T10:18:17Z"
tags: []
slug: "hibernate"
draft: false
lastmod: "2009-11-13T10:18:17Z"
id: 515976
url: "https://llc.iteye.com/blog/515976"
---

在平时的设计中，继承关系就随处可见的，Hibernate为我们提供了解决继承关系的数据库映射方案，以上课的例子为例：

    父类：EpetPO

          private int id;  
          private String name;

    子类一（extends EpetPO）：CatPO

          private int fishNum;

    子类二（extends EpetPO）：DogPO

          private int boneNum;

在数据库表方面，我们用一张表即可表示他们之间的关系：

      id,name,type,fishNum,boneNum

  其中type是不对应任何一个对象中的属性，是我们为了区别子类自己定义的。

  有了这样的准备工作，我们就可以开始写hbm文件了：

     &lt;class name="com.lei.hibernate.po.EpetPO" table="epet"&gt;  
         &lt;id name="id" column="id" type="int"&gt;  
             &lt;generator class="increment"&gt;&lt;/generator&gt;  
         &lt;/id&gt;  
         &lt;discriminator column="type" type="string"&gt;&lt;/discriminator&gt;  
         &lt;property name="name" column="name" type="string"&gt;&lt;/property&gt;  
         &lt;subclass name="com.lei.hibernate.po.CatPO" discriminator-value="C"&gt;  
             &lt;property name="fishNum" column="fish" type="int"&gt;&lt;/property&gt;  
         &lt;/subclass&gt;  
         &lt;subclass name="com.lei.hibernate.po.DogPO" discriminator-value="D"&gt;  
             &lt;property name="boneNum" column="bone" type="int"&gt;&lt;/property&gt;  
         &lt;/subclass&gt;  
    &lt;/class&gt;

    我们注意到与平常书写hbm文件一样，id还是有的，接下来出现了&lt;discriminator&gt;，该标签可以理解为标识，他是为了告诉Hibernate我们定义的type(本例中)字段，用该字段来区分子类。

    然后后面继续书写属性映射，在最下面我们要书写继承关系，使用&lt;subclass&gt;，name表示子类的全路径，discriminator-value="C"表示填加数据时，如果填加的是name属性中的子类，则在type字段中填入"C"，&lt;property&gt;标签与普通属性映射一样。

    这样，一个简单的继承关系的映射就写好了，在代码中使用时，我们只需要在Dao中设置传入参数为父类类型，在调用方传入子类类型Hibernate就可以自动帮我们存入数据库中，凡是存入的子类在数据库中没有的字段都以NULL代替。

    同时在获取数据库中数据时，传入HQL对象为父类则可以得到所有子类的数据，传入对象为某一子类则只得到该子类的数据，很方便。

    关于缓存：

      分两种：一级缓存和二级缓存

      其中一级缓存Hibernate已经自动帮我们配置好了，不需要我们配置，但是用处不到，只存在在一个Session生命周期中。

      二级缓存是我们需要关心的，因为基本都用他，但他需要我们自己手工配置，思维就不说了，直接说怎么配置吧：

      首先在cfg中书写：

         &lt;property name="hibernate.cache.EhCacheProvider"&gt;true&lt;/property&gt;  
         &lt;property name="cache.provider\_class"&gt;  
                   org.hibernate.cache.EhCacheProvider  
         &lt;/property&gt;  
         &lt;property name="hibernate.cache.use\_query\_cache"&gt;true&lt;/property&gt;

      在cfg同目录下建一个ehcache.xml：

         &lt;?xml version="1.0" encoding="UTF-8"?&gt;  
            &lt;ehcache&gt;  
                &lt;diskStore path="E:/cache" /&gt;  
                &lt;defaultCache maxElementsInMemory="1000" eternal="false"  
                   overflowToDisk="true" timeToIdleSeconds="180" timeToLiveSeconds="300"  
                   diskPersistent="false" diskExpiryThreadIntervalSeconds="120" /&gt;  
                &lt;cache name="userCache" maxElementsInMemory="5000" eternal="false"  
                   overflowToDisk="true" timeToIdleSeconds="18" timeToLiveSeconds="30"  
                   diskPersistent="false" diskExpiryThreadIntervalSeconds="120" /&gt;  
            &lt;/ehcache&gt;

     最后在需要缓存的hbm文件中&lt;class&gt;标签下的第1排写上：

          &lt;cache usage="read-write" /&gt;

     于是大功告成，这个配置我觉得还是不要去背了，理解下缓存的作用就好了，平时要用直接粘贴吧~~~