2

i have the following constellation:

public final class ApplicationUser {

private final @Id
@Wither
long applicationUserId;

@NotNull
@Size(min = 4, max = 20)
private final
String login;

@NotNull
@Email
private final
String eMail; 

@Wither
private Set<Privilege> privileges;
}

Now i want to query one ApplicationUser by its login which is done by a simple sql Query.
My question now:
Is there any spring-data-jdbc related thing to jpa´s join fetch so that i can fetch the associated Set<Privilege> privileges in one declared query?
Or do i have to fire another query like so:

  1. query ApplicationUser by its login
  2. query Privilege by its foreign key applicationuser

Thank you

Thomas Lang
  • 1,285
  • 17
  • 35

2 Answers2

2

I should have waited with my question, because i´ve just found an answer myself.
So here is my answer:
Just type this sql query onto your repository function:

@Query("select a.applicationuserid, a.email, a.login, p from applicationuser a left outer join privilege p on a.applicationuserid = p.applicationuser where lower(a.login) = lower(:login)")

In the corresponding logs there has been fired a second sql query to fetch the privilegs.

Thomas Lang
  • 1,285
  • 17
  • 35
2

Your answer describes the default way to do this. If you want to avoid the extra select you could use a statement that contains the join and use a ResultSetExtractor to construct your entities.

Pro: More efficient at runtime.

Con: More work for you.

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348