Hibernate缓存

来源:互联网 发布:店淘宝客怎么一键复制 编辑:程序博客网 时间:2024/05/23 11:57

 Hibernate采用二级缓存

第一级缓存   -> Session缓存;
evict(Object o) //清楚一个对象
clear()         //清楚所有对象

第二级缓存   -> SessionFactroy的外置缓存

备注:
执行大量的数据更新时可以:
1.使用JDBC
      tx = session.beginTransaction();
      Connection = session.connection();
      .....
      tx.commit();

    2.使用存储过程

管理第二级缓存:
hibernate.cfg.xml
1.打开二级缓存
hibernate.cache.use_secend_level_cache=true  //打开二级缓存

2.Hibernater二级缓存用第三方工具类
hibernate.cache.provider_calss=org.hibernate.cache.EhCacheProvider

3.指定缓存哪些对象
a:User.hbm.xml 里配置
<hibernate-mapping package="com.pannuo.entity">
    <class name="Card" table="tb_card" lazy="true">
        <cache useage="read-only" />                  //必须在<id>前
        <id name="cardId" column="cardId" type="int">
            <generator class="increment"/>
        </id>
</hibernate-mapping>

b:hibernate.cfg.xml 里配置
<hibernate-configuration>
    <session-factory>
        <mapping resource="com/pannuo/entity/User.hbm.xml"/>
        <calss-cache calss="come.pannuo.entity.User" usage="read-only"/> //必须在<mapping>后面
    </session-factory>
</hibernate-configuration>

原创粉丝点击