Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Partial fix for GRAILS-9464 "Add fetching strategy support to Where q…

…ueries"
  • Loading branch information...
commit 64e6127fa3b9de1808c3d3e7729b6f2e59b98e43 1 parent bc327a9
@graemerocher graemerocher authored
View
18 ...ernate/src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/query/HibernateQuery.java
@@ -30,6 +30,7 @@
import org.grails.datastore.mapping.query.api.QueryableCriteria;
import org.grails.datastore.mapping.query.criteria.FunctionCallingCriterion;
import org.hibernate.Criteria;
+import org.hibernate.FetchMode;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.*;
import org.hibernate.dialect.Dialect;
@@ -47,6 +48,8 @@
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.util.ReflectionUtils;
+import javax.persistence.FetchType;
+
/**
* Bridges the Query API with the Hibernate Criteria API
*
@@ -386,9 +389,23 @@ public List list() {
if(projectionLength<2)
criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
+
+ applyFetchStrategies();
return criteria.list();
}
+ private void applyFetchStrategies() {
+ for (Map.Entry<String, FetchType> entry : fetchStrategies.entrySet()) {
+ switch(entry.getValue()) {
+ case EAGER:
+ criteria.setFetchMode(entry.getKey(), FetchMode.JOIN); break;
+ case LAZY:
+ criteria.setFetchMode(entry.getKey(), FetchMode.SELECT); break;
+
+ }
+ }
+ }
+
@Override
protected void flushBeforeQuery() {
// do nothing
@@ -400,6 +417,7 @@ public Object singleResult() {
criteria.setProjection(hibernateProjectionList.getHibernateProjectionList());
}
criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
+ applyFetchStrategies();
return criteria.uniqueResult();
}
Please sign in to comment.
Something went wrong with that request. Please try again.