Hibernate

来源:互联网 发布:淘宝设置粉丝福利购 编辑:程序博客网 时间:2024/06/17 22:32

1  hibernate的update()方法:

                 (1)  对于update方法,在hbm.xml文件之中,class元素中的select-before-update属性,如果为true,则在执行update方法的时候,会在该方法执行之前执行一个select语句,与session中的被加载的持久化对象做对比,如果没有更改该持久化对象,则不执行被组装的update语句,即使update方法被调用,反之,则在执行完select语句之后,还要执行在调用update方法之后所的到的被组装的update语句, update.

                (2)  当无论是在对对象的属性进行更改在update之前还是之后,最终的组装的update语句,只与最后一次的所更改之后的持久化对象属性值为准.

                (3)  session缓存中的之前被加载的持久化对象,如果该对象的属性值没有被改变,则不执行update语句(事实上session是从一级缓存之中直接加载的持久化对象,并没有去操作数据库),一旦发生变化,就将执行组装好的update语句


2  对于Hibernate二级缓存的配置:

        (1)    首先引入第三方插件,插件的jar包为:hibernate-ehcache-5.2.3.Final.jar         slf4j-api-1.7.7.jar    ehcache-2.10.1.jar包三个包,该三个包在Hiberante压缩文件中可以找到。将其直接引入到项目的lib文件夹下。

         (2)  配置ehcache.xml文件  此文件在Hibernate压缩包中的etc目录之下,可以将其直接复制,添加到src的根目录之下,在这里我们只采用默认的缓存配置

<ehcache>
    <!-- Sets the path to the directory where cache .data files are created.
         If the path is a Java System Property it is replaced by
         its value in the running VM.
         The following properties are translated:
         user.home - User's home directory
         user.dir - User's current working directory
         java.io.tmpdir - Default temp file path -->
    <diskStore path="D:/hello"/>
    <!--Default Cache configuration. These will applied to caches programmatically created through
        the CacheManager.
        The following attributes are required for defaultCache:
        maxInMemory               - Sets the maximum number of objects that will be created in memory
        eternal                           - Sets whether elements are eternal. If eternal,  timeouts are ignored and the element  is never expired.
        timeToIdleSeconds    -Sets the time to idle for an element before it expires. Is only used            if the element is not eternal. Idle time is now - last accessed time
        timeToLiveSeconds   -Sets the time to live for an element before it expires. Is only used         if the element is not eternal. TTL is now - creation time
        overflowToDisk    - Sets whether elements can overflow to disk when the in-memory cache has reached the maxInMemory limit.
        -->
    <defaultCache
        maxElementsInMemory="10000"           //缓存中最大的缓存的对象数
        eternal="false"                                          //设置缓存对象是否是持久有效的的,如果为true则timeOut则将失去作用
        timeToIdleSeconds="120"                    //设置缓存对象在失效之前可以闲置的最大时间
        timeToLiveSeconds="120"                  //设置缓存对象在失效之前可以存在的最大时间
        overflowToDisk="true"                         //设置当缓存中缓存对象的个数达到最大的个数时,是否可以溢出到磁盘之中或其他指定的文件之中
        />
</ehcache>     

备注:  timeToldleSeconds=x      自缓存创建时间起,最后一次访问缓存的时间至缓存失效时间之间的间隔x

           timeToLiveSeconds=y     自缓存创建时间起,到缓存失效的时间之间的间隔y

  例如:timeToldeSeconds="200"        timeToLiveSeconds="300"

           表示该缓存对象最多可以存活300秒,如果超过200秒没有访问该缓存对象,则该缓存对象将失效,即在这300秒的时间内,如果间隔了200秒才访问该缓存,则该缓存将失效,但总的时间超过了300秒之后,该缓存也将失效。

        (3)   配置Hibernate.cfg.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>  
<session-factory>  
         <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>  
         <property name="hibernate.connection.url">jdbc:mysql://localhost/example</property>  
         <property name="hibernate.connection.username">root</property>  
         <property name="hibernate.connection.password">120110</property>  
         <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
         <property name="connection.characterEncoding">utf-8</property>   
         <property name="show_sql">true</property>
         <property name="hibernate.cache.use_second_level_cache">true</property>    //启用二级缓存
         <!--<property name="hibernate.cache.region.factory_class">org.hibernate.cache.SingletonEhCacheRegionFactory</property>  -->   //第二种方式指明二级缓存的提供商
         <property name="cache.region.factory_class">org.hibernate.cache.EhCacheRegionFactory</property>      //第一种方式指明二级缓存的提供商
         <mapping resource="HibernatePackage/User.hbm.xml" />
         <class-cache class="HibernatePackage.User" usage="read-write"/>
 </session-factory>  
</hibernate-configuration>  

          (4)配置User.hb.xml文件

              <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
          <class name="HibernatePackage.User" table="tb_User" select-before-update="false">
                     <cache usage="read-only"/>                 //指定该类使用二级缓存其中usage可以为read-only   read-write   read-write;三种方式
                     <id name="id" column="id" type="integer">
                              <generator class="native" />
                     </id>
                     <property name="name" type="string" not-null="true" length="50">
                               <column name="name" />
                     </property>
                     <property name="password" type="string" not-null="true" length="20">
                                 <column name="password" />
                     </property>
                     <property name="createTime" type="date" not-null="false" lazy="true">
                                 <column name="createTime" />
                     </property>
                     <property name="expireTime" type="date" not-null="false">
                                  <column name="expireTime" />
                     </property>
          </class>
</hibernate-mapping>

3  对于Hibernate缓存的理解

      首先是一级缓存,一级缓存只针对于当前局部的线程的session对象,且不同的线程中的sessio互不影响,既数据不能实现资源共享,当第一次从当前线程的session对象之中装在对象的时候,当前的session会直接向数据库中获取数据,然后输出同时并将该对象数据写入到当前的sessin的缓存当中,但如果第二次再次从当前的session中装载对象的时候,session管理器,会在当前线程的session缓存之中,搜索是否存在与第二次被装载的对象相同的对象,既判断是否该对象已经被装载过,如果找到,就直接从缓存当中取出该对象,既不再次访问数据库,否则将从数据库中装载需要的对象,这就叫做session级别的一级缓存,一级缓存的这种机制,极大的降低了对数据库的访问次数,提高了程序的效率。

     其次是二级缓存,既属于SessionFactory级别的缓存,指当前的线程中不同的session对象可以共享同一个数据对象,当当前线程中的session第一次装载数据对象时,会直接访问数据库,然后写入到当前session缓存当中,当另一个session1对象需要访问该session已经访问的那个数据对象的时候,在开启二级缓存的条件下(如果不开启二级缓存则无法实现不同session中同一数据对象的共享),   session1会先从自身的一级缓存之中,搜索是否存在该数据对象,如果存在,则返回该数据对象,如果不存在,则从二级缓存之中,搜索是否存在该对象,如果存在,则返回该对象,如果二级缓存之中仍旧没有该对象,则直接访问数据库,之后将该对象写入二级缓存和一级缓存之中。二级缓存适用于那些不经常被更改的数据,对于经常被更改的数据,不建议放在二级缓存之中。







0 0