【发布时间】:2016-04-08 08:36:52
【问题描述】:
我猜是一个有点傻的问题,但我不知道为什么它不起作用。我的目标是获取有孩子的父母列表(在一个请求中),其中孩子的日期介于请求参数 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"
}
}
}
【问题讨论】:
-
当您拥有
FetchType.EAGER时,Fetch join 并没有多大作用。这里的实际问题是什么?有什么问题? -
问题是我只得到没有子对象的父母,我的目标是让父母在其中封装了子对象
-
所以孩子列表是空的?或者你只得到有 0 个孩子的父母?无论哪种情况,都与 fetch join 无关。
-
我添加了我当前的回复。所以我得到了正确的父母(有孩子,日期在“从”和“到”之间),但子对象没有被封装
-
您应该进行调试以确定是 JPA 查询返回了错误,还是创建了 json..
标签: java spring hibernate jpa spring-data-rest