zoukankan      html  css  js  c++  java
  • 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);

           }

    <!------>

  • 相关阅读:
    SharePoint 2007图文开发教程(3)实现简单的WebPart
    ExtJS开发实践
    SharePoint 2007图文开发教程(2)使用SharePoint创建网站
    SharePoint 2007图文开发教程(1)简介,安装,配置及创建Web应用程序
    SharePoint 2007图文开发教程(6)实现Search Services
    回忆我们经典的开发工具
    SharePoint 2007图文开发教程(5)体验Event Handler
    常用Xpath对照表
    Oracle语句生成+存储过程测试工具发布:Easytran V0.1
    XML 路径语言(XPath) 版本 1.0
  • 原文地址:https://www.cnblogs.com/sundaysjava/p/10349555.html
Copyright © 2011-2022 走看看