I'm trying to use interceptors with hibernate in order to create a log table.
The application works fine but my interceptors are never triggered when they were supposed to be triggered.
I tried the solutions of this post on stack overflow and I did not succed in making it work.
What I did is:
- create an entity class that will be the table where the log are stored (
UserLog
) - create another class called by the interceptor that will fill my table (dao), in this example it is called
UserLogUtil
I tried the first solution, making my interceptor class not dependant on spring @Autowire
by making the UserLogUtil
a singleton manually and adding the following line to my application.properties : spring.jpa.properties.hibernate.ejb.interceptor=LogInterceptor
.
I also tried the second solution by autowiring UserLogUtil
however my spring configuration is in xml and although I tried to convert the beans from the above example it did not work.
This is what my interceptor looks like:
public class LogInterceptor extends EmptyInterceptor {
private UserLogUtil userLogUtil = UserLogUtil.getInstance();
public boolean onSave(Object entity,Serializable id, Object[] state,String[] propertyNames,Type[] types)
throws CallbackException {
if (entity instanceof Owner){
userLogUtil.log(........));
}
return false;
}
I think I missed something in the process of building my interceptor and my design could be flawed. Do you have any idea that could make my interceptor function as it should ?