Java 是否可以在SpringDataJpa@QueryHint注释中指定fetchgraph/loadgraph
是否可以在Spring数据Jpa中使用@QueryHint指定Java 是否可以在SpringDataJpa@QueryHint注释中指定fetchgraph/loadgraph,java,spring-data-jpa,javax.persistence,query-hints,Java,Spring Data Jpa,Javax.persistence,Query Hints,是否可以在Spring数据Jpa中使用@QueryHint指定javax.persistence.fetchgraph或javax.persistence.loadgraph 我有一个实体图 @Entity @NamedEntityGraph( name = "shop_with_all_associations", includeAllAttributes = true ) public class Shop { @JoinC
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<>();
}
@实体
@姓名识别图(
name=“与所有协会一起购物”,
includeAllAttributes=true
)
公共类商店{
@JoinColumn(name=“车间id”)
@OneToMany(fetch=FetchType.LAZY,cascade=CascadeType.ALL)
私有列表成员=新的ArrayList();
}
以及存储库中相应的方法
@Repository
public interface ShopRepository extends JpaRepository<Shop, Integer> {
@QueryHints({@QueryHint(name = "javax.persistence.fetchgraph", value = "shop_with_all_associations")})
List<Shop> getAllWithQueryHintBy();
}
@存储库
公共界面ShopRepository扩展了JpaRepository{
@QueryHints({@QueryHint(name=“javax.persistence.fetchgraph”,value=“shop\u with\u all\u associations”))
列出getAllWithQueryHintBy();
}
不,不可能。
您可以检查日志,并在调用该方法时看到下一个警告
o.h.q.internal.AbstractProducedQuery : The javax.persistence.fetchgraph hint was set, but the value was not an EntityGraph!
在AbstractProducedQuery类中,下一个代码部分显示,只有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;
}