Trying an interceptor
for logging purposes. Created a repository that extends CrudRepository
with Redis
Entity. I have simple repository.save(entity)
method.
My interceptor looks like below:
public class MyRepoInterceptor extends EmptyInterceptor {
@Override
public boolean onSave(Object entity, Serializable id,
Object[] state, String[] propertyNames, Type[] types) {
log.info("Saving...");
return super.onSave(entity, id, state, propertyNames, types);
}
@Override
public boolean onPrepareStatement(String sql) {
log.info("Prepared Statement");
return super.onPrepareStatement(sql);
}
}
I do not see the message Saving...
, obviously interceptor is not working here.
However, in the same app, I have another repository for mySQL
. In this, I have a repository method findByXXX
. The interceptor is intercepting this method and I see the message Prepared Statement
.
My interceptor Config class:
@Component
public class RepoInterceptor implements HibernatePropertiesCustomizer {
@Autowired
MyRepoInterceptor myRepoInterceptor;
@Override
public void customize(Map<String, Object> hibernateProperties) {
//Tried this too. but no luck
//hibernateProperties.put("hibernate.session_factory.interceptor", myRepoInterceptor);
hibernateProperties.put("hibernate.ejb.interceptor", myRepoInterceptor);
}
}
Has anyone tried this? All that I want is, log any calls to database from "Crud/JPARepository" methods anywhere in the app without forcing the developer to do the logging.
If above is not the right solution, any other better solution(s)?