I have a Hibernate Criteria object that I build thusly:
Criteria obsCriteria = hibernateTemplate.getSessionFactory()
.getCurrentSession().cr开发者_如何学CeateCriteria(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 a correct result for me, but the "observedSubject" property is an entity. When I set set show_sql to true, I saw that after the first query (which returned 18 rows) there were 18 selects to get the observedSubject entities. I've tried:
criteria.setFetchMode("observedSubject", FetchMode.JOIN);
But that didn't work. As kind of a stab in the dark, I tried:
criteria.createAlias("observedSubject", "observedSubject", Criteria.FULL_JOIN);
But that didn't work, either. Is there any way to prevent this behavior?
Did you annotate observedSubject
to FetchType.LAZY
? If not, Hibernate is reverting to default behavior, which is EAGER
fetching.
If you do want the child association fetched at runtime, but you don't want separate SELECT
calls for each association, set @BatchSize
on the association and Hibernate will batch the SELECT
calls, making things more efficient.
精彩评论