I recently switched from JEE to Spring Boot and I'm loving it so far. But I have run into a little issue. I have this method that fetches lazy references that used to look something like this:
public Foo getParentWithChildren(long id, boolean isFetchChild, boolean isFetchPets) {
StringBuilder sql = new StringBuilder();
sql.append("select DISTINCT(p) from Parent p");
if(isFetchChild) {
sql.append(" left join p.child c");
} if(isFetchPets) {
sql.append(" left join p.pets pts");
}
sql.append(" where p.id=:id");
TypedQuery<Foo> query = em.createQuery(sql.toString(), Foo.class);
query.setParameter("id", id);
return query.getSingleResult();
}
Now with spring data and their awesome interfaces I would like to do something simlar using the @Query annotation on the interface instead of having to write a custom implementation. But is it possible to do something similar using only the interface.
The example below will obviously not work but I hope you understand what I am trying to acchieve
@Query("select distinct(p) from Parent p " +
(fetchChild ? " left join p.child c" : "") +
(fetchPets ? " left join p.pets pts" : "") +
" where p.id=:id")
Foo getParentWithChildren(@Param("id") long id, @Param("fetchChild") boolean isFetchChild, @Param("fetchPets") boolean isFetchPets);
I something similar possible?