Issue
I have entity with some fields marked as LAZY
. In order to load LAZY
fields one must just access a getter while session is opened so ORM proxy perform select
sub-queries.
But this is suboptimal in case when you need a fully resolved object, using the same strategy as "eager" relations fetched by join
.
How is it possible to mark some fields as EAGER
temporary only for one query?
Is that possible with JPA standard or does it require proprietary extension like Hibernate?
Solution
Spring JpaRepository
allows marking queries (including custom) with org.springframework.data.jpa.repository.EntityGraph
:
@Entity Book {
@Id Long id;
@OneToMany List<Author> authors;
}
@EntityGraph(attributePaths = {"authors", "author.address"})
@Query("select b from Book b" +
" where b.id in (:ids)")
List<Book> loadAll(@Param("ids") List<Long> ids);
Answered By - gavenkoa
Answer Checked By - Gilberto Lyons (JavaFixing Admin)