I'm searching for the best way to optimize Hibernate select
queries.
Here is a basic example:
BDD model 1 Client -> n Contracts -> n Options
The simplest manner to request all data of the client "xxxx" is something like:
final Query hqlQuery = jdbcTemplate.createHQLQuery("from Client cli left join fetch cli.contracts con left join fetch con.options where cli.id=:idClient");
hqlQuery .setString("idClient", "xxxx");
Client client = (Client) hqlQuery.uniqueResult();
Sometimes this is just not possible since there are two much data to return.
So, I split the request, somethink like:
// Query 1
final Query hqlQueryClient = jdbcTemplate.createHQLQuery("from Client cli left join fetch cli.contracts where cli.id=:clientId");
hqlQueryClient.setString("clientId", "xxxx");
Client client = (Client) hqlQueryClient.uniqueResult();
List<String> listContractIds = new ArrayList<String>();
for (Contract contract : client.getContracts()) {
listContractIds.add(contract.getId());
}
// Query 2
final Query hqlQueryOptions = jdbcTemplate.createHQLQuery("from Option opt where opt.contract.id in(:contractIds)");
hqlQueryOptions.setParameterList("contractIds", listContractIds);
List<Option> options = hqlQueryClient.list();
But, with the second manner, I can't "inject" options
in client
object, so I have to deal with client
and options
objects in my code, and search in options
list those which correspond to the contract I'm working with.
Is there a way to complete Hibernate object (client in my example) with values requested in a second time ?
Thanks for your help.
PS: ask if it's not clear, I'm French guy :)