spring - 查询创建方法给出错误

标签 spring spring-boot spring-data-jpa jpql

我的实体有一个类似

的字段
@Column(name = "workflow_status", columnDefinition = "nvarchar(255)")
private String workflow_status;

因此,在我的 Controller 中,我想通过 workflow_status 获取条目,我编写了 JPA 提供的查询方法

List<Programme> findByWorkflow_status(String workflow_status);

但这会产生错误

Caused by: java.lang.IllegalArgumentException: Failed to create query method public abstract java.util.List com.taap.backend.programmes.repositories.ProgRepo.findByWorkflow_status(java.lang.String)! No property workflow found for type Programme!
at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:76) ~[spring-data-jpa-1.11.9.RELEASE.jar:na]
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:101) ~[spring-data-jpa-1.11.9.RELEASE.jar:na]
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:207) ~[spring-data-jpa-1.11.9.RELEASE.jar:na]
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:77) ~[spring-data-jpa-1.11.9.RELEASE.jar:na]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:451) ~[spring-data-commons-1.13.9.RELEASE.jar:na]
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:224) ~[spring-data-commons-1.13.9.RELEASE.jar:na]
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:277) ~[spring-data-commons-1.13.9.RELEASE.jar:na]
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:263) ~[spring-data-commons-1.13.9.RELEASE.jar:na]
at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:101) ~[spring-data-jpa-1.11.9.RELEASE.jar:na]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
... 48 common frames omitted
Caused by: org.springframework.data.mapping.PropertyReferenceException: No property workflow found for type Programme!
at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:77) ~[spring-data-commons-1.13.9.RELEASE.jar:na]
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:329) ~[spring-data-commons-1.13.9.RELEASE.jar:na]
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:309) ~[spring-data-commons-1.13.9.RELEASE.jar:na]
at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:272) ~[spring-data-commons-1.13.9.RELEASE.jar:na]
at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:243) ~[spring-data-commons-1.13.9.RELEASE.jar:na]
at org.springframework.data.repository.query.parser.Part.<init>(Part.java:76) ~[spring-data-commons-1.13.9.RELEASE.jar:na]
at org.springframework.data.repository.query.parser.PartTree$OrPart.<init>(PartTree.java:247) ~[spring-data-commons-1.13.9.RELEASE.jar:na]
at org.springframework.data.repository.query.parser.PartTree$Predicate.buildTree(PartTree.java:398) ~[spring-data-commons-1.13.9.RELEASE.jar:na]
at org.springframework.data.repository.query.parser.PartTree$Predicate.<init>(PartTree.java:378) ~[spring-data-commons-1.13.9.RELEASE.jar:na]
at org.springframework.data.repository.query.parser.PartTree.<init>(PartTree.java:89) ~[spring-data-commons-1.13.9.RELEASE.jar:na]
at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:70) ~[spring-data-jpa-1.11.9.RELEASE.jar:na]
... 58 common frames omitted

非常感谢任何帮助! :)

最佳答案

Spring JPA中的下划线(_)字符具有保留含义。

  • 或者更改变量的名称,强烈建议这样做。

改变

private String workflow_status;

private String workflowStatus;

List<Programme> findByWorkflow_status(String workflow_status);

List<Programme> findByWorkflowStatus(String workflow_status);
  • 或者,使用另一个下划线转义字符。

类似这样的事情:

List<Programme> findByWorkflow__status(String workflow_status);

有关更多信息,请参阅文档 here .

关于spring - 查询创建方法给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48768846/

相关文章:

java - 如何向 XML DOM 对象添加命名空间前缀?

java - Spring Pageable 不翻译@Column 名称

java - 如何使用类路径 : 通过 Liberty Profile 上的 application.properties 引用 Flyway.locations

spring-boot - Spring Webflux + JPA : Reactive Repositories are not supported by JPA

Spring Bean 继承使用注释 Bean 引用过刊

spring - SimpleMappingExceptionResolver-异常不显示给 View

mysql - 不同的移动计数在 spring 应用程序的 crudRepository 中不起作用

java - Springboot : Ignore null values in response optionally

java - 如何从部署在 PCF 上的 spring boot 应用程序连接到 Amazon RDS MySQL 数据库?

java - 使用 JPA 以编程方式在 Oracle 中创建序列