意外标记:SELECT 靠近第 1 行,第 485 列

unexpected token: SELECT near line 1, column 485

我有这三个查询以及第一个和第二个查询 运行 正确,然后我使用这些查询形成第三个查询。

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 " ;

当我 运行 第三个查询时,出现以下错误。

  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.<init>(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.<init>(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.<init>(HQLQueryPlan.java:126)
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(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

它在 EXISTS 关键字后抱怨,我注意到它也在 Hibernate 中。谁能给我提示来解决这个问题?有没有比编写上述查询更好的处理方法?

首先,您错过了 exists 子句的 ()。另外,我不太确定您是否可以在 AND 条件下使用 EXISTS 子句。如果这在 SQL 上有效,您可以试试这个:

String CHECK_FOR_COMBOS3 = CHECK_FOR_COMBOS1 + " AND ( EXISTS (" +CHECK_FOR_COMBOS2 + ") ) GROUP BY productSub.id , "
    + " productClass.productClassIsProduction " ;