给定以下域模型,我想加载所有Answer
s,包括它们Value
的 s 和它们各自的子子项,并将其放入 anAnswerDTO
中,然后转换为 JSON。我有一个可行的解决方案,但它遇到了我想通过使用 ad-hoc 摆脱的 N+1 问题@EntityGraph
。所有关联均已配置LAZY
。
@Query("SELECT a FROM Answer a")
@EntityGraph(attributePaths = {"value"})
public List<Answer> findAll();
@EntityGraph
在方法上使用 ad-hocRepository
我可以确保预先获取值以防止 N+1 出现Answer->Value
关联。虽然我的结果很好,但由于延迟加载 s 的selected
关联,还有另一个 N+1 问题MCValue
。
使用这个
@EntityGraph(attributePaths = {"value.selected"})
失败,因为该selected
字段当然只是某些Value
实体的一部分:
Unable to locate Attribute with the the given name [selected] on this ManagedType [x.model.Value];
我如何告诉 JPA 仅selected
在值为 a 的情况下尝试获取关联MCValue
?我需要类似的东西optionalAttributePaths
。