2016-04-08 30 views
0

我猜想有點傻,但我不知道爲什麼它不起作用。我的目標是得到一個有孩子的父母名單(在一個請求中),其中兒童日期介於請求參數from和to之間。我得到正確的父對象,但是沒有用它來獲取子對象。jpa/hibernate noob:聯接抓取無法正常工作

實體

@Entity 
@Table(name = "parent") 
@Data 
public class Parent implements Serializable { 

... 

    @OneToMany(fetch = FetchType.EAGER, mappedBy = "parent") 
    private List<Child> children = new ArrayList<>(); 

@Query("select p from Parent p JOIN FETCH p.children child where " + 
     "(child.date between ?1 and ?2)") 
List<Parent> findCustom(@Param("from") @DateTimeFormat(iso = ISO.DATE) Date from, @Param("to") @DateTimeFormat(iso = ISO.DATE) Date to); 

更新

這裏是請求的結果

{ 
    "_embedded" : { 
    "parent" : [ { 
     "name" : "name", 
     "category" : "UNASSIGNED", 
     "_links" : { 
     "self" : { 
      "href" : "http://localhost:8080/json/parent/10" 
     }, 
     "parent" : { 
      "href" : "http://localhost:8080/json/parent/10{?projection}", 
      "templated" : true 
     }, 
     "children" : { 
      "href" : "http://localhost:8080/json/parent/10/children" 
     } 
     } 
    } ] 
    }, 
    "_links" : { 
    "self" : { 
     "href" : "http://localhost:8080/json/parent/search/findCustom?from=2016-01-14T07:35+0000&to=2017-01-14T07:35+0000" 
    } 
    } 
} 
+0

抓取連接時,你有'FetchType.EAGER'沒有做太多。這裏的實際問題是什麼?問題是什麼? – Tobb

+0

問題是我得到的只有沒有孩子的父母對象,我的目標是讓父母在其中封裝子對象 – user3287019

+0

那麼孩子列表是空的嗎?或者你只能得到有0個孩子的父母?無論哪種情況,與獲取連接無關。 – Tobb

回答

0
@Query("select p from Parent as p,Child as c " + 
     "where (c.date between ?1 and ?2)"+ 
     " and c.parent.id = p.id ") 

嘗試了這一點

+0

如果我使用這個查詢我有同樣的問題 - >沒有子對象在迴應中 – user3287019