【问题标题】:spring Data Jpa findFirst is failing when the method parameter data type is integer当方法参数数据类型为整数时,spring Data Jpa findFirst 失败
【发布时间】:2021-01-12 18:02:35
【问题描述】:

我正在使用 spring-data-jpa 并且在我的 JPARepository 界面中我使用了“findFirst”功能,但是当我的存储库方法具有 Integer 参数时它会失败.

存储库:

public interface EmployeeRepository extends
    JpaRepository<Employee, Integer> {

Employee findFirstByAgeOrderByEmployeeId(int age);

}

实体类:

@Entity
Employee{

@Id
int employeeId;
int age;
String name;

}

查询生成如下

select e.employeeId, e.age, e.name from Employee e where e.age = ? Order by e.employeeId limit ?

出现以下错误

org.springframework.dao.InvalidDataAccessApiUsageException:对于 带有您需要使用的命名参数的查询为方法提供名称 参数。使用 @Param 查询方法参数,或者在 Java 8+ 上使用 使用 javac 标志 - 参数。嵌套异常是 java.lang.IllegalStateException:对于带有命名参数的查询,您 需要为方法参数使用提供名称。使用@Param 进行查询 方法参数,或者在 Java 8+ 上使用 javac 标志 -parameters。

【问题讨论】:

    标签: spring-boot jpa spring-data-jpa


    【解决方案1】:

    也许您的查询不好:findFirstByAge"Oderder"ByEmployeeId 试试“订购”。

    并且使用对象而不是原始对象(Integer 代替 int),有时 hibernate 使用 getter 和 setter 来访问数据。

    【讨论】:

    • 这是错字问题 Alejandrao/
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-06
    • 1970-01-01
    • 1970-01-01
    • 2012-05-21
    • 1970-01-01
    • 1970-01-01
    • 2012-08-31
    相关资源
    最近更新 更多