簡體   English   中英

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

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

是否可以在 Spring 數據 Jpa 中使用 @QueryHint 以某種方式指定javax.persistence.fetchgraphjavax.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中對應的方法

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

不,這是不可能的。 您可以在調用該方法時檢查您的日志並查看下一個警告

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

在 AbstractProducedQuery class 中,下一個代碼部分顯示,只有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;
            }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM