我们有两个实体:A和B.
A列表上有@OneToMany。
两个实体都有约会。
我们希望得到一个日期设置为null的实体,并用日期设置为null的B填充它。
@Query("select a from A a " +
"left join a.bs b " +
"on b.date is null " +
"where a.date is null")
我们想使用连接,因为如果没有B,即使A有日期,也会显示以下请求。
@Query("select a from A a where a.date is null and a.bs.date is null").
以下是用示例
解释的数据集A B1 B2 Result
date not set Date not set Date not set no result
date not set Date not set Date set no result
date not set Date set Date not set no result
date not set Date set Date set no result
date set Date not set Date not set A only
date set Date set Date not set A with B1
date set Date not set Date set A with B2
date set Date set Date set A with B1 + B2
示例类:
public class A {
@Id
private Long id;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "bId")
private List<B> bs;
}
public class B {
@Id
private Long id;
}
很多! :)
答案 0 :(得分:0)
第二个查询应该只是:
@Query("select a from A a where a.date is null and a.bs.date is null").
但这很奇怪,你的例子并不对应你的查询...... 我会写一些类似的东西:
@Query("select a from A where a.date is not null and a.bs.date is not null")