日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

Hibernate 拦截器实例

發(fā)布時間:2023/12/19 47 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Hibernate 拦截器实例 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Hibernate 在操作數(shù)據(jù)庫的時候要執(zhí)行很多操作,這些動作對用戶是透明的。這些操作主要是有攔截器和時間組成

?hibernate攔截器可以攔截大多數(shù)動作,比如事務(wù)開始之后(afterTransactionBegin)、事務(wù)完成之前(beginTransactionCompletion)、事務(wù)完成之后(afterTransactionCompletion)、持久化對象之前(onSave),一個攔截器必須實現(xiàn)org.hibernate.Interceptor借口,hibernate提供了一個實現(xiàn)該借口的類EmptyInterceptor類

下面編寫一個hibernate實例來說明hibernate攔截器的作用

首先編寫一個保存持久化對象的信息類EntityInfo

public class EntityInfo {public Object entityBean;public Serializable id;public String[] properties;public Object getEntityBean() {return entityBean;}public void setEntityBean(Object entityBean) {entityBean = entityBean;}public Serializable getId() {return id;}public void setId(Serializable id) {this.id = id;}public String[] getProperties() {return properties;}public void setProperties(String[] properties) {this.properties = properties;}public String toString(){String info = "";if(entityBean !=null){info = entityBean.getClass().toString()+"\r\nid:"+id+"\r\n";if(properties != null){//處理properties中的所有元素for(String property:properties){//得到getter方法名try {String getter = "get" + property.substring(0, 1).toUpperCase()+property.substring(1);//使用反射技術(shù)和gettter方法名獲得Method對象Method method = entityBean.getClass().getMethod(getter);//調(diào)用gettter方法,并追加生成要返回的信息info = info + property + ":" +method.invoke(entityBean).toString() +"\r\n";} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}return info;} }

實現(xiàn)攔截器類,代碼如下

public class EntityBeanInterceptor extends EmptyInterceptor {private ThreadLocal entityBeans = new ThreadLocal();@Overridepublic void afterTransactionBegin(Transaction tx){entityBeans.set(new HashSet<EntityInfo>());}@Overridepublic void afterTransactionCompletion(Transaction tx){if(tx.wasCommitted()){Iterator i = ((Collection)entityBeans.get()).iterator();while (i.hasNext()) {//在提交事務(wù)之后輸出試題bean的內(nèi)容EntityInfo info = (EntityInfo) i.next();//調(diào)用方法數(shù)據(jù)EntityBean對象processEntityBean(info);}}}private void processEntityBean(EntityInfo info){System.out.println(info);}@Overridepublic boolean onSave(Object entity,Serializable id, Object[] state,String[] propertyNames,Type[] types){EntityInfo info = new EntityInfo();info.entityBean = entity;info.properties = propertyNames;info.id =id;//在持久化對象后,將對象信息保存到當(dāng)前線程的HashSet<EntityInfo>對象中((HashSet<EntityInfo>) entityBeans.get()).add(info);return false;}}

注冊攔截器類,本例中在構(gòu)造Session時創(chuàng)建攔截器類

public class HibernateSessionFactory {/*其他代碼省略*/private static ThreadLocal threadLocal = new ThreadLocal();private static Configuration configuration = new Configuration();private static org.hibernate.SessionFactory sessionFactory;public static Session getSession(Interceptor... interceptor){Session session = (Session) threadLocal.get();if(session == null || !session.isOpen()){//如果session為空重新建立一個Session工廠if(sessionFactory == null){rebuildSessionFactory();}//如果interceptor參數(shù)值中包含攔截器對象,則安裝該攔截器session = (sessionFactory != null)?((interceptor.length == 0)?sessionFactory.openSession():sessionFactory.openSession(interceptor[0])):null;//如果ThreadLocal對象中沒有屬于當(dāng)前線程的session對象,則添加一個Session對象threadLocal.set(session);}}}


測試攔截器類

public class TestInterceptor {private void mian() { // TODO Auto-generated method stub Session session = HibernateSessionFactory.getSession(new EntityBeanInterceptor()); Transaction tx = session.beginTransaction(); //Customer是一個實體bean Customer customer = new Customer(); customer.setName("hqw"); session.saveOrUpdate(customer); tx.commit(); session.close(); } }

這樣在構(gòu)造session時就注冊了攔截器,應(yīng)為本文在EntityInterceptor類中注冊了在事務(wù)開始后、事務(wù)完成后、持久化后三個方法,所以在相應(yīng)地方就會調(diào)用攔截器中的方法

轉(zhuǎn)載于:https://www.cnblogs.com/haquanwen/p/3812609.html

總結(jié)

以上是生活随笔為你收集整理的Hibernate 拦截器实例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。