如何在Spring Boot中使用Spring管理的Hibernate拦截器? [英] How to use Spring managed Hibernate interceptors in Spring Boot?
问题描述
是否可以集成Spring管理的Hibernate拦截器( http:/ /docs.jboss.org/hibernate/orm/4.3/manual/en-US/html/ch14.html )在Spring Boot?
I' m使用Spring Data JPA和Spring Data REST,并且需要一个Hibernate拦截器来处理实体上特定字段的更新。
对于标准的JPA事件,不可能获取旧值,因此我认为我需要使用Hibernate拦截器。
没有特别简单的添加方法一个Hibernate拦截器,它也是一个Spring Bean,但如果它完全由Hibernate管理,你可以轻松地添加一个拦截器。为此,请在 application.properties
中添加以下内容: .jpa.properties.hibernate.ejb.interceptor = my.package.MyInterceptorClassName
如果您需要拦截器也可以创建自己的 LocalContainerEntityManagerFactoryBean
。来自Spring Boot 1.1.4的 EntityManagerFactoryBuilder
对于属性的泛型有一点限制,因此您需要将其转换为(Map)$ c $ b>
$ b
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(
EntityManagerFactoryBuilder工厂,DataSource数据源,
JpaProperties属性){
Map< String,Object> jpaProperties = new HashMap< String,Object>();
jpaProperties.putAll(properties.getHibernateProperties(dataSource));
jpaProperties.put(hibernate.ejb.interceptor,hibernateInterceptor());
return factory.dataSource(dataSource).packages(sample.data.jpa)
.properties((Map)jpaProperties).build();
$ b @Bean
public EmptyInterceptor hibernateInterceptor(){
return new EmptyInterceptor(){
@Override
public boolean onLoad(Object实体,Serializable id,Object []状态,
String [] propertyNames,Type []类型){
System.out.println(Loaded+ id);
返回false;
}
};
}
Is it possible to integrate Spring managed Hibernate interceptors (http://docs.jboss.org/hibernate/orm/4.3/manual/en-US/html/ch14.html) in Spring Boot?
I'm using Spring Data JPA and Spring Data REST and need an Hibernate interceptor to act on an update of a particular field on an entity.
With standard JPA events it's not possible to get the old values, and hence I think I need to use the Hibernate interceptor.
There's not a particularly easy way to add a Hibernate interceptor that is also a Spring Bean but you can easily add an interceptor if it's manged entirely by Hibernate. To do that add the following to your application.properties
:
spring.jpa.properties.hibernate.ejb.interceptor=my.package.MyInterceptorClassName
If you need the Interceptor to also be a bean you can create your own LocalContainerEntityManagerFactoryBean
. The EntityManagerFactoryBuilder
from Spring Boot 1.1.4 is a little too restrictive with the generic of the properties so you need cast to (Map)
, we'll look at fixing that for 1.2.
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(
EntityManagerFactoryBuilder factory, DataSource dataSource,
JpaProperties properties) {
Map<String, Object> jpaProperties = new HashMap<String, Object>();
jpaProperties.putAll(properties.getHibernateProperties(dataSource));
jpaProperties.put("hibernate.ejb.interceptor", hibernateInterceptor());
return factory.dataSource(dataSource).packages("sample.data.jpa")
.properties((Map) jpaProperties).build();
}
@Bean
public EmptyInterceptor hibernateInterceptor() {
return new EmptyInterceptor() {
@Override
public boolean onLoad(Object entity, Serializable id, Object[] state,
String[] propertyNames, Type[] types) {
System.out.println("Loaded " + id);
return false;
}
};
}
这篇关于如何在Spring Boot中使用Spring管理的Hibernate拦截器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!