2017-02-03 55 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