bigywater.tech
Spring · 183

PropertyReferenceException:派生查询方法名写错的几种报错,以及 @Query 写错属性名是另一种异常

No property xxx found for type 是什么错?findByIsActive、findByAddressCty、大小写写错各报什么?@Query 里写错属性名是不是同一种异常?这篇在 Spring Boot 4.0.8 上逐个启动。

启动即失败
派生查询方法名里的属性写错,应用启动失败,不是调用时才出错;同一个 Repo 里写对的方法也跟着用不了
Did you mean
findByNme 的信息里直接给出了相近属性 name、age;findByfirstname 给出 firstName;findByIsActive 没有建议
IncorrectResultSize
方法名对、返回单个 Person 或 Optional,库里有两个同名的人时调用抛 IncorrectResultSizeDataAccessException

少个字母

实测 · 本机执行输出
  启动失败。异常链:PropertyReferenceException → QueryCreationException → BeanCreationException
  最里面一层的信息:No property 'nme' found for type 'Person'; Did you mean 'name','age'

No property 'nme' found,带 Did you mean 建议。

findByIsActive

实测 · 本机执行输出
  启动失败。异常链:PropertyReferenceException → QueryCreationException → BeanCreationException
  最里面一层的信息:No property 'isActive' found for type 'Person'

is 不是 getter 前缀,无建议。

嵌套属性

实测 · 本机执行输出
  启动失败。异常链:PropertyReferenceException → QueryCreationException → BeanCreationException
  最里面一层的信息:No property 'cty' found for type 'Address'; Did you mean 'city'; Traversed path: Person.address

报错带出 Traversed path。

参数个数

实测 · 本机执行输出
  启动失败。异常链:IllegalStateException → QueryCreationException → BeanCreationException
  最里面一层的信息:Method expects at least 2 arguments but only found 1; This leaves an operator of type 'SIMPLE_PROPERTY' for property 'age' unbound

IllegalStateException。

大小写

实测 · 本机执行输出
  启动失败。异常链:PropertyReferenceException → QueryCreationException → BeanCreationException
  最里面一层的信息:No property 'firstname' found for type 'Person'; Did you mean 'firstName'

属性名区分大小写。

多于一个结果

实测 · 本机执行输出
  启动成功
  Person findByName → IncorrectResultSizeDataAccessException:Query did not return a unique result: 2 results were returned
  Optional findOneByName → IncorrectResultSizeDataAccessException:Query did not return a unique result: 2 results were returned

调用时才抛 IncorrectResultSizeDataAccessException。

@Query 属性名

实测 · 本机执行输出
  启动失败。异常链:PathElementException → UnknownPathException → IllegalArgumentException → QueryCreationException → BeanCreationException
  最里面一层的信息:Could not resolve attribute 'nme' of 'demo.c7.Person'

Hibernate 的 PathElementException。

@Query 实体名

实测 · 本机执行输出
  启动失败。异常链:UnknownEntityException → IllegalArgumentException → QueryCreationException → BeanCreationException
  最里面一层的信息:Could not resolve root entity 'Persn'

UnknownEntityException。

分步演示

PropertyReference
本步的对象

    本文用到的工具与边界

    • 本机 macOS 27 arm64 / OpenJDK 21.0.12(Homebrew) / Spring Boot 4.0.8(Spring Framework 7.0.9、Tomcat 11.0.24) / Spring Cloud 2025.1.3;用 javac 编译、java 直接运行,依赖由 Maven 从 Maven Central 解析;数字每次运行会有小幅抖动(Spring Boot 4.0.8,Spring Data JPA 4.0.7,Hibernate 7.2,H2 内存库;每个情形是 demo.cN 包里一个 Person 实体和一个 JpaRepository,由 SpringApplication 启动;异常链由程序沿 getCause 打印)
    • 没有验证:Spring Data JDBC、MongoDB 里同样写法的报错;@Query 的 nativeQuery 写错列名的报错;@NamedQuery 的启动校验;spring.data.jpa.repositories.bootstrap-mode=lazy 时错误出现的时机;IDE 对方法名的补全;findFirstBy 与 findTopBy 在多结果时的行为;排序属性 OrderBy 写错的报错
    京ICP备2026067502号-1京公网安备11010502063487号