JPA @OneToOne select lists with N+1 queries
I finally found a better solution than JOIN FETCH
that also works with QueryDsl, using @EntityGraph
annotation on repository methods.
Here is the updated Child
definition :
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Entity
@NamedEntityGraph(name = "Child.withParent", attributeNodes = @NamedAttributeNode("parent"))
@Table(name = "child")
public class Child {
@Id
private Long id;
@Column
private String value;
@OneToOne(optional = false)
@JoinColumn(name = "parent")
private Parent parent;
}
And the ChildJpaRepository
definition :
public interface ChildJpaRepository extends JpaRepository<Child, Long>, QueryDslPredicateExecutor<Child> {
@Override
@EntityGraph("Child.withParent")
List<Child> findAll();
@Override
@EntityGraph("Child.withParent")
List<Child> findAll(Predicate predicate);
}
Thanks to Simon Martinelli and Vlad Mihalcea for your help