[英]What's the difference between fetchgraph and loadgraph in JPA 2.1?
[英]Is it possible to specify fetchgraph/loadgraph in SpringDataJpa @QueryHint annotation
是否可以在 Spring 數據 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中對應的方法
@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.