[英]How to Audit Spring data jpa @Query?
To Audit log all the DB changes , we have implemented Hibernate Interceptor(org.hibernate.Interceptor)
. 为了审核所有数据库更改,我们实现了Hibernate
Interceptor(org.hibernate.Interceptor)
。 We can able to log the audit for the query executed using JpaRepository 我们可以记录使用JpaRepository执行的查询的审核
Interceptor We have used- Sample 我们使用的拦截器-样本
import java.io.Serializable;
import java.util.Iterator;
import org.hibernate.CallbackException;
import org.hibernate.EntityMode;
import org.hibernate.Interceptor;
import org.hibernate.Transaction;
import org.hibernate.type.Type;
public class TestInterceptor implements Interceptor {
@Override
public boolean onLoad(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types)
throws CallbackException {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState,
String[] propertyNames, Type[] types) throws CallbackException {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types)
throws CallbackException {
// TODO Auto-generated method stub
return false;
}
@Override
public void onDelete(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types)
throws CallbackException {
// TODO Auto-generated method stub
}
@Override
public void onCollectionRecreate(Object collection, Serializable key) throws CallbackException {
// TODO Auto-generated method stub
}
@Override
public void onCollectionRemove(Object collection, Serializable key) throws CallbackException {
// TODO Auto-generated method stub
}
@Override
public void onCollectionUpdate(Object collection, Serializable key) throws CallbackException {
// TODO Auto-generated method stub
}
@Override
public void preFlush(Iterator entities) throws CallbackException {
// TODO Auto-generated method stub
}
@Override
public void postFlush(Iterator entities) throws CallbackException {
// TODO Auto-generated method stub
}
@Override
public Boolean isTransient(Object entity) {
// TODO Auto-generated method stub
return null;
}
@Override
public int[] findDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState,
String[] propertyNames, Type[] types) {
// TODO Auto-generated method stub
return null;
}
@Override
public Object instantiate(String entityName, EntityMode entityMode, Serializable id) throws CallbackException {
// TODO Auto-generated method stub
return null;
}
@Override
public String getEntityName(Object object) throws CallbackException {
// TODO Auto-generated method stub
return null;
}
@Override
public Object getEntity(String entityName, Serializable id) throws CallbackException {
// TODO Auto-generated method stub
return null;
}
@Override
public void afterTransactionBegin(Transaction tx) {
// TODO Auto-generated method stub
}
@Override
public void beforeTransactionCompletion(Transaction tx) {
// TODO Auto-generated method stub
}
@Override
public void afterTransactionCompletion(Transaction tx) {
// TODO Auto-generated method stub
}
@Override
public String onPrepareStatement(String sql) {
// TODO Auto-generated method stub
return null;
}
} }
But if we run the query via org.springframework.data.jpa.repository.Query
that interceptor is not getting called. 但是,如果我们通过
org.springframework.data.jpa.repository.Query
运行查询,则不会调用该拦截器。
Is this possible to Audit/Intercept the Query Executed using org.springframework.data.jpa.repository.Query
这可以审计/拦截使用
org.springframework.data.jpa.repository.Query
执行的查询
ie I have the following Query in my Repository, this is not triggering Hibernate Interceptor 即我的存储库中有以下查询,这不会触发休眠拦截器
@Transactional
@Modifying
@Query("DELETE from MyEntity my where my.id =?1")
void deleteById(Long id);
To intercept spring data queries add this prop: 要拦截spring数据查询,请添加以下属性:
spring.jpa.properties.hibernate.session_factory.interceptor=com.yourpacakge.TestInterceptor spring.jpa.properties.hibernate.session_factory.interceptor = com.yourpacakge.TestInterceptor
I used an interceptor class that extends from EmptyInterceptor just for simplicity. 为了简单起见,我使用了从EmptyInterceptor扩展的拦截器类。
public class MyInterceptor extends EmptyInterceptor {
@Override
public String onPrepareStatement(String sql) {
System.out.println("Query intercepted: " + sql);
return super.onPrepareStatement(sql);
}
}
DOCS: https://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html#configurations-session-events DOCS: https : //docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html#configurations-session-events
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.