【发布时间】:2017-06-20 21:46:58
【问题描述】:
我们有 2 个实体:A 和 B。
一个列表上有一个@OneToMany。
两个实体都有一个日期。
我们想要获取日期设置为空的 A 实体,并用日期设置为空的 B 填充它。
@Query("select a from A a " +
"left join a.bs b " +
"on b.date is null " +
"where a.date is null")
我们想使用join是因为下面的请求,如果没有B,即使A有日期也不会显示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;
}
非常感谢! :)
【问题讨论】: