jpa / hibernate noob:join fetch无法正常工作

时间:2016-04-08 08:36:52

标签: java spring hibernate jpa spring-data-rest

我想有点愚蠢的问题,但我不知道为什么它不起作用。我的目标是获得一个有孩子的父母列表(在一个请求中),其中孩子的日期在请求参数之间和之间。我得到了正确的父对象,但是没有使用子对象获取子对象。

实体

@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"
    }
  }
} 

1 个答案:

答案 0 :(得分:0)

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

试试这个

相关问题