简体   繁体   中英

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?

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
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. But our @QueryHint allow to pass only String as value.

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;
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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