Spring Data Jpa 关于fetch join 的错误

spring data jpa 文档的官方网站:

https://docs.spring.io/spring-data/jpa/docs/current/reference/html/


在开发中使用到 Jpa Dao 方法时,出现如下错误:

fetch join...

but the owner of the fetched association was not present in the select list。。。


原因是和使用了 left join fetch 关键字有关,可以理解为使用 join 查询的表,并没有出现在 select 关键字之后。

就是说查询的结果集中没有直接关联到join表的属性。


我的真实情况如下:

写成如下,正常编译

@Query("SELECT o FROM Order o left join fetch o.orderStatus where o.creation between ?1 and ?2 and o.removed=?3")
List<Order> findByCreationBetweenAndRemoved(Date beginDate, Date endDate, boolean b);


改为如下,则出现上述所说的错误

@Query("SELECT o.id FROM Order o left join fetch o.orderStatus where o.creation between ?1 and ?2 and o.removed=?3")
List<Long> findByCreationBetweenAndRemoved(Date beginDate, Date endDate, boolean b);

原因就是 left join fetch 后面的o.orderStatus 对应的实体表,并没有在o.id中出现。


修改比较简单,

1,返回值用包含join表的实体接受

2,去掉left join fetch关联



猜你喜欢

转载自blog.csdn.net/u013276512/article/details/78540368