criteria api - Prevent Hibernate N+1 Selects when grouping by an entity -
i have hibernate criteria object build thusly:
criteria obscriteria = hibernatetemplate.getsessionfactory() .getcurrentsession().createcriteria(observation.class); projectionlist projection = projections.projectionlist() .add(projections.rowcount()) .add(projections.avg("value").as("avgscore")) .add(projections.avg("type.score")) .add(projections.max("date")) .add(projections.groupproperty("observedsubject")); criteria.setprojection(projection);
this produces correct result me, "observedsubject" property entity. when set set show_sql true, saw after first query (which returned 18 rows) there 18 selects observedsubject entities. i've tried:
criteria.setfetchmode("observedsubject", fetchmode.join);
but didn't work. kind of stab in dark, tried:
criteria.createalias("observedsubject", "observedsubject", criteria.full_join);
but didn't work, either. there way prevent behavior?
did annotate observedsubject
fetchtype.lazy
? if not, hibernate reverting default behavior, eager
fetching.
if do want child association fetched @ runtime, don't want separate select
calls each association, set @batchsize
on association , hibernate batch select
calls, making things more efficient.
Comments
Post a Comment