我有这三个查询,第一和第二个查询正确运行,然后我使用这些查询来形成第三个查询。
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中。任何人都可以给我一个解决方法吗?有没有比编写上述查询更好的处理方法?
答案 0 :(得分:1)
首先,你错过了存在clausule的()
。另外,我不太确定你可以在AND条件上使用EXISTS子句。如果这在SQL上有效,您可以尝试:
String CHECK_FOR_COMBOS3 = CHECK_FOR_COMBOS1 + " AND ( EXISTS (" +CHECK_FOR_COMBOS2 + ") ) GROUP BY productSub.id , "
+ " productClass.productClassIsProduction " ;