views:

31

answers:

1

Hi,

I'm trying to access the database from Hibernate Interceptor (I need to audit only specific objects that are defined in a different table) and the access is impassable (I get exceptions). Is there a way to access database in interceptor?

My AuditTrailInterceptor is:

public class AuditTrailInterceptor extends EmptyInterceptor {

public boolean onSave(Object entity, Serializable id, Object[] state, String[]    
propertyNames, Type[] types) {
   AuditTrailService serviceComp = (AuditTrailService) SpringBeanFinder
                       .findBean(SpringBeanFinder.AUDIT_SERVICE);
    serviceComp.getObjectAuditCompanies(theCompany)
    return false;
  }

}

@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
      public Collection<ObjectAuditCompany> getObjectAuditCompanies(Company company){
            return  objectAuditCompanyDAO.findByQuery("from " + objectAuditCompanyDAO.getPersistentClass().getName() + " where company=? ", company);

      }

The AuditTrailInterceptor defined in the applicationContext.xml as a property

<bean id="onboardSessionFactory" parent="sessionFactory">
<property name="entityInterceptor">
              <bean class="com.mycompany.daoimpl.AuditTrailInterceptor" />
  </property>
</bean>

Thanks!

A: 

Not sure exactly what problem you're having, but I remember one stumbling block I've seen in dealing with interceptors.

In order to access the database in a hibernate interceptor or event listener, you have to open a new session and can't just share a session with the hibernate operation being intercepted, which makes sense if you contemplate what the interceptor is doing a bit...

There's example code at the JBoss wiki for use of both interceptors and event listeners for audit logging that might be helpful.

If this doesn't help you find the solution, you might want to post more of what you've tried and a stack trace of the issue.

Don Roby