【问题标题】:Jpa does not recognize WHERE conditionJpa 无法识别 WHERE 条件
【发布时间】: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


    【解决方案1】:

    你想收到什么?您可以 LAZY 获取书籍并自定义序列化。

    如果使用 Jackson,您可以自定义不同视图所需的字段,请查找 JsonView 注释。

    您可能还对存储库端的Projections 感兴趣。

    【讨论】:

    • 我希望收到与 MySql Workbanch 相同的结果。具有给定时间戳的书籍的学生,而不是链接到该学生的所有书籍
    猜你喜欢
    • 2020-06-06
    • 1970-01-01
    • 2020-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多