2017-02-03 60 views
0

我们有2个实体:A和B.Jpql加入多个

在列表中有一个@OneToMany。

两个实体都有一个日期。

我们希望获得A日期设置为null的实体,并用B填充日期设置为null的实体。

@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 

Example类:

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; 
} 

THX很多! :)

回答

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") 
+0

嗨,thx为你的回应,你对请求的权利,我编辑它,但对于你的回应, 如果a.bs.date为空且a.date不为空,那么将不会有结果。 但是我们需要一个空A的结果。 换句话说,我想在JPQL中使用此SQL查询: SELECT * FROM A a LEFT JOIN B b ON b.aId = a.id and b.date is null 其中a.date为空。 – user1829826