我有Spring Boot
应用程序。在应用程序中存在实体:
@NamedEntityGraph(
name = "paragraph-graph-questions",
attributeNodes = {
@NamedAttributeNode(value = "questions")
})
@Entity
@Table(name = "paragraphs")
public class Paragraph {
@Id
private Integer id;
@OneToMany(mappedBy = "paragraph")
private Set<Question> questions;
@OneToMany
@JoinTable(
name = "sub_paragraphs",
joinColumns = @JoinColumn(name = "paragraph_id"),
inverseJoinColumns = @JoinColumn(name = "sub_paragraph_id")
)
private Set<Paragraph> subParagraphs;
}
特点是Paragraph
实体与其他段落具有反身的OneToMany关系。我制作了 JpaRepository 以便获得该段落的子段落:
@Transactional(readOnly = true)
public interface ParagraphDao extends JpaRepository<Paragraph, Integer> {
@EntityGraph(value = "paragraph-graph-questions")
@Query("SELECT sp FROM Paragraph p JOIN p.subParagraphs sp WHERE p.id=:id")
Set<Paragraph> getAllSubParagraphs(int id);
}
但是如果我使用@EntityGraph
这个方法,调用这个方法会抛出异常,否则方法工作正常。这是我在使用时得到的异常堆栈跟踪@EntityGraph
:
org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present in the select list [FromElement{explicit,not a collection join,fetch join,fetch non-lazy properties,classAlias=sp,role=ru.zhenyria.deeptest.model.task.Paragraph.subParagraphs,tableName=paragraphs,tableAlias=paragraph2_,origin=paragraphs paragraph0_,columns={paragraph0_.id,className=ru.zhenyria.deeptest.model.task.Paragraph}}]
我该如何解决?谢谢。