首页 > 解决方案 > Spring boot - 找不到用户类型的属性 ID

问题描述

好吧,我搜索了谷歌,发现了很多结果,但没有一个能够回答我的问题。所以,就这样吧。

我正在尝试通过对 pinterest 克隆的最小实现来研究 Spring MVC 和 Spring Data JPA。因此,以下是我认为与我的问题相关的代码部分。

存储库类:

public interface CourseStudentRepository extends JpaRepository <CourseStudent, Long> {

    List<CourseStudent> findByCourseInstructorId(Long instructorId);

    List<CourseStudent> findByStudentID (Long studentId);
}

实体类:

@Data
@Entity
@Table(name = "course_student")
public class CourseStudent implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "student_id", referencedColumnName = "id")
    private User student;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "course_id", referencedColumnName = "id")
    private Course course;

}

错误:

Failed to create query for method public abstract java.util.List com.sha.serverside.repository.CourseStudentRepository.findByStudentID(java.lang.Long)! No property ID found for type User! Did you mean 'id'? Traversed path: CourseStudent.student.

标签: javaspringspring-bootspring-data-jpa

解决方案


错误说没有ID字段是Student类,但您List<CourseStudent> findByStudentID在存储库中使用。字段名称在这里区分大小写,ID并且Id不一样。

使用findByStudentId代替findByStudentID

List<CourseStudent> findByStudentId (Long studentId);

推荐阅读