攔截器
測(cè)試
Hibernate: insert into t2oo (version, avg, aid, id) values (?, ?, ?, ?)
Hibernate: update t2oo set version=?, avg=?, aid=? where id=? and version=?
//攔截到的
update=hbn.bean.T2oo@277
inserthbn.bean.T2oo@277
package
?hbn.test.supper.Interceptor;
import ?java.io.Serializable;
import ?java.util.HashSet;
import ?java.util.Iterator;
import ?java.util.Set;
import ?org.hibernate.CallbackException;
import ?org.hibernate.EntityMode;
import ?org.hibernate.Interceptor;
import ?org.hibernate.Transaction;
import ?org.hibernate.type.Type;
public ? class ?TestInterceptor? implements ?Interceptor,Serializable{
???? private ?Set?inserts? = ? new ?HashSet();
???? private ?Set?updates? = ? new ?HashSet();
????
?? ? // Session初化一個(gè)持久對(duì)象 如果這方法中改變了對(duì)象屬性就返回true 否則null
???? public ? boolean ?onLoad(Object?entity,?Serializable?id,?Object[]?state,?String[]?propertyNames,?Type[]?types)? throws ?CallbackException?{
???? ??? return ? false ;
????}
??? // Session flush()中檢查到臟數(shù)據(jù)是調(diào)用 如:tr.commit() ....
???? public ? boolean ?onFlushDirty(Object?entity,?Serializable?id,?Object[]?currentState,?Object[]?previousState,?String[]?propertyNames,?Type[]?types)? throws ?CallbackException?{
????????updates.add(entity);
???????? return ? false ;
????}
??? // Session Save() 當(dāng)修改了對(duì)象屬性返回true
???? public ? boolean ?onSave(Object?entity,?Serializable?id,?Object[]?state,?String[]?propertyNames,?Type[]?types)? throws ?CallbackException?{
????????
????????inserts.add(entity);
???????? return ? false ;
????}
???? // delete
???? public ? void ?onDelete(Object?entity,?Serializable?id,?Object[]?state,?String[]?propertyNames,?Type[]?types)? throws ?CallbackException?{
????}
? ?? //flush() 之前調(diào)用
???? public ? void ?preFlush(Iterator?entities)? throws ?CallbackException?{
????}
???? // flush() 執(zhí)行SQL語(yǔ)句之后調(diào)用
???? public ? void ?postFlush(Iterator?entities)? throws ?CallbackException?{
????????
???????? try ?{
???????????? for (Iterator?it? = ?updates.iterator();it.hasNext();){
????????????????System.out.println( " update= " + ?it.next()?);????
????????????}
???????????? for (Iterator?it? = ?inserts.iterator();it.hasNext();){
????????????????System.out.println( " insert " + ?it.next()?);????
????????????}
????????????
????????}? catch ?(Exception?e)?{
????????????e.printStackTrace();
????????}
????????
????}
???? public ?Boolean?isTransient(Object?entity)?{
???????? // ?TODO?Auto-generated?method?stub
???????? return ? null ;
????}
? ? //決定Session中那些對(duì)象是臟數(shù)據(jù) 如果null Session使用默認(rèn)處理臟數(shù)據(jù)
???? public ? int []?findDirty(Object?entity,?Serializable?id,?Object[]?currentState,?Object[]?previousState,?String[]?propertyNames,?Type[]?types)?{
???????? return ? null ;
????}
???? // 當(dāng)Session構(gòu)造實(shí)體類(lèi)對(duì)象前調(diào)用
???? public ?Object?instantiate(String?entityName,?EntityMode?entityMode,?Serializable?id)? throws ?CallbackException?{
???????? return ? null ;
????}
???? public ?String?getEntityName(Object?object)? throws ?CallbackException?{
???????? // ?TODO?Auto-generated?method?stub
???????? return ? null ;
????}
???? public ?Object?getEntity(String?entityName,?Serializable?id)? throws ?CallbackException?{
???????? // ?TODO?Auto-generated?method?stub
???????? return ? null ;
????}
???? public ? void ?afterTransactionBegin(Transaction?tx)?{
???????? // ?TODO?Auto-generated?method?stub
????????
????}
???? public ? void ?beforeTransactionCompletion(Transaction?tx)?{
???????? // ?TODO?Auto-generated?method?stub
????????
????}
???? public ? void ?afterTransactionCompletion(Transaction?tx)?{
???????? // ?TODO?Auto-generated?method?stub
????????
????}
}
import ?java.io.Serializable;
import ?java.util.HashSet;
import ?java.util.Iterator;
import ?java.util.Set;
import ?org.hibernate.CallbackException;
import ?org.hibernate.EntityMode;
import ?org.hibernate.Interceptor;
import ?org.hibernate.Transaction;
import ?org.hibernate.type.Type;
public ? class ?TestInterceptor? implements ?Interceptor,Serializable{
???? private ?Set?inserts? = ? new ?HashSet();
???? private ?Set?updates? = ? new ?HashSet();
????
?? ? // Session初化一個(gè)持久對(duì)象 如果這方法中改變了對(duì)象屬性就返回true 否則null
???? public ? boolean ?onLoad(Object?entity,?Serializable?id,?Object[]?state,?String[]?propertyNames,?Type[]?types)? throws ?CallbackException?{
???? ??? return ? false ;
????}
??? // Session flush()中檢查到臟數(shù)據(jù)是調(diào)用 如:tr.commit() ....
???? public ? boolean ?onFlushDirty(Object?entity,?Serializable?id,?Object[]?currentState,?Object[]?previousState,?String[]?propertyNames,?Type[]?types)? throws ?CallbackException?{
????????updates.add(entity);
???????? return ? false ;
????}
??? // Session Save() 當(dāng)修改了對(duì)象屬性返回true
???? public ? boolean ?onSave(Object?entity,?Serializable?id,?Object[]?state,?String[]?propertyNames,?Type[]?types)? throws ?CallbackException?{
????????
????????inserts.add(entity);
???????? return ? false ;
????}
???? // delete
???? public ? void ?onDelete(Object?entity,?Serializable?id,?Object[]?state,?String[]?propertyNames,?Type[]?types)? throws ?CallbackException?{
????}
? ?? //flush() 之前調(diào)用
???? public ? void ?preFlush(Iterator?entities)? throws ?CallbackException?{
????}
???? // flush() 執(zhí)行SQL語(yǔ)句之后調(diào)用
???? public ? void ?postFlush(Iterator?entities)? throws ?CallbackException?{
????????
???????? try ?{
???????????? for (Iterator?it? = ?updates.iterator();it.hasNext();){
????????????????System.out.println( " update= " + ?it.next()?);????
????????????}
???????????? for (Iterator?it? = ?inserts.iterator();it.hasNext();){
????????????????System.out.println( " insert " + ?it.next()?);????
????????????}
????????????
????????}? catch ?(Exception?e)?{
????????????e.printStackTrace();
????????}
????????
????}
???? public ?Boolean?isTransient(Object?entity)?{
???????? // ?TODO?Auto-generated?method?stub
???????? return ? null ;
????}
? ? //決定Session中那些對(duì)象是臟數(shù)據(jù) 如果null Session使用默認(rèn)處理臟數(shù)據(jù)
???? public ? int []?findDirty(Object?entity,?Serializable?id,?Object[]?currentState,?Object[]?previousState,?String[]?propertyNames,?Type[]?types)?{
???????? return ? null ;
????}
???? // 當(dāng)Session構(gòu)造實(shí)體類(lèi)對(duì)象前調(diào)用
???? public ?Object?instantiate(String?entityName,?EntityMode?entityMode,?Serializable?id)? throws ?CallbackException?{
???????? return ? null ;
????}
???? public ?String?getEntityName(Object?object)? throws ?CallbackException?{
???????? // ?TODO?Auto-generated?method?stub
???????? return ? null ;
????}
???? public ?Object?getEntity(String?entityName,?Serializable?id)? throws ?CallbackException?{
???????? // ?TODO?Auto-generated?method?stub
???????? return ? null ;
????}
???? public ? void ?afterTransactionBegin(Transaction?tx)?{
???????? // ?TODO?Auto-generated?method?stub
????????
????}
???? public ? void ?beforeTransactionCompletion(Transaction?tx)?{
???????? // ?TODO?Auto-generated?method?stub
????????
????}
???? public ? void ?afterTransactionCompletion(Transaction?tx)?{
???????? // ?TODO?Auto-generated?method?stub
????????
????}
}
測(cè)試
package
?hbn.test.supper.Interceptor;
import ?java.lang.reflect.Field;
import ?org.hibernate.Session;
import ?org.hibernate.SessionFactory;
import ?org.hibernate.Transaction;
import ?hbn.HibernateSessionFactory;
import ?hbn.bean.T2oo;
import ?junit.framework.TestCase;
public ? class ?TestIC? extends ?TestCase?{
???? private ?SessionFactory?sessionFactory;
???? protected ? void ?setUp()? throws ?Exception?{
???????? super .setUp();
???????? // 利用java反射得到?HibernateSessionFactory?->
???????? // private??static?org.hibernate.SessionFactory?sessionFactory;
???????? // 要模擬?并發(fā)?要?HibernateSessionFactory?得出的?有?threadLocal?不行?
????????HibernateSessionFactory.currentSession();
????????HibernateSessionFactory.closeSession();
????????Field?field? = ?HibernateSessionFactory. class .getDeclaredField( " sessionFactory " );
????????field.setAccessible( true );
????????sessionFactory? = ?(SessionFactory)?field.get(HibernateSessionFactory. class );
????}
????
???? public ? void ?testInc()? throws ?Exception?{
????????TestInterceptor?intx? = ? new ?TestInterceptor();
???????? // 加載攔截器
????????Session?session? = ?sessionFactory.openSession(intx);
????????
????????Transaction?tr? = ?session.beginTransaction();
????????T2oo?t2? = ? new ?T2oo( 23 );
????????session.save(t2);
????????t2.setAvg( new ?Integer( 99 ));
????????tr.commit();
????}
}
結(jié)果import ?java.lang.reflect.Field;
import ?org.hibernate.Session;
import ?org.hibernate.SessionFactory;
import ?org.hibernate.Transaction;
import ?hbn.HibernateSessionFactory;
import ?hbn.bean.T2oo;
import ?junit.framework.TestCase;
public ? class ?TestIC? extends ?TestCase?{
???? private ?SessionFactory?sessionFactory;
???? protected ? void ?setUp()? throws ?Exception?{
???????? super .setUp();
???????? // 利用java反射得到?HibernateSessionFactory?->
???????? // private??static?org.hibernate.SessionFactory?sessionFactory;
???????? // 要模擬?并發(fā)?要?HibernateSessionFactory?得出的?有?threadLocal?不行?
????????HibernateSessionFactory.currentSession();
????????HibernateSessionFactory.closeSession();
????????Field?field? = ?HibernateSessionFactory. class .getDeclaredField( " sessionFactory " );
????????field.setAccessible( true );
????????sessionFactory? = ?(SessionFactory)?field.get(HibernateSessionFactory. class );
????}
????
???? public ? void ?testInc()? throws ?Exception?{
????????TestInterceptor?intx? = ? new ?TestInterceptor();
???????? // 加載攔截器
????????Session?session? = ?sessionFactory.openSession(intx);
????????
????????Transaction?tr? = ?session.beginTransaction();
????????T2oo?t2? = ? new ?T2oo( 23 );
????????session.save(t2);
????????t2.setAvg( new ?Integer( 99 ));
????????tr.commit();
????}
}
Hibernate: insert into t2oo (version, avg, aid, id) values (?, ?, ?, ?)
Hibernate: update t2oo set version=?, avg=?, aid=? where id=? and version=?
//攔截到的
update=hbn.bean.T2oo@277
inserthbn.bean.T2oo@277