【发布时间】:2017-10-06 11:36:34
【问题描述】:
在两个实体之间的双向关系(由 ControlSteps 组成的 ControlTable)中,我只是通过了解 ControlSteps 的集合来尝试通过不同的方式来请求 ControlTable。我知道不建议使用这种双向映射,但我需要知道父母的每个孩子,以及每个孩子的父母。
我在 ControlTable 类中是这样配置的:
@OneToMany(mappedBy = "controlTable",cascade = CascadeType.ALL, fetch=FetchType.EAGER)
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Fetch(FetchMode.JOIN)
private Set<ControlStep> controlSteps;
对于 ControlStep 类也是这样:
@ManyToOne(optional=false, fetch=FetchType.LAZY)
@JoinColumn(name="ctrl_table_id", referencedColumnName = "id")
private ControlTable controlTable;
当我使用默认的 JPA 查询 findAll() 时,它无法获取 ControlTables(或仅一个)的列表,因为它递归地请求子父级中的父级(无限响应)。 在另一种方式中,尝试将所有内容都放入 LAZY 加载中,使用 HQL 查询获取子项,但结果是一样的。
您知道如何毫无问题地获得这些收藏吗?
非常感谢您
【问题讨论】:
-
仅供参考 JPA API 没有这样的“findAll”。 Spring Data JPA (!= JPA API) 确实有这样的方法
-
你说得对,我错过了提及“Spring 数据”。对不起;)
标签: java spring hibernate spring-data-jpa jpql