<hibernate-annotations.version>3.3.1.GA</hibernate-annotations.version>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle9iDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.generate_statistics">true</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
<prop key="hibernte.cache.provider_configuration_file_resource_path">classpath*:ehcache.xml</prop>
</props>
</property>
<property name="dataSource" ref="dataSource"/>
<!--<property name="entityInterceptor" ref="auditInterceptor"/>-->
<property name="eventListeners" >
<map>
<!-- @see org.hibernate.event.EventListeners#eventInterfaceFromType -->
<entry key="pre-update" value-ref="metaDataListener" />
<entry key="pre-insert" value-ref="metaDataListener" />
<entry key="create" value-ref="metaDataListener" />
</map>
</property>
<!-- let hibernate scan all these packages for persistent classes! -->
<property name="annotatedClasses">
<list>
<value>eu.europa.ec.digit.systat2.security.User</value>
</list>
</property>
</bean>
<bean id="metaDataListener" class="eu.europa.ec.digit.systat2.util.model.MetaDataListener" scope="singleton" />
public class MetaDataListener implements PreUpdateEventListener, PreInsertEventListener, PersistEventListener {
private static final Logger log = Logger.getLogger(MetaDataListener.class);
public MetaDataListener() {
log.info("metadatalistener created");
}
@PrePersist
public void setCreation(Object o) {
log.debug("metadatalistener .setCreation");
DataBaseMeta data = getMetaDataFromObject(o);
if (data != null) {
data.setDateCreat(getCurrentTime());
data.setUserCreat(SecurityUtilities.getCurrentUser());
data.setDateModif(data.getDateCreat());
data.setUserModif(data.getUserCreat());
if (log.isDebugEnabled()) {
log.debug(data.toString());
}
}
}
@PreUpdate
public void setModification(Object o) {
log.debug("metadatalistener .setModification");
DataBaseMeta data = getMetaDataFromObject(o);
if (data != null) {
data.setDateModif(getCurrentTime());
data.setUserModif(SecurityUtilities.getCurrentUser());
if (log.isDebugEnabled()) {
log.debug(data.toString());
}
}
}
private DataBaseMeta getMetaDataFromObject(Object o) {
DataBaseMeta data = null;
try {
BaseObject obj = (BaseObject) o;
data = obj.getMetaData();
if(data == null) {
data = new DataBaseMeta();
obj.setMetaData(data);
log.warn("metadatalistener .getMetaDataFromObject metadata created!");
}
} catch (Exception e) {
log.error("MetaDataListener could not extract metadata from object!", e);
}
return data;
}
private Date getCurrentTime() {
return new Date();
}
/**
* Return true if the operation should be vetoed
*/
public boolean onPreUpdate(PreUpdateEvent event) {
log.debug("metadatalistener .onPreUpdate");
setModification(event.getEntity());
return false;
}
/**
* Return true if the operation should be vetoed
*/
public boolean onPreInsert(PreInsertEvent event) {
log.debug("metadatalistener .onPreInsert");
setCreation(event.getEntity());
return false;
}
/**
* Handle the given create event.
*
* @param event The create event to be handled.
* @throws org.hibernate.HibernateException
*/
public void onPersist(PersistEvent event) throws HibernateException{
log.debug("metadatalistener .onPersist(event)");
setCreation(event.getObject());
}
/**
* Handle the given create event.
*
* @param event The create event to be handled.
* @throws HibernateException
*/
public void onPersist(PersistEvent event, Map createdAlready) throws HibernateException{
log.debug("metadatalistener .onPersist(event, map)");
setCreation(event.getObject());
}
}
DEBUG AbstractBatcher - preparing statement
DEBUG SequenceGenerator - Sequence identifier generated: 25
DEBUG AbstractBatcher - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
DEBUG AbstractBatcher - closing statement
DEBUG SequenceHiLoGenerator - new hi value: 25
DEBUG AbstractSaveEventListener - generated identifier: 1250, using strategy: org.hibernate.id.SequenceHiLoGenerator
DEBUG AbstractSaveEventListener - saving [eu.europa.ec.digit.systat2.security.User#1250]
DEBUG AbstractFlushingEventListener - flushing session
DEBUG AbstractFlushingEventListener - processing flush-time cascades
DEBUG AbstractFlushingEventListener - dirty checking collections
DEBUG AbstractFlushingEventListener - Flushing entities and processing referenced collections
DEBUG AbstractFlushingEventListener - Processing unreferenced collections
DEBUG AbstractFlushingEventListener - Scheduling collection removes/(re)creates/updates
DEBUG AbstractFlushingEventListener - Flushed: 1 insertions, 0 updates, 0 deletions to 1 objects
DEBUG AbstractFlushingEventListener - Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
DEBUG Printer - listing entities:
DEBUG Printer - eu.europa.ec.digit.systat2.security.User{viewingLastName=null, sexCode=M, perId=155000,
metaData=null, userId=1250, login=landepe,
[email protected], jobId=null, usualLastName=Van Landeghem, firstName2=null, birthDate=12 juin 2008, firstName=Peter, firstName4=null, jobId2=null, firstName3=null, syserNbr=null, sp2OuId=null, birthLastName=Van Landeghem, fullName=null, sp2OuId2=null}
DEBUG AbstractFlushingEventListener - executing flush
DEBUG ConnectionManager - registering flush begin
DEBUG UpdateTimestampsCache - Pre-invalidating space [sy2_user]
DEBUG MetaDataListener - metadatalistener .onPreInsert
DEBUG MetaDataListener - metadatalistener .setCreation
WARN MetaDataListener - metadatalistener .getMetaDataFromObject metadata created!
DEBUG MetaDataListener - dateCreat = Thu Jun 12 11:07:41 CEST 2008
,userCreat = opsomgu
,dateModif = Thu Jun 12 11:07:41 CEST 2008
,userModif = opsomgu
DEBUG AbstractEntityPersister - Inserting entity: [eu.europa.ec.digit.systat2.security.User#1250]
DEBUG AbstractBatcher - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
DEBUG SQL -
insert
into
sy2_user
(date_creat, date_modif, util_creat, util_modif, birth_date, birth_last_name, first_name, first_name_2, first_name_3, first_name_4, job_id_1, job_id_2, userid, email, per_id, sex_cd, org_id_sp2_1, org_id_sp2_2, no_sysper, usual_last_name, viewing_last_name, usr_id)
values
(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate:
insert
into
sy2_user
(date_creat, date_modif, util_creat, util_modif, birth_date, birth_last_name, first_name, first_name_2, first_name_3, first_name_4, job_id_1, job_id_2, userid, email, per_id, sex_cd, org_id_sp2_1, org_id_sp2_2, no_sysper, usual_last_name, viewing_last_name, usr_id)
values
(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
DEBUG AbstractBatcher - preparing statement
DEBUG AbstractEntityPersister - Dehydrating entity: [eu.europa.ec.digit.systat2.security.User#1250]
DEBUG TimestampType - binding null to parameter: 1
DEBUG TimestampType - binding null to parameter: 2
DEBUG StringType - binding null to parameter: 3
DEBUG StringType - binding null to parameter: 4
DEBUG DateType - binding '12 juin 2008' to parameter: 5
so, what I see is that my PreUpdateEventListener and PreInsertEventListener come too late or something.
I see the null values for the audit fields being set on the preparedstatement instead of the values of the toString().
I'm sure they are there... but metadata was null from the beginning and it seems the listeners come in when the map of properties to persist has already been built, is this possible.
I hope I have given enough information, If more info is needed I will provide it directly when asked (I just thought the post is already far too long for a quick read :)).