I have this constellation:
public class Users {
private final @Id
@With
long id;
private final String userName;
private final String firstName;
private final String lastName;
private final LocalDateTime created;
@With
@MappedCollection(idColumn = "users_id")
private final Set<Privileges> privileges;
}
I have this query to fetch all users:
@Query("select u.id, u.userName, u.firstName, u.lastName from users u order by u.username")
List<Users> findsAllOrderedByUserName();
When i set the debug level for spring data jdbc (to get the queries fired to the db), i can see that there are lots of queries to get the Set<Privileges>
to every user.
Is there a way to tell Spring Data JDBC to not fetch this Set<>
automatically?
I have just figured it out.
You need to provide a DTO (Data Transfer Object) or a record as return type for the Query - not the entity with the
Set<>
.The DTO: