从 CriteriaQuery 获取列表时出现异常
Posted
技术标签:
【中文标题】从 CriteriaQuery 获取列表时出现异常【英文标题】:Exception in getting list from CriteriaQuery 【发布时间】:2014-01-14 11:09:03 【问题描述】:由于某种原因我无法判断,当我尝试使用子查询。有人请帮忙!!! 这是代码:
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 FROM productscategory t1 WHERE (t1.productcatgoryname = 'Pri' 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 => [1个参数绑定] 调用: SELECT DISTINCT t0.productname FROM products t0 WHERE t0.productscategoryid IN (SELECT t1.productscategoryid.t1.productscategoryid FROM productscategory t1 WHERE (t1.productcatgoryname = ?)) bind => [1个参数绑定] 查询:TupleQuery(referenceClass=Products sql="SELECT DISTINCT t0.productname FROM products t0 WHERE t0.productscategoryid IN (SELECT t1.productscategoryid.t1.productscategoryid FROM productscategory t1 WHERE (t1.productcatgoryname = ?))") 查询:TupleQuery(referenceClass=Products sql="SELECT DISTINCT t0.productname FROM products t0 WHERE t0.productscategoryid IN (SELECT t1.productscategoryid.t1.productscategoryid FROM productscategory t1 WHERE (t1.productcatgoryname = ?))") 在 org.eclipse.persistence.internal.jpa.QueryImpl.getDetailedException(QueryImpl.java:378) 在 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) 在 com.inventory.service.ProductsServices.getProductsDistinctBySubQueriesName(ProductsServices.java:112) 在 com.inventory.service.ProductsServices.getAllByName(ProductsServices.java:211) 在 com.inventory.server.InventorySocketRequest.getServiceClass(InventorySocketRequest.java:77) 在 com.inventory.server.InventorySocketRequest.run(InventorySocketRequest.java:44) 原因:异常 [EclipseLink-4002] (Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.DatabaseException 内部异常:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在 '.productscategoryid FROM productscategory t1 WHERE (t1.productcatgoryname = 'Pri' 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 => [1个参数绑定] 查询:TupleQuery(referenceClass=Products sql="SELECT DISTINCT t0.productname FROM products t0 WHERE t0.productscategoryid IN (SELECT t1.productscategoryid.t1.productscategoryid FROM productscategory t1 WHERE (t1.productcatgoryname = ?))") 在 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) 在 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) 在 org.eclipse.persistence.queries.ObjectLevelReadQuery.execute(ObjectLevelReadQuery.java:1127) 在 org.eclipse.persistence.queries.ReadAllQuery.execute(ReadAllQuery.java:403) 在 org.eclipse.persistence.queries.ObjectLevelReadQuery.executeInUnitOfWork(ObjectLevelReadQuery.java:1215) 在 org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.internalExecuteQuery(UnitOfWorkImpl.java:2896) 在 org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1793) 在 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 FROM productscategory t1 WHERE (t1.productcatgoryname = 'Pri' at line 1 附近使用正确的语法 在 sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 在 sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) 在 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】:尝试使用此查询而不是您的查询,这与 EclipseLink 提供程序完美配合,不确定您的查询在 eclipselink 中不起作用但在 hibernate 中起作用,我测试了下面的查询,您将能够根据产品类别名称。
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。
【讨论】:
成功了!!!你拯救了这一天!太感谢了。这是一种怎样的体验。再次感谢您! 当我设置 distinct(true) 时,我似乎无法获得不同的值。奇怪!!!以上是关于从 CriteriaQuery 获取列表时出现异常的主要内容,如果未能解决你的问题,请参考以下文章
获取'无效更新:尝试从 UICollectionView 删除单元格时出现异常
从 ContactsContract 表中获取数据时出现 SQLITE 异常