• java之Hibenate中监听事件的重写和二级cache缓存


    管理缓存和统计缓存

    Cache cache  = sessionFactory.getCache();

                  //清除指定的News对象

                  cache.evictEntity(News.class, id);

                  //清除所有的news对象

                  cache.evictEntityRegion(News.class);

                  //清除指定id的news所关联的参与者集合属性

                  cache.evictColleciton("News.actors",id);

                  //清除所有News关联的参与者集合属性

                  cache.evictCollectionRegion("News.actors");

    <!---开启二级缓存--->

    <property name=”hibernate.cache.user_second_level_cache”>true</property>

    <!---设置缓存区实现类--->

    <property name=”hibernate.cahce.regin.factory_class”>org.hibernate.cahce.ehcahe.EhCacheRegionFactory</property>

    //使用缓存

    @Entity

    @Table

    @Cache(usage=CacheConcurrencyStrategy.READ_ONLY)

    //统计缓存

    <!---开启二级缓存的统计功能--->

    <property name=”hibernate.generate_statistics”>true</property>

    <!---设置使用结构化方法来维护缓存项--->

    <property anem=”hibernate.cache.user_structured_entries”>true</property>

    //查看二级缓存  [统计二级缓存]

                  Map cacheEntries = sessionFactory.getStatistics()

                                //

                                .getSecondLevelCacheStatistics("org.crazyit.app.domain.News")

                                .getEntries();

    使用查询缓存

    //默认是关闭缓存

    session.createQuery().setCacheable(false).list();

    //开启查询缓存

    session.createQuery().setCacheable(true).list();

    Iterator it = session.createQuery().setCacheable(true).iterate();

    拦截器的定义

    MyInterCeptor extends EmptyInterceptor

    {

    //当删除实体时,onDelete()方法被调用

    public void onDelete(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types){}

    //当把持久化实体的状态同步到数据库时,onFlushDirty()方法调用

    public Boolean onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousSate, String[] propertyNames, Type[] types){}

    //当加载持久化实体时,onLoad()方法被调用

    public Boolean onload(Object entity,, Serializable id, Object[] state, String[] proipertyNames, Type[] types){}

    //保存持久化实例时,调用该方法

    public Boolean onSave(Obejct entity, Serializable id, Object[] state, String[] propertyNames, Type[] type){}

    //持久化所做修改同步后,调用preFlush()方法

    public void postFlush(Iterator entities){}

    //在同步持久化所做修改之前,调用preFlush()方法

    public void preFlush(Iterater entities){}

    //事务提交之前,触发该方法

    public void beforeTransactionCompletion(Transaction tx){}

    //事务提交之后触发该方法

    public void afterTransactionComplistion(Transaction tx){}

    }

    //设置全家拦截器

    static Configuration cfg = new Configration().configure()

    //设置启用全局拦截器

    .setInterceptor(new MyInterceptor())

    自定义监听器

    MyNameListener extends DefaultLoadEventLister{}

    //1.创建一个SessionFactory对象

                  SessionFactory sessionFactory = null;

                  //1).创建Configuration对象:对应hibernate的基本配置信息和对象映射信息

                  Configuration configuration = new Configuration().configure();

                  //2)创建ServiceRegistry对象:hibernate 4.x新加对象

                  //hibernate的任何配置和服务都需要在该对象中注册后才能生效。

                  ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());

    static{

           //获取该SessionFactory的事件监听注册器

           EventListenerRegister elr = ((SessionFactoryImpl)sf).getServiceRegistry().getService(EventListerRegistry.class);

    //使用用于指定的拦截器序列代替系统的save拦截器

    elr.setListers(EvetnType.SAVE,mySaveLister.class);

    //使用用于指定的拦截器序列代替系统的load拦截器序列

    elr.setListeners(EventyType.LOAD,MyLoadLsiter.class);

           }

    <!------>

  • 相关阅读:
    Git使用入门
    Android系统移植和驱动开发
    搭建Android开发环境
    Android源代码的下载和编译
    Andriod深度探索(卷1)HAL与驱动开发 第十章读书心得
    Andriod深度探索(卷1)HAL与驱动开发 第九章读书心得
    Andriod深度探索(卷1)HAL与驱动开发 第八章读书心得
    Android深度探索(卷1)HAL与驱动开发 第七章读书心得
    Android深度探索(卷1)HAL与驱动开发 第六章读书心得
    Android深度探索(卷1)HAL与驱动开发 第五章读书心得
  • 原文地址:https://www.cnblogs.com/sundaysjava/p/10349555.html
Copyright © 2020-2023  润新知