给定以下域模型,我想加载所有Answers,包括它们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。
