我正在使用 JPA,但我需要解开 EntityManagerFactory,以便我可以向 session 添加拦截器。之后我想将 session 包装回 EntityManager。
“为什么不直接使用 Session 而不是 EntityManager?”我们仍然希望减少可能的技术迁移的影响
For what do I want to use Interceptor:
我可以通过以下方式恢复该问题:该项目可以在警报数据库上运行查询。每个地点都有一个带有警报表的数据库,但客户端希望拥有一个数据库,我们必须在其中创建多个“警报表”,每个地点一个(例如:Table_Alarm-Place1、Table_Alarm-Place2)。这意味着我们将为同一实体拥有多个表,拦截器的目标是更改 hibernate 在最终 SQL 中生成的表名称
How I pretend to use the interceptor:
public class SqlInterceptor extends EmptyInterceptor {
private String tableSufix;
private static final Logger LOGGER = LoggerFactory.getLogger(SqlInterceptor.class);
public SqlInterceptor(String tableSufix) {...}
@Override
public String onPrepareStatement(String sql) {
String finalSql;
//Manipulated SQL (parsed by Hibernate)
return finalSql;
}
}
- 该项目使用 JPA 2.1 和 Hibernate 4.3.11.Final
最佳答案
重写 Hibernate 的 EmptyInterceptor 的一个 super 简单的方法就是在属性文件中这样做
spring.jpa.properties.hibernate.session_factory.interceptor=<fully-qualified-interceptor-class-name>
干杯:)
关于java - 如何为 EntityManagerFactory 设置 Hibernate 拦截器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34655449/