title: unexpected token: SELECT near line 1, column 485
body: <p>I have these three queries and first and second queries run correctly and then I use those queries to form the third query.</p>
  
  <pre><code>String CHECK_FOR_COMBOS1 = " SELECT new com.xxxx.domain.OpenCombo( productSub.id , CASE WHEN productClass.productClassIsProduction = 1 "
          + "THEN 0 ELSE 1 END )  FROM ProductSub productSub, Product product, ProductGroup productGroup,"
          + "ProductClass  productClass  "
          + " WHERE productSub.id = :productSubId"
          + " AND product.id = productSub.productId"
          + " AND productGroup.id = product.productGroupId"
          + " AND productClass.id = productGroup.productClassId";
  
  String CHECK_FOR_COMBOS2 = "SELECT 1 FROM ProductSubProductSub productSubProductSub ,ProductSub productSub, Product product,"
          + "ProductGroup productGroup,ProductClass  productClass "
          + " WHERE productSub.id = productSubProductSub.childProductSubId "
          + " AND product.id = productSub.productId "
          + " AND productGroup.id = product.productGroupId "
          + " AND productClass.id = productGroup.productClassId AND productClass.productClassIsProduction = 0"
          + " AND productSubProductSub.productSubId = :productSubId ";
  
  String CHECK_FOR_COMBOS3 = CHECK_FOR_COMBOS1 + " AND ( EXISTS " +CHECK_FOR_COMBOS2 + " ) GROUP BY productSub.id , "
          + " productClass.productClassIsProduction " ;
  </code></pre>
  
  <p>When I run the third query I get the following error.</p>
  
  <pre><code>  Caused by: java.lang.IllegalArgumentException: Validation failed for query for method public abstract com.xxxx.domain.OpenCombo com.xxxx.repository.ProductSubRepository.findItemHasCombo(int)!
      at org.springframework.data.jpa.repository.query.SimpleJpaQuery.validateQuery(SimpleJpaQuery.java:97)
      at org.springframework.data.jpa.repository.query.SimpleJpaQuery.&lt;init&gt;(SimpleJpaQuery.java:66)
      at org.springframework.data.jpa.repository.query.SimpleJpaQuery.fromQueryAnnotation(SimpleJpaQuery.java:169)
      at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$DeclaredQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:114)
      at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:160)
      at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:68)
      at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.&lt;init&gt;(RepositoryFactorySupport.java:304)
      at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:161)
      at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:162)
      at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:44)
      at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:168)
      ... 90 more
  Caused by: java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: SELECT near line 1, column 485 [ SELECT new com.xxxx.domain.OpenCombo( productSub.id , CASE WHEN productClass.productClassIsProduction = 1 THEN 0 ELSE 1 END )  FROM com.xxxx.domain.ProductSub productSub, com.xxxx.domain.Product product, com.xxxx.domain.ProductGroup productGroup,com.xxxx.domain.ProductClass  productClass   WHERE productSub.id = :productSubId AND product.id = productSub.productId AND productGroup.id = product.productGroupId AND productClass.id = productGroup.productClassId AND ( EXISTS SELECT 1 FROM com.xxxx.domain.ProductSubProductSub productSubProductSub ,com.xxxx.domain.ProductSub productSub, com.xxxx.domain.Product product,com.xxxx.domain.ProductGroup productGroup,com.xxxx.domain.ProductClass  productClass  WHERE productSub.id = productSubProductSub.childProductSubId  AND product.id = productSub.productId  AND productGroup.id = product.productGroupId  AND productClass.id = productGroup.productClassId AND productClass.productClassIsProduction = 0 AND productSubProductSub.productSubId = :productSubId  ) GROUP BY productSub.id ,  productClass.productClassIsProduction ]
      at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1750)
      at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1677)
      at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1683)
      at org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:331)
      at sun.reflect.GeneratedMethodAccessor68.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)
      at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:342)
      at com.sun.proxy.$Proxy74.createQuery(Unknown Source)
      at org.springframework.data.jpa.repository.query.SimpleJpaQuery.validateQuery(SimpleJpaQuery.java:91)
      ... 100 more
  Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: SELECT near line 1, column 485 [ SELECT new com.xxxx.domain.OpenCombo( productSub.id , CASE WHEN productClass.productClassIsProduction = 1 THEN 0 ELSE 1 END )  FROM com.xxxx.domain.ProductSub productSub, com.xxxx.domain.Product product, com.xxxx.domain.ProductGroup productGroup,com.xxxx.domain.ProductClass  productClass   WHERE productSub.id = :productSubId AND product.id = productSub.productId AND productGroup.id = product.productGroupId AND productClass.id = productGroup.productClassId AND ( EXISTS SELECT 1 FROM com.xxxx.domain.ProductSubProductSub productSubProductSub ,com.xxxx.domain.ProductSub productSub, com.xxxx.domain.Product product,com.xxxx.domain.ProductGroup productGroup,com.xxxx.domain.ProductClass  productClass  WHERE productSub.id = productSubProductSub.childProductSubId  AND product.id = productSub.productId  AND productGroup.id = product.productGroupId  AND productClass.id = productGroup.productClassId AND productClass.productClassIsProduction = 0 AND productSubProductSub.productSubId = :productSubId  ) GROUP BY productSub.id ,  productClass.productClassIsProduction ]
      at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:91)
      at org.hibernate.hql.internal.ast.ErrorCounter.throwQueryException(ErrorCounter.java:109)
      at org.hibernate.hql.internal.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:304)
      at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:203)
      at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:158)
      at org.hibernate.engine.query.spi.HQLQueryPlan.&lt;init&gt;(HQLQueryPlan.java:126)
      at org.hibernate.engine.query.spi.HQLQueryPlan.&lt;init&gt;(HQLQueryPlan.java:88)
      at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:167)
      at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:301)
      at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:236)
      at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1800)
      at org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:328)
      ... 106 more
  </code></pre>
  
  <p>It complains after <code>EXISTS</code> keyword and I noticed it's in Hibernate as well. Can anybody give me a hint to fix this? Is there a better way of handling this than writing the above query? </p>