在我的 Spring Boot/JPA(搭配 Postgres)项目中,
我有一个名为“projects”的表,定义如下:
create table projects(
project_id int primary key,
name varchar(255),
description text
);
在我的控制器中,我有一个获取项目列表的方法:
@GetMapping("/list")
@ResponseBody
public Page<Project> list(@AuthenticationPrincipal UserDetails currentUserDetails) {
int page = 0;
int size = 100;
Sort sort = Sort.by("name").ascending();
PageRequest p = PageRequest.of(page, size, sort);
return projectService.list(p);
}
这段代码能够正常运行。
问题出现在当我更改代码以按照 project_id
列进行排序时:
Sort sort = Sort.by("project_id").ascending();
此时,运行时会抛出以下异常:
2024-01-01T14:34:04,424 ERROR [http-nio-8081-exec-6] o.a.j.l.DirectJDKLog: Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: org.springframework.data.mapping.PropertyReferenceException: No property 'project' found for type 'Project'] with root cause
org.springframework.data.mapping.PropertyReferenceException: No property 'project' found for type 'Project'
at org.springframework.data.mapping.PropertyPath.(PropertyPath.java:90)
我的 Project 实体类非常简单:
@Entity
@Table(name = "Projects")
public class Project {
@Id
@GeneratedValue(generator = "ProjectSeq")
@SequenceGenerator(name = "ProjectSeq", sequenceName = "PROJECTS_PROJECT_ID_SEQ", allocationSize = 1)
Integer project_id;
String name;
public Integer getProject_id() {
return project_id;
}
public void setProject_id(Integer project_id) {
this.project_id = project_id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
String description;
}
是否因为我遇到了一个保留字的问题?