0

是否可以在 Spring Data Jpa 中以某种方式指定javax.persistence.fetchgraphjavax.persistence.loadgraph使用 @QueryHint?

我有一个实体图

@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中对应的方法

@Repository
public interface ShopRepository extends JpaRepository<Shop, Integer> {
    @QueryHints({@QueryHint(name = "javax.persistence.fetchgraph", value = "shop_with_all_associations")})
    List<Shop> getAllWithQueryHintBy();
}
4

1 回答 1

0

不,这是不可能的。您可以在调用该方法时检查您的日志并查看下一个警告

o.h.q.internal.AbstractProducedQuery     : The javax.persistence.fetchgraph hint was set, but the value was not an EntityGraph!

在 AbstractProducedQuery 类中,下一个代码部分显示,只有 的实例RootGraph才能被视为 EntityGraph。但是我们@QueryHint只允许将 String 作为值传递。

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;
            }
于 2021-05-22T04:59:51.173 回答