[英]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.