java之Hibenate中监听事件的重写和二级cache缓存
管理緩存和統(tǒng)計(jì)緩存
Cache cache? = sessionFactory.getCache();
????????????? //清除指定的News對(duì)象
????????????? cache.evictEntity(News.class, id);
????????????? //清除所有的news對(duì)象
????????????? cache.evictEntityRegion(News.class);
????????????? //清除指定id的news所關(guān)聯(lián)的參與者集合屬性
????????????? cache.evictColleciton("News.actors",id);
????????????? //清除所有News關(guān)聯(lián)的參與者集合屬性
????????????? cache.evictCollectionRegion("News.actors");
<!---開啟二級(jí)緩存--->
<property name=”hibernate.cache.user_second_level_cache”>true</property>
<!---設(shè)置緩存區(qū)實(shí)現(xiàn)類--->
<property name=”hibernate.cahce.regin.factory_class”>org.hibernate.cahce.ehcahe.EhCacheRegionFactory</property>
//使用緩存
@Entity
@Table
@Cache(usage=CacheConcurrencyStrategy.READ_ONLY)
//統(tǒng)計(jì)緩存
<!---開啟二級(jí)緩存的統(tǒng)計(jì)功能--->
<property name=”hibernate.generate_statistics”>true</property>
<!---設(shè)置使用結(jié)構(gòu)化方法來維護(hù)緩存項(xiàng)--->
<property anem=”hibernate.cache.user_structured_entries”>true</property>
//查看二級(jí)緩存? [統(tǒng)計(jì)二級(jí)緩存]
????????????? Map cacheEntries = sessionFactory.getStatistics()
??????????????????????????? //
??????????????????????????? .getSecondLevelCacheStatistics("org.crazyit.app.domain.News")
??????????????????????????? .getEntries();
使用查詢緩存
//默認(rèn)是關(guān)閉緩存
session.createQuery().setCacheable(false).list();
//開啟查詢緩存
session.createQuery().setCacheable(true).list();
Iterator it = session.createQuery().setCacheable(true).iterate();
攔截器的定義
MyInterCeptor extends EmptyInterceptor
{
//當(dāng)刪除實(shí)體時(shí),onDelete()方法被調(diào)用
public void onDelete(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types){}
//當(dāng)把持久化實(shí)體的狀態(tài)同步到數(shù)據(jù)庫(kù)時(shí),onFlushDirty()方法調(diào)用
public Boolean onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousSate, String[] propertyNames, Type[] types){}
//當(dāng)加載持久化實(shí)體時(shí),onLoad()方法被調(diào)用
public Boolean onload(Object entity,, Serializable id, Object[] state, String[] proipertyNames, Type[] types){}
//保存持久化實(shí)例時(shí),調(diào)用該方法
public Boolean onSave(Obejct entity, Serializable id, Object[] state, String[] propertyNames, Type[] type){}
//持久化所做修改同步后,調(diào)用preFlush()方法
public void postFlush(Iterator entities){}
//在同步持久化所做修改之前,調(diào)用preFlush()方法
public void preFlush(Iterater entities){}
//事務(wù)提交之前,觸發(fā)該方法
public void beforeTransactionCompletion(Transaction tx){}
//事務(wù)提交之后觸發(fā)該方法
public void afterTransactionComplistion(Transaction tx){}
}
//設(shè)置全家攔截器
static Configuration cfg = new Configration().configure()
//設(shè)置啟用全局?jǐn)r截器
.setInterceptor(new MyInterceptor())
?
自定義監(jiān)聽器
MyNameListener extends DefaultLoadEventLister{}
//1.創(chuàng)建一個(gè)SessionFactory對(duì)象
????????????? SessionFactory sessionFactory = null;
????????????? //1).創(chuàng)建Configuration對(duì)象:對(duì)應(yīng)hibernate的基本配置信息和對(duì)象映射信息
????????????? Configuration configuration = new Configuration().configure();
????????????? //2)創(chuàng)建ServiceRegistry對(duì)象:hibernate 4.x新加對(duì)象
????????????? //hibernate的任何配置和服務(wù)都需要在該對(duì)象中注冊(cè)后才能生效。
????????????? ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
static{
?????? //獲取該SessionFactory的事件監(jiān)聽注冊(cè)器
?????? EventListenerRegister elr = ((SessionFactoryImpl)sf).getServiceRegistry().getService(EventListerRegistry.class);
//使用用于指定的攔截器序列代替系統(tǒng)的save攔截器
elr.setListers(EvetnType.SAVE,mySaveLister.class);
//使用用于指定的攔截器序列代替系統(tǒng)的load攔截器序列
elr.setListeners(EventyType.LOAD,MyLoadLsiter.class);
?????? }
?
?
?
?
<!------>
?
轉(zhuǎn)載于:https://www.cnblogs.com/sundaysjava/p/10349555.html
總結(jié)
以上是生活随笔為你收集整理的java之Hibenate中监听事件的重写和二级cache缓存的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Shadow DOM及自定义标签
- 下一篇: Sx05RE-S905.arm-2.2.