弹簧数据JPA – “找不到types的属性”exception
好吧,我search了Google,发现了很多结果,但是没有一个能够回答我的问题。 所以,在这里呢。
我正在试图通过做一个pinterest克隆的最小实现来研究Spring MVC和Spring Data JPA。 所以,下面是我认为与我的问题有关的部分代码。
型号/实体
@Entity @Table(name = "pin_item") public class PinItem implements Serializable { // properties ... @JoinColumn(name = "board_id", referencedColumnName = "user_board_id") @ManyToOne(optional = false) private UserBoard board; // getters and setters... } @Entity @Table(name = "user_board") public class UserBoard implements Serializable { // properties ... @OneToMany(cascade = CascadeType.ALL, mappedBy = "board") private List<PinItem> pinItemList; // getters and setters... }
服务
@Service @Transactional(readOnly = true) public class BoardServiceImpl implements BoardService { @Autowired private UserBoardRepository boardRepository; @Override public List<UserBoard> findLatestBoards() { PageRequest request = new PageRequest( 0, PresentationUtil.PAGE_SIZE, Sort.Direction.DESC, "boardId" ); return boardRepository.findAll(request).getContent(); } // Other Methods }
知识库
public interface UserBoardRepository extends JpaRepository<UserBoard, Integer> { }
现在,当我调用findLatestBoards
中的findLatestBoards
方法时,在线return boardRepository.findAll(request).getContent();
抛出“找不到属性”exceptionreturn boardRepository.findAll(request).getContent();
。 这里是tomcat日志的摘录。
debugging日志
12:28:44,254 DEBUG AnnotationTransactionAttributeSource:106 - Adding transactional method 'findLatestBoards' with attribute: PROPAGATION_REQUIRED,ISOLATION_DEFAULT,readOnly; '' 12:28:44,254 DEBUG DefaultListableBeanFactory:246 - Returning cached instance of singleton bean 'transactionManager' 12:28:44,254 DEBUG JpaTransactionManager:366 - Creating new transaction with name [com.tecnooc.picpin.service.impl.BoardServiceImpl.findLatestBoards]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT,readOnly; '' 12:28:44,254 DEBUG JpaTransactionManager:369 - Opened new EntityManager [org.hibernate.ejb.EntityManagerImpl@75284194] for JPA transaction 12:28:44,255 DEBUG AbstractTransactionImpl:158 - begin 12:28:44,255 DEBUG LogicalConnectionImpl:212 - Obtaining JDBC connection 12:28:44,255 DEBUG DriverManagerDataSource:162 - Creating new JDBC DriverManager Connection to [jdbc:mysql://localhost:3306/pic_pin] 12:28:44,266 DEBUG LogicalConnectionImpl:218 - Obtained JDBC connection 12:28:44,267 DEBUG JdbcTransaction:69 - initial autocommit status: true 12:28:44,267 DEBUG JdbcTransaction:71 - disabling autocommit 12:28:44,267 DEBUG JpaTransactionManager:401 - Exposing JPA transaction as JDBC transaction [org.springframework.orm.jpa.vendor.HibernateJpaDialect$HibernateConnectionHandle@370da60e] 12:28:44,274 DEBUG TransactionalRepositoryProxyPostProcessor$CustomAnnotationTransactionAttributeSource:286 - Adding transactional method 'findAll' with attribute: PROPAGATION_REQUIRED,ISOLATION_DEFAULT,readOnly; '' 12:28:44,274 DEBUG DefaultListableBeanFactory:246 - Returning cached instance of singleton bean 'transactionManager' 12:28:44,274 DEBUG JpaTransactionManager:332 - Found thread-bound EntityManager [org.hibernate.ejb.EntityManagerImpl@75284194] for JPA transaction 12:28:44,274 DEBUG JpaTransactionManager:471 - Participating in existing transaction 12:28:44,279 DEBUG CachedIntrospectionResults:159 - Not strongly caching class [java.io.Serializable] because it is not cache-safe 12:28:44,281 DEBUG JpaTransactionManager:851 - Participating transaction failed - marking existing transaction as rollback-only 12:28:44,281 DEBUG JpaTransactionManager:559 - Setting JPA transaction on EntityManager [org.hibernate.ejb.EntityManagerImpl@75284194] rollback-only 12:28:44,283 DEBUG JpaTransactionManager:844 - Initiating transaction rollback 12:28:44,284 DEBUG JpaTransactionManager:534 - Rolling back JPA transaction on EntityManager [org.hibernate.ejb.EntityManagerImpl@75284194] 12:28:44,284 DEBUG AbstractTransactionImpl:203 - rolling back 12:28:44,284 DEBUG JdbcTransaction:164 - rolled JDBC Connection 12:28:44,285 DEBUG JdbcTransaction:126 - re-enabling autocommit 12:28:44,285 DEBUG JpaTransactionManager:594 - Closing JPA EntityManager [org.hibernate.ejb.EntityManagerImpl@75284194] after transaction 12:28:44,285 DEBUG EntityManagerFactoryUtils:338 - Closing JPA EntityManager 12:28:44,286 DEBUG LogicalConnectionImpl:232 - Releasing JDBC connection 12:28:44,286 DEBUG LogicalConnectionImpl:250 - Released JDBC connection 12:28:44,287 DEBUG ExceptionHandlerExceptionResolver:132 - Resolving exception from handler [public java.lang.String com.tecnooc.picpin.controller.BoardController.latest(javax.servlet.http.HttpSession,org.springframework.ui.Model)]: org.springframework.data.mapping.PropertyReferenceException: No property board found for type com.tecnooc.picpin.model.UserBoard 12:28:44,289 DEBUG ResponseStatusExceptionResolver:132 - Resolving exception from handler [public java.lang.String com.tecnooc.picpin.controller.BoardController.latest(javax.servlet.http.HttpSession,org.springframework.ui.Model)]: org.springframework.data.mapping.PropertyReferenceException: No property board found for type com.tecnooc.picpin.model.UserBoard 12:28:44,290 DEBUG DefaultHandlerExceptionResolver:132 - Resolving exception from handler [public java.lang.String com.tecnooc.picpin.controller.BoardController.latest(javax.servlet.http.HttpSession,org.springframework.ui.Model)]: org.springframework.data.mapping.PropertyReferenceException: No property board found for type com.tecnooc.picpin.model.UserBoard 12:28:44,291 DEBUG DispatcherServlet:959 - Could not complete request
例外
例外情况是“ org.springframework.data.mapping.PropertyReferenceException: No property board found for type com.tecnooc.picpin.model.UserBoard
”。 但是,如果我理解正确,属性board
存在于PinItem
,并正确映射到PinItem
mappedBy = "board"
。
org.springframework.data.mapping.PropertyReferenceException: No property board found for type com.tecnooc.picpin.model.UserBoard at 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.jpa.repository.query.QueryUtils.toJpaOrder(QueryUtils.java:408) at org.springframework.data.jpa.repository.query.QueryUtils.toOrders(QueryUtils.java:372) at org.springframework.data.jpa.repository.support.SimpleJpaRepository.getQuery(SimpleJpaRepository.java:456) at org.springframework.data.jpa.repository.support.SimpleJpaRepository.getQuery(SimpleJpaRepository.java:437) at org.springframework.data.jpa.repository.support.SimpleJpaRepository.findAll(SimpleJpaRepository.java:319) at org.springframework.data.jpa.repository.support.SimpleJpaRepository.findAll(SimpleJpaRepository.java:289) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:333) at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:318) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:96) at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:260) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:155) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) at org.springframework.data.jpa.repository.support.LockModeRepositoryPostProcessor$LockModePopulatingMethodIntercceptor.invoke(LockModeRepositoryPostProcessor.java:92) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:91) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204) at com.sun.proxy.$Proxy147.findAll(Unknown Source) at com.tecnooc.picpin.service.impl.BoardServiceImpl.findLatestBoards(BoardServiceImpl.java:45) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317) at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150) at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:96) at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:260) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204) at com.sun.proxy.$Proxy148.findLatestBoards(Unknown Source) at com.tecnooc.picpin.controller.BoardController.latest(BoardController.java:31) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219) at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132) at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686) at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936) at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827) at javax.servlet.http.HttpServlet.service(HttpServlet.java:621) at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812) at javax.servlet.http.HttpServlet.service(HttpServlet.java:728) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) at java.lang.Thread.run(Thread.java:744)
我不明白为什么会引发这个exception。 任何想法为什么发生?
注意:我使用Hibernate作为持久性提供者。 另外,我放在这里的代码部分是我认为与问题有关的部分。 如果不是,让我知道,我会更新所需的部分的问题。
我遇到了同样的问题,并find了解决办法在这里: http : //java.dzone.com/articles/persistence-layer-spring-data
我已经重新命名了一个实体属性。 但是在弹簧自动定制查询中,有一个为旧属性名称定义的接口。
public interface IFooDAO extends JpaRepository< Foo, Long >{ Foo findByOldPropName( final String name ); }
该错误表示它不能再find“OldPropName”并抛出exception。
引用DZone上的文章:
当Spring Data创build一个新的Repository实现时,它将分析接口定义的所有方法,并尝试从方法名称中自动生成查询。 虽然这有一定的局限性,但它是一个非常强大和优雅的定义新的自定义访问方法的方式。 例如,如果托pipe实体具有一个名称字段(以及该字段的Java Bean标准getter和setter),则在DAO接口中定义findByName方法将自动生成正确的查询:
public interface IFooDAO extends JpaRepository< Foo, Long >{ Foo findByName( final String name ); }
这是一个比较简单的例子。 查询创build机制支持更大的一组关键字。
如果parsing器无法将该属性与域对象字段匹配,则会引发以下exception:
java.lang.IllegalArgumentException: No property nam found for type class org.rest.model.Foo
你的命名不正确 。
根据文档 ,如果你的版本库是UserBoardRepository
,你的自定义版本库的实现应该被命名为UserBoardRepositoryImpl
,在这里你将它命名为BoardServiceImpl
,这就是为什么它会抛出exception。
修正了,在使用Spring的CrudRepository
时,我们必须在findBy后正确追加属性名,否则会给你exception“找不到types的属性”
我得到这个例外。 因为属性名称和方法名称不同步。
我使用下面的代码访问数据库。
public interface UserDao extends CrudRepository<User, Long> { User findByUsername(String username);
和我的域用户有财产。
@Entity public class User implements UserDetails { /** * */ private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "userId", nullable = false, updatable = false) private Long userId; private String username;
由于您的JPA存储库名称为UserBoardRepository ,因此您的自定义接口名称应为UserBoardRepositoryCustom (应以“Custom”结尾),而实现类名称应为UserBoardRepositoryImpl (应以Impl结尾;可使用不同的后缀使用存储库 – impl-postfix属性)
如果您尝试访问不存在的属性,则会发生此错误
我的猜测是sorting是由spring完成的property name
而不是real column name
。 错误表示在"UserBoard"
中没有名为"boardId"
属性。
最好成绩,
橡木
在我的情况下,我的方法名称中有一个错字(骆驼案例)。 我把它命名为“findbyLastName”并面对这个exception。 我把它改成“findByLastName”后,exception消失了。
在JPA中,一个关系有一个单独的所有者,通过在你的UserBoard
类中使用mappedBy
,你告诉PinItem
是双向关系的所有者,并且关系的PinItem
中的属性被命名为board
。
在你的UserBoard
类中,你没有任何字段/属性的名字board
,但它有一个属性pinItemList
,所以你可以尝试使用该属性来代替。
如果您的项目使用Spring-Boot,则可以尝试在Application.java中添加此注释。
@EnableJpaRepositories(repositoryFactoryBeanClass=CustomRepositoryFactoryBean.class) @SpringBootApplication public class Application {.....
请注意:Zane XY和Alan B. Dee的答案相当不错。 但对于那些现在要使用Spring Boot和Spring Data的人来说,这将是一个更现代的答案。
假设你有一个类如:
@Entity class MyClass { @Id @GeneratedValue private Long id; private String myClassName; }
现在一个JpaRepository
为这个样子
interface MyClassRepository extends JpaRepository { Collection<MyClass> findByMyClassName(String myClassName); }
现在你的“custom” find方法必须精确地拼写Collection<MyClass> findByMyClassName(String myClassName)
因为Spring需要有一些机制来把这个方法映射到MyClass
属性myClassName
!
我明白了这一点,因为对我来说,在语义上 按名称查找类似乎很自然,而事实上, 通过myClassName
干杯