gpt4 book ai didi

java - JPQL 查询子页面和孙子的连接获取会抛出异常查询指定的连接获取

转载 作者:行者123 更新时间:2023-12-02 01:33:06 24 4
gpt4 key购买 nike

我有实体:

class Parent {
...
@OneToMany(Fetch = FetchType.EAGER)
@OrderBy("id")
private List<Child> children;
}


class Child{
...
@OneToMany(Fetch = FetchType.LAZY)
@OrderBy("id")
private List<GrandChild> grandChildren;
}

class GrandChild {
...
}

我想查询分页的子项并获取孙子项。我还按名字过滤 child ,但我认为这并不重要。我有这样的 JPQL 查询:

@Query("select c " +
"from Parent p " +
"inner join p.children c " +
"inner join fetch c.grandchildren g " +
"where p = :parent and " +
"c.childName like concat(:childName,'%')")
Page<Expense> findBy(@Param("parent") Parent parent, @Param("childName")String childName, Pageable pageable);

在启动 Spring Boot 应用程序期间抛出异常:

query specified join fetching, but the owner of the fetched association was not present in the select list

消息很简单,但是我无法将父级添加到选择语句,因为我只想要子级。

最佳答案

c.grandchildren 缺少别名 e

@Query("select e " +
"from Parent p " +
"inner join p.children c " +
"inner join fetch c.grandchildren e" +
"where p = :parent and " +
"c.childName like concat(:childName,'%')")
Page<Expense> findBy(@Param("parent") Parent parent, @Param("childName")String childName, Pageable pageable);

关于java - JPQL 查询子页面和孙子的连接获取会抛出异常查询指定的连接获取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55727390/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com