2004.1.1 12:30 Ikuya wind orders Sales Order Date 2004.1.2-> 2004.1.3
The first time I thought of AOP is also the Trigger, but Trigger and Java code, after all, are two worlds, how the operator put names into a bit of trouble (to have all tables together with the last modifier of a column, and then modify the program) At the same time, a very inadequate Trigger's place AOP is the number of tables will be the number of Writing Triger, how many have shown how much we should write out.
Hibernate is just the Community Area has a History Interceptor's article, it is AOP's solved the problem.
The concept of 1.Hibernate Inteceptor
Inteceptor - the concept of interceptors from the AOP, the Chinese version of the 2.1.6 reference manual which is written this way
Interceptor interface from the session to your application's callback method, so that you can be in persistent object save / change / delete or load when the operation of its property. Typical code:
Java code
- public class AuditInterceptor implements Interceptor, Serializable
- (
- public boolean onFlushDirty (Object entity,
- Serializable id,
- Object [] currentState,
- Object [] previousState,
- String [] propertyNames,
- Type [] types)
- (
- if (entity instanceof Auditable)
- (
- for (int i = 0; i <propertyNames.length; i + +)
- (
- if ( "lastUpdateTimestamp". equals (propertyNames [i]))
- (
- currentState [i] = new Date ();
- return true;
- )
- )
- )
- return false;
- )
- )
When the session is created when the designated interceptor.
Java code
- Session session = sf.openSession (new AuditInterceptor ());
Can be seen, Interceptor basic models are
1. Provide the onLoad (), onSave (), onDelete, onFlushDirty () callback function a few.
In the creation of session to session designated Interceptor.
2. Each callback function parameters are as follows, have been enough for anything
1.Object entity object
2.Serializable id object id
3.String [] propertyNames property name of the array
4.Type [] types array of property types
5.Object [] previousState update the value of property before
6.Object [] currentState updated property values
3. In the function, through if (entity instanceof Auditable) to determine whether the target object type.
2.History Interceptor pattern
1. All records required to change the history of the object implement Historizable interface.
History Interceptor according to if (entity instanceof Historizable) l to determine whether the required work.
2.History Interceptor has setUserName () function. In the process put the name of incoming operators.
3. HistoryEntry create the POJO and HBM
Contains the following columns id, who, what, property, oldValue, newValue
4. For all the records necessary to change the history of the object separately to create a History table, just a different table name, (or a table with composite primary key?)
5. For all the records necessary to change the history of the object in the POJO and Medium HBM add historyEntry Set.
3. The remaining questions
1. Monolayer of the target has been the perfect solution, but orders a subset of object solution?
2. Since the use of Interceptor required to create the session, with Spring's declarative transaction management how to combine?
3. Need to be changed table name and column names translated into Chinese.
Trigger questions are one of one more could not be completed.







