java - Hibernate Criteria: distinct entities and then limit -
i have criteria returns data application requires, 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 relation client / address bidirectional: on client has 1 address , 1 address may belong more 1 client.
i want retrieve "single" client objects based on pk of course, number of clients displayed in table.
because setfirstresult/setmaxresults executed first getting duplicated clients within applied limits. after (application level not group used) hibernate gets rids of duplicate clients end less clients maximum specified in setmaxresults.
cannot group (projection group) won't return columns required in client/addresses, group query grouping by.
(to sum up, table has 100 results per page after discarding duplicates have 98 results instead of 100...) because limit : limit 0,100 applied before hibernate groups when should performed after)
as pointed out in thread linked "ashish thukral" next line solves this:
criteria.setfetchmode("address.clients", fetchmode.select); it prevents join causes problem made.
of course, possible remove fetch="join" xml configuration file solution not affect other places beans may being retrieved.
Comments
Post a Comment