spring data jpa的误将方法名当成属性名称的问题

问题描述

项目中使用了spring data jpa,自己扩展了一个全局接口AbstractJxetRepositorypublic interface AbstractJxetRepository<T> {public void batchInster(List<T> list, int size) throws Exception;public void batchUpdate(List<T> list, int size) throws Exception;}实现类AbstractDao@NoRepositoryBeanpublic abstract class AbstractDao<T> implements AbstractJxetRepository<T> {@PersistenceContextprotected EntityManager em;@Transactionalpublic void batchInster(List<T> list, int size) throws Exception{for (int i = 0; i < list.size(); i++) {em.persist(list.get(i));if (i % size == 0) {em.flush();em.clear();}}}@Transactionalpublic void batchUpdate(List<T> list, int size) {for (int i = 0; i < list.size(); i++) {em.merge(list.get(i));if (i % size == 0) {em.flush();em.clear();}}}}在一个模块接口中继承AbstractJxetRepositorypublic interface EduUnitLevelDao extends PagingAndSortingRepository<EduUnitLevel, Integer>,JpaSpecificationExecutor<EduUnitLevel> {public List<EduUnitLevel> getBySchoolId(Integer schoolId);@SuppressWarnings("unchecked")public EduUnitLevel save(EduUnitLevel eduUnitLevel);public List<EduUnitLevel> save(List<EduUnitLevel> eduUnitLevels);现在的问题是,在service中调用EduUnitLevelDao ,会将batchInster方法中的batch当做EduUnitLevel的属性,提示No property batch found for type cn.com.jxet.datalayer.entity.EduUnitLevel全部的报错信息org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cn.com.jxet.datalayer.service.EduUnitLevelServiceTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private cn.com.jxet.datalayer.service.EduUnitLevelService cn.com.jxet.datalayer.service.EduUnitLevelServiceTest.eduUnitLevelService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'eduUnitLevelService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void cn.com.jxet.datalayer.service.EduUnitLevelService.setEduUnitLevelDao(cn.com.jxet.datalayer.dao.EduUnitLevelDao); nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'eduUnitLevelDao': FactoryBean threw exception on object creation; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property batch found for type cn.com.jxet.datalayer.entity.EduUnitLevelat org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:288)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1122)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:379)at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:110)at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:312)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:288)at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:284)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)at org.junit.runners.ParentRunner.run(ParentRunner.java:236)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private cn.com.jxet.datalayer.service.EduUnitLevelService cn.com.jxet.datalayer.service.EduUnitLevelServiceTest.eduUnitLevelService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'eduUnitLevelService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void cn.com.jxet.datalayer.service.EduUnitLevelService.setEduUnitLevelDao(cn.com.jxet.datalayer.dao.EduUnitLevelDao); nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'eduUnitLevelDao': FactoryBean threw exception on object creation; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property batch found for type cn.com.jxet.datalayer.entity.EduUnitLevelat org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:514)at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285)... 26 moreCaused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'eduUnitLevelService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void cn.com.jxet.datalayer.service.EduUnitLevelService.setEduUnitLevelDao(cn.com.jxet.datalayer.dao.EduUnitLevelDao); nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'eduUnitLevelDao': FactoryBean threw exception on object creation; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property batch found for type cn.com.jxet.datalayer.entity.EduUnitLevelat org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:288)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1122)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:522)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:910)at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:853)at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:768)at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:486)... 28 moreCaused by: org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void cn.com.jxet.datalayer.service.EduUnitLevelService.setEduUnitLevelDao(cn.com.jxet.datalayer.dao.EduUnitLevelDao); nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'eduUnitLevelDao': FactoryBean threw exception on object creation; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property batch found for type cn.com.jxet.datalayer.entity.EduUnitLevelat org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:601)at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285)... 39 moreCaused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'eduUnitLevelDao': FactoryBean threw exception on object creation; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property batch found for type cn.com.jxet.datalayer.entity.EduUnitLevelat org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:149)at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:102)at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1448)at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:249)at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:910)at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:853)at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:768)at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:558)... 41 moreCaused by: org.springframework.data.mapping.PropertyReferenceException: No property batch found for type cn.com.jxet.datalayer.entity.EduUnitLevelat org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:75)at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:327)at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:353)at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:307)at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:271)at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:245)at org.springframework.data.repository.query.parser.Part.<init>(Part.java:72)at org.springframework.data.repository.query.parser.PartTree$OrPart.<init>(PartTree.java:180)at org.springframework.data.repository.query.parser.PartTree$Predicate.buildTree(PartTree.java:260)at org.springframework.data.repository.query.parser.PartTree$Predicate.<init>(PartTree.java:240)at org.springframework.data.repository.query.parser.PartTree.<init>(PartTree.java:68)at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:57)at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:90)at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:162)at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:68)at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:279)at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:147)at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:153)at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:43)at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:142)... 49 more 问题补充:模块接口贴错了,应该是public interface EduUnitLevelDao extends PagingAndSortingRepository<EduUnitLevel, Integer>,JpaSpecificationExecutor<EduUnitLevel>,AbstractJxetRepository<EduUnitLevel> {public List<EduUnitLevel> getBySchoolId(Integer schoolId);@SuppressWarnings("unchecked")public EduUnitLevel save(EduUnitLevel eduUnitLevel);public List<EduUnitLevel> save(List<EduUnitLevel> eduUnitLevels);}

解决方案

No property batch found for type cn.com.jxet.datalayer.entity.EduUnitLevel 1、spring data jpa 没识别你这个东西 public void batchInster(List<T> list, int size) throws Exception; public void batchUpdate(List<T> list, int size) throws Exception; 2、你这么写不对的public abstract class AbstractDao<T> implements AbstractJxetRepository<T> { 3、注册有两种方式:3.1、如果单个模块需要UserRepository2.java 接口UserRepository2Impl.java 实现https://github.com/zhangkaitao/es/tree/master/common/src/test/java/com/sishuok/es/common/repository3.2、如果所有模块都需要:注册方式 <jpa:repositories base-package="com.sishuok.es.**.repository" repository-impl-postfix="Impl" factory-class="com.sishuok.es.common.repository.support.SimpleBaseRepositoryFactoryBean" entity-manager-factory-ref="entityManagerFactory" transaction-manager-ref="transactionManager"> </jpa:repositories>https://github.com/zhangkaitao/es/blob/master/web/src/main/resources/spring-config.xml

时间: 2024-10-21 13:58:04

spring data jpa的误将方法名当成属性名称的问题的相关文章

Spring Data JPA方法定义规范【从零开始学Spring Boot】

视频&交流平台] à SpringBoot网易云课堂视频 http://study.163.com/course/introduction.htm?courseId=1004329008 à Spring Boot交流平台 http://412887952-qq-com.iteye.com/blog/2321532           事情的起因:有人问过我们这个这个问题:为什么我利用Spring data jpa写的方法没有按照我想要的情况进行执行呢?我记得当时只是告诉他你你先看看Spring

springboot(五):spring data jpa的使用

在上篇文章springboot(二):web综合开发中简单介绍了一下spring data jpa的基础性使用,这篇文章将更加全面的介绍spring data jpa 常见用法以及注意事项 使用spring data jpa 开发时,发现国内对spring boot jpa全面介绍的文章比较少案例也比较零碎,因此写文章总结一下.本人也正在翻译Spring Data JPA 参考指南,有兴趣的同学欢迎联系我,一起加入翻译中! spring data jpa介绍 首先了解JPA是什么? JPA(Ja

8.3. Spring Data JPA

8.3.1. @Entity 8.3.1.1. @Id ID 字段 @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id", unique = true, nullable = false, insertable = true, updatable = false) private int id; 8.3.1.2. @Table 8.3.1.2.1. catalog @Table(name=

spring boot(五):spring data jpa的使用

在上篇文章springboot(二):web综合开发中简单介绍了一下spring data jpa的基础性使用,这篇文章将更加全面的介绍spring data jpa 常见用法以及注意事项 使用spring data jpa 开发时,发现国内对spring boot jpa全面介绍的文章比较少案例也比较零碎,因此写文章总结一下.本人也正在翻译Spring Data JPA 参考指南,有兴趣的同学欢迎联系我,一起加入翻译中! spring data jpa介绍 首先了解JPA是什么? JPA(Ja

5.3. Spring Data JPA

5.3.1. @Entity 5.3.1.1. @Id ID 字段 @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id", unique = true, nullable = false, insertable = true, updatable = false) private int id; 5.3.1.2. @Table 5.3.1.2.1. catalog @Table(name=

spring data jpa-学习Spring data jpa遇到了这个问题,求大家帮帮忙

问题描述 学习Spring data jpa遇到了这个问题,求大家帮帮忙 SEVERE: StandardWrapper.Throwable org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaMapppingContext': Invocation of init method failed; nested exception is java.lang.NoSuch

spring data jpa 怎么处理多个表的查询呢?

问题描述 spring data jpa 怎么处理多个表的查询呢? 有什么好的方法来应对多个表之间复杂的连接查询? .

spring data jpa 的一些疑问

问题描述 http://www.ibm.com/developerworks/cn/opensource/os-cn-spring-jpa/<使用 Spring Data JPA 简化 JPA 开发>这篇文章相信大家一定拜读过 但是这2天我在继续试用spring data jpa中遇到了不少挫折. 首先遇到的是原文中写到"默认情况下,Spring Data JPA 实现的方法都是使用事务的.针对查询类型的方法,其等价于 @Transactional(readOnly=true):增删

spring data jpa + spring mvc的事务控制问题

问题描述 spring data jpa + spring mvc的事务控制问题 问题大概是这样的,如下@Transactionalpublic void save(A a){ a = aRepository.save(a); B b = new B();b.setAId(a.getId());bRepository.save(b); }这样是获取不到a对象的id的,因为方法没有执行完,这个事务没有提交,数据没有更新到数据库,请问如果我想在save之后通过返回的对象就能拿到主键需要怎么做,事务是