java - 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.
解决方案
错误说没有ID
字段是Student
类,但您List<CourseStudent> findByStudentID
在存储库中使用。字段名称在这里区分大小写,ID
并且Id
不一样。
使用findByStudentId
代替findByStudentID
List<CourseStudent> findByStudentId (Long studentId);
推荐阅读
- logging - Undertow 访问日志问题
- sql - 正确的嵌套查询
- javascript - 通过根据位置比较对数组进行切片,随机抓取三个位置
- python - 在对熊猫数据框进行分组操作期间,为无法聚合的列保留数据的最佳方法是什么?
- javascript - 从 html 表单获取用户输入并将其写入文本、csv 或其他数据库文件(express.js、node.js、html)
- linux - 将更多文件的行数与文件名结合起来
- flutter - 处置后使用了 TextEditingController
- python - Python BeautifulSoup,如何更改下拉列表并获取 html?
- django - 将现有博客文章导入 django-cms-blog
- xcode - 如何在另一个视图控制器中更改标签文本