繁体   English   中英

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

[英]jpa/hibernate noob: join fetch does not work properly

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

实体

@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"
    }
  }
} 
@Query("select p from Parent as p,Child as c " +
        "where (c.date between ?1 and ?2)"+
         " and c.parent.id = p.id " )

试试这个

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM