【发布时间】:2021-12-01 22:39:51
【问题描述】:
我有两张桌子
class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
String name
@OneToMany(mappedBy = "student", cascade = CascadeType.ALL)
List<Book> books
}
class Book {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
String name
String time_stamp;
@ManyToOne(fetch = FetchType.LAZY)
@JsonIgnore
private Student student;
}
所以在数据库中,我有例如:
学生桌
| id | name |
|---|---|
| 1 | John |
桌书
| id | name | time_stamp | studentId |
|---|---|---|---|
| 1 | Bla | 12:00 | 1 |
| -- | ---- | ---------- | ---------- |
| 2 | Test | 12:45 | 1 |
@Query(value = "SELECT * FROM student LEFT JOIN Book ON student.id = book.student.id WHERE book.time_stamp >= '12:00' AND book.time_stamp <= '12:00'", nativeQuery = true)
List<Student> findFromTo();
如果我们在 MySql Workbench 中运行查询,一切正常,我们只会收到给定时间戳的学生和书籍。
当我们通过 spring JPA 运行查询时会出现问题,我们在此时间戳之间收到 Student,但所有书籍都链接到 Student。
我想在 jpa Student 对象中接收带有时间戳过滤的 Books 对象。
【问题讨论】:
标签: sql spring-boot spring-data-jpa