afertel wrote:
Ok, it does automatically. But I want to know if an object is dirty just to add a history record. For that I suppose that I have to develop an interceptor, but when I call it, do I have to set all the arguments
Code:
findDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types)
whenever I call this method or does Hibernate have any method to know the property names, the previous state and the types by itself?
Yes, for history purposes you have to implement an interceptor. But be aware: you must not use the Hibernate session in this interceptor (if you need a session method, for instance to save a history record, you must use another session); and: your interceptor must be thread safe and should not throw. If you are using JPA, then your interceptor must be a stateless class and you have no access to EntityManager.
That said, I show you how I made a history interceptor. It's interesting part is this:
Code:
public class HistoryInterceptor extends EmptyInterceptor {
private static Logger logger = LoggerFactory.getLogger(HistoryInterceptor.class);
/**
* Called when an object is detected to be dirty, during a flush. The interceptor may modify the detected
* <tt>currentState</tt>, which will be propagated to both the database and the persistent object.
* Note that not all flushes end in actual synchronization with the database, in which case the
* new <tt>currentState</tt> will be propagated to the object, but not necessarily (immediately) to
* the database. It is strongly recommended that the interceptor <b>not</b> modify the <tt>previousState</tt>.
*
* @return <tt>true</tt> if the user modified the <tt>currentState</tt> in any way.
*/
@Override
public boolean onFlushDirty(final Object pEntity,
final Serializable pId,
final Object[] pCurrentState,
final Object[] pPreviousState,
final String[] pPropertyNames,
final Type[] pTypes) throws CallbackException {
// there must no exception be uncaught here!
try {
.....
Finally, I don't think you should call into the
Code:
findDirty
method.
Carlo
-------------------------
please give me some credits if this post helped you