@@ -95,22 +95,23 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport {
9595 public static final String ORDER_ASCENDING = "asc" ;
9696
9797
98- private static final String ROOT_CALL = "doCall" ;
98+ private static final String ROOT_DO_CALL = "doCall" ;
99+ private static final String ROOT_CALL = "call" ;
99100 private static final String LIST_CALL = "list" ;
100101 private static final String LIST_DISTINCT_CALL = "listDistinct" ;
101102 private static final String COUNT_CALL = "count" ;
102103 private static final String GET_CALL = "get" ;
103104 private static final String SCROLL_CALL = "scroll" ;
104- private static final String PROJECTIONS = "projections" ;
105105
106106
107+ private static final String PROJECTIONS = "projections" ;
107108 private SessionFactory sessionFactory ;
108109 private Session session ;
109110 private Class targetClass ;
110111 private Criteria criteria ;
111112 private MetaClass criteriaMetaClass ;
112- private boolean uniqueResult = false ;
113113
114+ private boolean uniqueResult = false ;
114115 private Stack logicalExpressionStack = new Stack ();
115116 private boolean participate ;
116117 private boolean scroll ;
@@ -122,7 +123,6 @@ public class HibernateCriteriaBuilder extends GroovyObjectSupport {
122123 private ResultTransformer resultTransformer ;
123124
124125
125-
126126 public HibernateCriteriaBuilder (Class targetClass , SessionFactory sessionFactory ) {
127127 super ();
128128 this .targetClass = targetClass ;
@@ -679,15 +679,9 @@ private boolean validateSimpleExpression() {
679679
680680 public Object invokeMethod (String name , Object obj ) {
681681 Object [] args = obj .getClass ().isArray () ? (Object [])obj : new Object []{obj };
682- if (this .criteria != null )
683- this .criteriaMetaClass = GroovySystem .getMetaClassRegistry ().getMetaClass (criteria .getClass ());
682+
684683
685- if (name .equals (ROOT_CALL ) ||
686- name .equals (LIST_CALL ) ||
687- name .equals (LIST_DISTINCT_CALL ) ||
688- name .equals (GET_CALL ) ||
689- name .equals (COUNT_CALL ) ||
690- name .equals (SCROLL_CALL ) && args .length == 1 && args [0 ] instanceof Closure ) {
684+ if (isCriteriaConstructionMethod (name , args )) {
691685
692686 if (this .criteria != null ) {
693687 throwRuntimeException ( new IllegalArgumentException ("call to [" + name + "] not supported here" ));
@@ -707,14 +701,7 @@ else if (name.equals(LIST_DISTINCT_CALL)) {
707701 this .resultTransformer = CriteriaSpecification .DISTINCT_ROOT_ENTITY ;
708702 }
709703
710- if (TransactionSynchronizationManager .hasResource (sessionFactory )) {
711- this .participate = true ;
712- this .session = ((SessionHolder )TransactionSynchronizationManager .getResource (sessionFactory )).getSession ();
713- }
714- else {
715- this .session = sessionFactory .openSession ();
716- }
717- this .criteria = this .session .createCriteria (targetClass );
704+ createCriteriaInstance ();
718705
719706 invokeClosureNode (args );
720707
@@ -746,6 +733,8 @@ else if(count) {
746733 }
747734 else {
748735
736+ if (criteria ==null ) createCriteriaInstance ();
737+
749738 MetaMethod metaMethod = getMetaClass ().getMetaMethod (name , args );
750739 if (metaMethod != null ) {
751740 return metaMethod .invoke (this , args );
@@ -846,6 +835,29 @@ else if(name.equals( IS_NOT_EMPTY )) {
846835 throw new MissingMethodException (name , getClass (), args ) ;
847836 }
848837
838+ private boolean isCriteriaConstructionMethod (String name , Object [] args ) {
839+ return name .equals (ROOT_CALL ) ||
840+ name .equals (ROOT_DO_CALL ) ||
841+ name .equals (LIST_CALL ) ||
842+ name .equals (LIST_DISTINCT_CALL ) ||
843+ name .equals (GET_CALL ) ||
844+ name .equals (COUNT_CALL ) ||
845+ name .equals (SCROLL_CALL ) && args .length == 1 && args [0 ] instanceof Closure ;
846+ }
847+
848+ private void createCriteriaInstance () {
849+ if (TransactionSynchronizationManager .hasResource (sessionFactory )) {
850+ this .participate = true ;
851+ this .session = ((SessionHolder )TransactionSynchronizationManager .getResource (sessionFactory )).getSession ();
852+ }
853+ else {
854+ this .session = sessionFactory .openSession ();
855+ }
856+
857+ this .criteria = this .session .createCriteria (targetClass );
858+ this .criteriaMetaClass = GroovySystem .getMetaClassRegistry ().getMetaClass (criteria .getClass ());
859+ }
860+
849861 private void invokeClosureNode (Object [] args ) {
850862 Closure callable = (Closure )args [0 ];
851863 callable .setDelegate (this );
0 commit comments