I have an SQL query that joins 3 tables, one of which is just many-to-many joining the other two. I am using Spring JDBC ResultSetExtractor to convert a ResultSet into my objects, which look something like this:
class Customer { private String id; private Set<AccountType> accountTypes; ... }
The implementation of ResultSetExtractor is as follows:
public List<Client> extractData(ResultSet rs) throws SQLException, DataAccessException { Map<Integer, Client> clientsMap = new LinkedHashMap<Integer, Client>(); while (rs.next()) { int id = rs.getInt("id");
This works great without pagination.
However, I need to break these results. I usually do this by adding this to the request, for example:
SELECT ... ORDER BY name ASC LIMIT 10 OFFSET 30;
However, since this query has connections, when I limit the number of results, I actually limit the number of JOINED results (that is, since the client will be displayed as many times as the number of types of accounts that they have, then LIMIT is applied not to the number of clients, but the number * accountTypes clients, which is not what I want).
The only solution I came across was to remove the LIMIT (and OFFSET, because that would also be wrong) from the query and apply them programmatically:
List<Client> allClients = jdbcTemplate.query.... List<Client> result = allClients.subList(offset, offset+limit);
But this is clearly not a very pleasant, effective solution. Is there a better way?