简体   繁体   English

是否可以在 SpringDataJpa @QueryHint 注释中指定 fetchgraph/loadgraph

[英]Is it possible to specify fetchgraph/loadgraph in SpringDataJpa @QueryHint annotation

Is it possible somehow to specify the javax.persistence.fetchgraph or javax.persistence.loadgraph using @QueryHint in Spring Data Jpa?是否可以在 Spring 数据 Jpa 中使用 @QueryHint 以某种方式指定javax.persistence.fetchgraphjavax.persistence.loadgraph

I have an entity graph我有一个实体图

@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<>();
}

And the corresponding method in the repository以及repository中对应的方法

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

No, it's not possible.不,这是不可能的。 You can check your log and see the next warning when calling the method您可以在调用该方法时检查您的日志并查看下一个警告

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

And in the AbstractProducedQuery class, the next code section shows, that only instances of RootGraph could be treated as EntityGraph.在 AbstractProducedQuery class 中,下一个代码部分显示,只有RootGraph的实例可以被视为 EntityGraph。 But our @QueryHint allow to pass only String as value.但是我们的@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;
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM