Hibernate 拦截器实例
生活随笔
收集整理的這篇文章主要介紹了
Hibernate 拦截器实例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Hibernate 在操作數據庫的時候要執行很多操作,這些動作對用戶是透明的。這些操作主要是有攔截器和時間組成
?hibernate攔截器可以攔截大多數動作,比如事務開始之后(afterTransactionBegin)、事務完成之前(beginTransactionCompletion)、事務完成之后(afterTransactionCompletion)、持久化對象之前(onSave),一個攔截器必須實現org.hibernate.Interceptor借口,hibernate提供了一個實現該借口的類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);//使用反射技術和gettter方法名獲得Method對象Method method = entityBean.getClass().getMethod(getter);//調用gettter方法,并追加生成要返回的信息info = info + property + ":" +method.invoke(entityBean).toString() +"\r\n";} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}return info;} }實現攔截器類,代碼如下
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()) {//在提交事務之后輸出試題bean的內容EntityInfo info = (EntityInfo) i.next();//調用方法數據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;//在持久化對象后,將對象信息保存到當前線程的HashSet<EntityInfo>對象中((HashSet<EntityInfo>) entityBeans.get()).add(info);return false;}}注冊攔截器類,本例中在構造Session時創建攔截器類
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參數值中包含攔截器對象,則安裝該攔截器session = (sessionFactory != null)?((interceptor.length == 0)?sessionFactory.openSession():sessionFactory.openSession(interceptor[0])):null;//如果ThreadLocal對象中沒有屬于當前線程的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(); } }
這樣在構造session時就注冊了攔截器,應為本文在EntityInterceptor類中注冊了在事務開始后、事務完成后、持久化后三個方法,所以在相應地方就會調用攔截器中的方法
轉載于:https://www.cnblogs.com/haquanwen/p/3812609.html
總結
以上是生活随笔為你收集整理的Hibernate 拦截器实例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2013年网络安全事件盘点
- 下一篇: 符号链接