Spring Data Jpa 自定义 Repository EntityManager is null
来源:互联网 发布:网络专供款烟机 编辑:程序博客网 时间:2024/06/08 08:11
项目中升级了spring-data-jpa版本,发现继承了 QueryDslRepositorySupport 的自定义的Repository类一直报:
Caused by: java.lang.IllegalArgumentException: EntityManager must not be null! at org.springframework.util.Assert.notNull(Assert.java:134) ~[spring-core-4.3.12.RELEASE.jar:4.3.12.RELEASE] at org.springframework.data.jpa.repository.support.QueryDslRepositorySupport.validate(QueryDslRepositorySupport.java:75) ~[spring-data-jpa-1.11.8.RELEASE.jar:na] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_121] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_121] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_121] at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_121] at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:366) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE] at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:311) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE] at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:134) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE] ... 23 common frames omitted
旧版本为: 1.11.4.RELEASE
新版本为: 1.11.8.RELEASE
对比源代码:
1.11.4:
/** * Setter to inject {@link EntityManager}. * * @param entityManager must not be {@literal null} */ @PersistenceContext public void setEntityManager(EntityManager entityManager) { Assert.notNull(entityManager, "EntityManager must not be null!"); this.querydsl = new Querydsl(entityManager, builder); this.entityManager = entityManager; }
1.11.8:
/** * Setter to inject {@link EntityManager}. * * @param entityManager must not be {@literal null}. */ public void setEntityManager(EntityManager entityManager) { Assert.notNull(entityManager, "EntityManager must not be null!"); this.querydsl = new Querydsl(entityManager, builder); this.entityManager = entityManager; }
新版本中去除了 @PersistenceContext 注解, 无法自动进行注入, 应该是新版本为了支持微服务, 表分库策略, 可以使用多个数据源, 多个EntityManager, 故移除自动注入, 改为手动注入:
@Autowired public CustomerRepositoryImpl(JpaContext context){ super(Customer.class); setEntityManager(context.getEntityManagerByManagedType(Follow.class)); }
阅读全文
0 0
- Spring Data Jpa 自定义 Repository EntityManager is null
- Spring Data jpa Repository
- Spring Data JPA Repository
- Spring Data Jpa ---- repository
- Spring技术内幕之Spring Data JPA-自定义Repository实现
- Spring Data JPA: 为所有Repository添加自定义方法
- Spring Data JPA: 为单个Repository添加自定义方法
- Spring Data JPA: 为所有Repository添加自定义方法
- 测试Spring Data JPA的repository
- 初识Spring Data与JPA Repository
- 利用AOP为Spring Data Jpa的接口Repository添加全局自定义过滤
- spring data jpa 自定义接口
- Spring Data JPA 自定义查询
- spring boot data-jpa 放回null
- jdbc hibernate(session) jpa (entityManager ) spring-data-jpa mybatis完全混用
- Spring data 接口之 自定义Repository 接口
- Spring Data JPA Entity Repository 多路径配置
- Spring Data JPA(1)--Repository和CrudRepository接口
- windows远程服务器发送ctrl+alt+delete组合登录
- MyServlet总结
- 简单粗暴的入门机器学习
- Wordpress插件开发过程
- 在几年后,程序员的薪资会一直这么高么?
- Spring Data Jpa 自定义 Repository EntityManager is null
- 在Centos 7 上 搭建 K8S --坑a)
- typeScript 装饰器
- 2017.11.7作业
- bash脚本中的单引号双引号与反引号
- C语言知识点
- 简单排序算法:直接插入排序(插入排序)
- Spring-data-redis操作redis知识总结
- Generative Adversarial Network (GAN) papers (不定期更新)