Search code examples
springormspring-dataspring-data-jpajpql

Fetch Join not working in JPA Repository with OneToOne or OneToMany


Entity A has one to one mapped with Entity B.Created the EntityARepository and use find() with Entity A.(Eager Loading)

When I sql output it shows me the two select statements but it should be single select join query.This can be possible by writing the JPQL. But I would like to know is that possible to do with JPARepository method.


Solution

  • As far as I know JPA specification does not let you choose the fetch strategy on the entity level, by default the strategy is "select" which means that for every fetch there will be a new query. You are looking for "join" strategy. If you are using Hibernate you can use @Fetch(FetchMode.JOIN) to specify the strategy.