栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在Spring Boot中使用Spring托管的Hibernate拦截器?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在Spring Boot中使用Spring托管的Hibernate拦截器?

添加一个同时也是Spring
Bean的Hibernate拦截器并没有特别简单的方法,但是如果完全由Hibernate进行管理,则可以轻松添加一个拦截器。为此,将以下内容添加到您的

application.properties

spring.jpa.properties.hibernate.ejb.interceptor=my.package.MyInterceptorClassName

如果您还需要Interceptor成为bean,则可以创建自己的

LocalContainerEntityManagerFactoryBean
。在
EntityManagerFactoryBuilder
从spring引导1.1.4是有点过于严格,以与一般的属性,所以你需要转换为中
(Map)
,我们将看到固定,对1.2。

@Beanpublic 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();}@Beanpublic 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;        }    };}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/381990.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号