Hi,
I just finished the implementation of my Interceptor.
I based it on the documentation example and on the EmptyInterceptor.
Everything works fine except that I can't see the amount of object created.
The weird thing is that I can never have a log with 'Creations:' being different from 0. Whereas it works fine with updates:
when updating, preFlush() is called
before onFlushDirty(), which is fine.
The reason for the failure is that the preFlush() method is called
after onSave() when creating an object, reseting the local counter previously increased.
I can't figure out why, and according to the doc implementation, it's not an expected behavior.
Anyone already encountered the problem?
Btw, I don't really understand how to implement the getEntity() method.
What is the purpose of this one? the doc says:
Quote:
Get a fully loaded entity instance that is cached externally
Are we speaking about the 2nd level cache? Shall I use this method to load object from the DB?
the Interceptor's code:
Code:
/**
* @version Aug 29, 2005 - 7:22:53 PM
*/
public final class BoInterceptor implements Interceptor, Serializable {
private Date dateMutualisation;
private int updates;
private int creates;
private int deletes;
/**
* The <code>Log</code> instance for this package.
*/
private Log log = LogFactory.getLog(this.getClass());
/**
* @param sessionFactory The sessionFactory to set.
*/
public void setSessionFactory(SessionFactory sessionFactory) {
Session session = sessionFactory.openSession();
DateComptable dateComptable = (DateComptable) session.get(DateComptable.class, "DATE MUTUALISATION");
this.dateMutualisation = dateComptable.getDateComptableDate();
session.close();
}
public void onDelete(Object entity,
Serializable id,
Object[] state,
String[] propertyNames,
Type[] types) {
this.deletes++;
}
public boolean onFlushDirty(Object entity,
Serializable id,
Object[] currentState,
Object[] previousState,
String[] propertyNames,
Type[] types) {
this.updates++;
boolean isModified = false;
for (int i = 0; i < types.length; i++) {
if ((types[i] instanceof StringType) && "".equals(currentState[i])) {
currentState[i] = null;
isModified = true;
log.debug("Empty attribute '" + propertyNames[i] + "', replaced by 'null'");
} else {
if (propertyNames[i].endsWith("DateMaj") && (new Date()).after(this.dateMutualisation)) {
currentState[i] = this.dateMutualisation;
isModified = true;
log.debug("Updated Objet with Mutualisation Date :" + entity.getClass().getName() + " Date Mutualisation : " + this.dateMutualisation);
}
}
}
return isModified;
}
public boolean onLoad(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {
return false;
}
public boolean onSave(Object entity,
Serializable id,
Object[] state,
String[] propertyNames,
Type[] types) {
boolean isModified = false;
this.creates++;
for (int i = 0; i < types.length; i++) {
if ((types[i] instanceof StringType) && "".equals(state[i])) {
state[i] = null;
isModified = true;
log.debug("Empty attribute '" + propertyNames[i] + "', replaced by 'null'");
} else {
if ((propertyNames[i].endsWith("DateCreation") || propertyNames[i].endsWith("DateMaj")) && (new Date()).after(this.dateMutualisation)) {
state[i] = this.dateMutualisation;
isModified = true;
log.debug("Created Objet with Mutualisation Date :" + entity.getClass().getName() + " Date Mutualisation : " + this.dateMutualisation);
}
}
}
return isModified;
}
public void postFlush(Iterator entities) {
log.debug("Creations: " + this.creates + ", Updates: " + this.updates + ", Deletes: " + this.deletes);
}
public void preFlush(Iterator entities) {
this.updates = 0;
this.creates = 0;
this.deletes = 0;
}
public Boolean isTransient(Object entity) {
return null;
}
public Object instantiate(String entityName, EntityMode entityMode, Serializable id) {
return null;
}
public int[] findDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types) {
return null;
}
public String getEntityName(Object object) {
return null;
}
public Object getEntity(String entityName, Serializable id) {
return null;
}
public void afterTransactionBegin(Transaction tx) {
}
public void afterTransactionCompletion(Transaction tx) {
}
public void beforeTransactionCompletion(Transaction tx) {
}
}
Hibernate version: 3.0.5 Code between sessionFactory.openSession() and session.close():Full stack trace of any exception that occurs:Name and version of the database you are using: MSSQL ServerThe generated SQL (show_sql=true):Debug level Hibernate log excerpt:[/quote]