从CriteriaQuery获取列表的例外情况

时间:2014-01-14 11:09:03

标签: list exception subquery criteria-api criteriaquery

由于某种原因,我无法告诉,当我尝试使用子查询从CriteriaQuery获取列表时会出现异常。有人请帮忙!!! 这是代码:

public String[] getProductsDistinctBySubQueriesName(String category) {
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Tuple> criteria = builder.createTupleQuery();
    //subquery
    Subquery<Integer> subqueries = criteria.subquery(Integer.class);
    Root<Productscategory> productCategory = subqueries.from(Productscategory.class);
    subqueries.select(productCategory.<Integer>get("productscategoryid"))
            .where(builder.equal((productCategory.<String>get("productcatgoryname")), category));
    //outerquery
    Root<Products> root = criteria.from(Products.class);
    criteria.multiselect(root.get(Products_.productname)).distinct(true)
            .where(builder.in(root.get("productscategoryid")).value(subqueries));


    List<Tuple> tupleResult = em.createQuery(criteria).getResultList(); // the exception is thrown here
    String[] arrayProducts = new String[tupleResult.size()];
    for (int i = 0; i < tupleResult.size(); i++) {
        arrayProducts[i] = (String) tupleResult.get(i).get(0);
    }
    return arrayProducts;

}

这是例外

内部异常:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:SQL语法中有错误;检查与您的MySQL服务器版本相对应的手册,以便在#productscategoryid附近使用正确的语法。产品类别t1 WHERE(t1.productcatgoryname =&#39; Pri&#39; at line 1 错误代码:1064 错误代码:1064 调用:SELECT DISTINCT t0.productname FROM products t0 WHERE t0.productscategoryid IN(SELECT t1.productscategoryid.t1.productscategoryid FROM productscategory t1 WHERE(t1.productcatgoryname =?))     bind =&gt; [1参数界限] 调用:SELECT DISTINCT t0.productname FROM products t0 WHERE t0.productscategoryid IN(SELECT t1.productscategoryid.t1.productscategoryid FROM productscategory t1 WHERE(t1.productcatgoryname =?))     bind =&gt; [1参数界限] 查询:TupleQuery(referenceClass = Products sql =&#34; SELECT DISTINCT t0.productname FROM products t0 WHERE t0.productscategoryid IN(SELECT t1.productscategoryid.t1.productscategoryid FROM productscategory t1 WHERE(t1.productcatgoryname =?))&#34 ;) 查询:TupleQuery(referenceClass = Products sql =&#34; SELECT DISTINCT t0.productname FROM products t0 WHERE t0.productscategoryid IN(SELECT t1.productscategoryid.t1.productscategoryid FROM productscategory t1 WHERE(t1.productcatgoryname =?))&#34 ;)     at org.eclipse.persistence.internal.jpa.QueryImpl.getDetailedException(QueryImpl.java:378)     at org.eclipse.persistence.internal.jpa.QueryImpl.getDetailedException(QueryImpl.java:378)     在org.eclipse.persistence.internal.jpa.QueryImpl.executeReadQuery(QueryImpl.java:260)     在org.eclipse.persistence.internal.jpa.QueryImpl.getResultList(QueryImpl.java:469)     at com.inventory.service.ProductsServices.getProductsDistinctBySubQueriesName(ProductsServices.java:112)     at com.inventory.service.ProductsServices.getAllByName(ProductsServices.java:211)     at com.inventory.server.InventorySocketRequest.getServiceClass(InventorySocketRequest.java:77)     在com.inventory.server.InventorySocketRequest.run(InventorySocketRequest.java:44) 引起:Exception [EclipseLink-4002](Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5):org.eclipse.persistence.exceptions.DatabaseException 内部异常:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:SQL语法中有错误;检查与您的MySQL服务器版本相对应的手册,以便在#productscategoryid附近使用正确的语法。产品类别t1 WHERE(t1.productcatgoryname =&#39; Pri&#39; at line 1 错误代码:1064 调用:SELECT DISTINCT t0.productname FROM products t0 WHERE t0.productscategoryid IN(SELECT t1.productscategoryid.t1.productscategoryid FROM productscategory t1 WHERE(t1.productcatgoryname =?))     bind =&gt; [1参数界限] 查询:TupleQuery(referenceClass = Products sql =&#34; SELECT DISTINCT t0.productname FROM products t0 WHERE t0.productscategoryid IN(SELECT t1.productscategoryid.t1.productscategoryid FROM productscategory t1 WHERE(t1.productcatgoryname =?))&#34 ;)     at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:340)     在org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.basicExecuteCall(DatabaseAccessor.java:682)     在org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeCall(DatabaseAccessor.java:558)     at org.eclipse.persistence.internal.sessions.AbstractSession.basicExecuteCall(AbstractSession.java:1991)     在org.eclipse.persistence.sessions.server.ServerSession.executeCall(ServerSession.java:570)     在org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeCall(DatasourceCallQueryMechanism.java:242)     在org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeCall(DatasourceCallQueryMechanism.java:228)     在org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeSelectCall(DatasourceCallQueryMechanism.java:299)     在org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.selectAllRows(DatasourceCallQueryMechanism.java:694)     在org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.selectAllRowsFromTable(ExpressionQueryMechanism.java:2738)     在org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.selectAllReportQueryRows(ExpressionQueryMechanism.java:2675)     在org.eclipse.persistence.queries.ReportQuery.executeDatabaseQuery(ReportQuery.java:848)     在org.eclipse.persistence.queries.DatabaseQuery.execute(DatabaseQuery.java:899)     at org.eclipse.persistence.queries.ObjectLevelReadQuery.execute(ObjectLevelReadQuery.java:1127)     在org.eclipse.persistence.queries.ReadAllQuery.execute(ReadAllQuery.java:403)     at org.eclipse.persistence.queries.ObjectLevelReadQuery.executeInUnitOfWork(ObjectLevelReadQuery.java:1215)     at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.internalExecuteQuery(UnitOfWorkImpl.java:2896)     at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1793)     at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1775)     在org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1740)     在org.eclipse.persistence.internal.jpa.QueryImpl.executeReadQuery(QueryImpl.java:258)     ......还有5个 引发者:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:您的SQL语法中有错误;检查与您的MySQL服务器版本相对应的手册,以便在#productscategoryid附近使用正确的语法。产品类别t1 WHERE(t1.productcatgoryname =&#39; Pri&#39; at line 1     at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)     at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)     at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)     在java.lang.reflect.Constructor.newInstance(Constructor.java:525)     在com.mysql.jdbc.Util.handleNewInstance(Util.java:411)     在com.mysql.jdbc.Util.getInstance(Util.java:386)     在com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1053)     在com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4120)     在com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4052)     在com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2503)     在com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2664)     在com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2794)     在com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155)     在com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2322)     在org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeSelect(DatabaseAccessor.java:1007)     在org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.basicExecuteCall(DatabaseAccessor.java:642)     ......还有24个

1 个答案:

答案 0 :(得分:0)

尝试使用此查询而不是您的查询,这与EclipseLink提供程序完美配合,不确定您的查询在eclipselink中不起作用但在hibernate中工作,我测试了下面的查询,您将能够选择基于ProductCategoryName。

    EntityManagerFactory emf = Persistence.createEntityManagerFactory("jpaQuery");
    EntityManager em = emf.createEntityManager();

    String category = "cat2";

    CriteriaBuilder builder = em.getCriteriaBuilder();

    CriteriaQuery<Products> query = builder.createQuery(Products.class);
    Root<Products> products = query.from(Products.class);

    Subquery<Productscategory> squery = query.subquery(Productscategory.class);
    Root<Productscategory> productCategoryRoot = squery.from(Productscategory.class);
    Join<Productscategory, Products> join = productCategoryRoot.join("productsCollection");

    squery.select(productCategoryRoot)
    .where(builder.equal(join, products), builder.equal(productCategoryRoot.get("productcatgoryname"), category));

    query.where( builder.exists(squery));
    List<Products> productList = em.createQuery(query).getResultList(); 

    for (Products product : productList) {
           System.out.println(product);
    }

在哪个类别中是您要搜索的类别的名称..

这解决了问题=)..如果这样做,不要忘记接受答案XD。