Java – hibernate criteria: different entities and then restrictions
I have a standard to return all the data required by the application, basically:
Criteria criteria = session.createCriteria(Client.class); criteria.createAlias("address","address"); criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY); criteria.setFirstResult(init); criteria.setMaxResults(max); List<Client> clients = criteria.list();
The problem is that the relational client / address is bidirectional: the client has an address, and an address may belong to multiple clients
I want to retrieve "single" client objects based on their PK. Of course, some clients are displayed in the table
Because setfirstresult / setmaxresults are executed first, I have repeated the client within the limits that have been applied After that (the application level is not used by group by), hibernate gets the rids of duplicate clients, so I finally get the maximum value specified in setmaxresults for fewer clients
Cannot group (projection group) because it does not return all the columns required in the client / address, only the groups grouped by the query
(in a word, my table has 100 results per page, but after discarding the duplicates, I have 98 results instead of 100...) this is because of the limitation: the application of limit 0100 before the sleep group should be executed after)
Solution
As pointed out in the thread of the "Ashish thukral" link, this is solved:
criteria.setFetchMode("address.clients",FetchMode.SELECT);
It blocks connections that cause problems
Of course, fetch = "join" can be removed from the XML configuration file, but this solution will not affect other places where beans may be retrieved