日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/12/1 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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);

?????? }

?

?

?

?

<!------>

?

轉載于:https://www.cnblogs.com/sundaysjava/p/10349555.html

總結

以上是生活随笔為你收集整理的java之Hibenate中监听事件的重写和二级cache缓存的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。