是否可以在Spring Data Jpa中使用@QueryHint指定javax.persistence.fetchgraph或javax.persistence.loadgraph?
我有一个实体图
@Entity
@NamedEntityGraph(
name = "shop_with_all_associations",
includeAllAttributes = true
)
public class Shop {
@JoinColumn(name = "shop_id")
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private List<Member> members = new ArrayList<>();
}以及存储库中相应的方法
@Repository
public interface ShopRepository extends JpaRepository<Shop, Integer> {
@QueryHints({@QueryHint(name = "javax.persistence.fetchgraph", value = "shop_with_all_associations")})
List<Shop> getAllWithQueryHintBy();
}发布于 2021-05-22 12:59:51
不,这不可能。您可以检查日志,并在调用该方法时查看下一个警告
o.h.q.internal.AbstractProducedQuery : The javax.persistence.fetchgraph hint was set, but the value was not an EntityGraph!在AbstractProducedQuery类中,下一个代码部分显示,只有RootGraph的实例可以被视为EntityGraph。但是我们的@QueryHint只允许将字符串作为值传递。
else if ( HINT_FETCHGRAPH.equals( hintName ) || HINT_LOADGRAPH.equals( hintName ) ) {
if ( value instanceof RootGraph ) {
applyGraph( (RootGraph) value, GraphSemantic.fromJpaHintName( hintName ) );
applyEntityGraphQueryHint( new EntityGraphQueryHint( hintName, (RootGraphImpl) value ) );
}
else {
MSG_LOGGER.warnf( "The %s hint was set, but the value was not an EntityGraph!", hintName );
}
applied = true;
}https://stackoverflow.com/questions/67646264
复制相似问题