CustomEnversListener and Spring
inna1411 Sep 16, 2011 7:06 PMI'm new to Envers, but was able to implement the basic auditing without problems. Now I'm trying to use my own table instead of REVINFO for saving the username. But no success !
My environment :
Hibernate 3.3.1 (not using config, but annotations instead)
Spring 2.6
Envers 1.2.2.GA
I created CustomEnversListener.java and CustomRevisionEntity.java as suggested by specs, but auditing process is still looking for REVINFO.
What changes should I make in Spring application-config.xml under sessionFactory bean to register CustomEnversListener.java ?
<property name="eventListeners">
<map>
<entry key="post-update">
<bean class="org.hibernate.envers.event.AuditEventListener"> ?????
should I add settings for CustomEnversListener here ? If, yes, how ?
</bean>
</entry>
</map>
</property>
CustomRevisionEntity.java
package com.company.audit;
import org.hibernate.envers.RevisionEntity;
import org.hibernate.envers.RevisionNumber;
import org.hibernate.envers.RevisionTimestamp;
import javax.persistence.*;
@Entity
@Table(name = "revision_info")
@RevisionEntity(CustomEnversListener.class)
public class CustomRevisionEntity
{
private int id;
private long timestamp; private String username = "test";
@Id
@RevisionNumber
@Column(name = "revision_id", nullable = false, updatable = false)
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
@RevisionTimestamp
@Column(name = "revision_timestamp")
public long getTimestamp()
{
return timestamp;
}
public void setTimestamp(long timestamp)
{
this.timestamp = timestamp;
}
@Column(name = "username")
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
}
CustomEnversListener.java
package com.company.audit;
import org.acegisecurity.Authentication;
import org.acegisecurity.context.SecurityContextHolder;
import org.hibernate.envers.RevisionListener;
public class CustomEnversListener implements RevisionListener
{
public void newRevision(Object revisionEntity)
{
CustomRevisionEntity customRevisionEntity = (CustomRevisionEntity) revisionEntity;
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
customRevisionEntity.setUsername(authentication.getName());
}
}
The output looks like :
Hibernate: insert into REVINFO (REVTSTMP, REV) values (?, ?)
[ERROR] JDBCExceptionReporter GDS Exception. 335544569. Dynamic SQL Error
SQL error code = -204
Table unknown
REVINFO doesn't exist and revision_info (see at the top) has been created in Firebird 2.1.
Please help to solve this problem !