Hello,
With Hibernate 3.0.5 and Mysql 5, I don't know what is wrong with the following count() method:
Code:
public int count() {
javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
javax.persistence.criteria.Root<T> rt = cq.from(entityClass);
cq.select(getEntityManager().getCriteriaBuilder().count(rt));
javax.persistence.Query q = getEntityManager().createQuery(cq);
return ((Long) q.getSingleResult()).intValue();
}
I got the following error when I execute the count() method:
Quote:
08:01:04,245 INFO [stdout] (http-portal.ixsystems.com.au-192.168.1.20-8080-1)
08:01:04,245 INFO [stdout] (http-portal.ixsystems.com.au-192.168.1.20-8080-1)
08:01:04,245 INFO [stdout] (http-portal.ixsystems.com.au-192.168.1.20-8080-1) CategoryImagesServiceImpl Hello count()...
08:01:04,246 INFO [stdout] (http-portal.ixsystems.com.au-192.168.1.20-8080-1) ****calling AbstractFacade().....**********
08:01:04,247 INFO [stdout] (http-portal.ixsystems.com.au-192.168.1.20-8080-1) Hibernate:
08:01:04,247 INFO [stdout] (http-portal.ixsystems.com.au-192.168.1.20-8080-1) select
08:01:04,248 INFO [stdout] (http-portal.ixsystems.com.au-192.168.1.20-8080-1) count((categoryim0_.category_id,
08:01:04,248 INFO [stdout] (http-portal.ixsystems.com.au-192.168.1.20-8080-1) categoryim0_.image_id)) as col_0_0_
08:01:04,248 INFO [stdout] (http-portal.ixsystems.com.au-192.168.1.20-8080-1) from
08:01:04,248 INFO [stdout] (http-portal.ixsystems.com.au-192.168.1.20-8080-1) houseware.category_images categoryim0_ limit ?
08:01:04,248 WARN [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (http-portal.ixsystems.com.au-192.168.1.20-8080-1) SQL Error: 1241, SQLState: 21000
08:01:04,249 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (http-portal.ixsystems.com.au-192.168.1.20-8080-1) Operand should contain 1 column(s)
08:01:04,249 INFO [org.hibernate.engine.jdbc.internal.LogicalConnectionImpl] (http-portal.ixsystems.com.au-192.168.1.20-8080-1) HHH00106:Forcing container resource cleanup on transaction completion
08:01:04,250 ERROR [org.jboss.ws.common.invocation.InvocationHandlerJAXWS] (http-portal.ixsystems.com.au-192.168.1.20-8080-1) Method invocation failed with exception: null: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [:1.6.0_07]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) [:1.6.0_07]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) [:1.6.0_07]
at java.lang.reflect.Method.invoke(Method.java:597) [:1.6.0_07]
at org.jboss.ws.common.invocation.AbstractInvocationHandlerJSE.invoke(AbstractInvocationHandlerJSE.java:111)
at org.jboss.wsf.stack.cxf.JBossWSInvoker._invokeInternal(JBossWSInvoker.java:169)
at org.jboss.wsf.stack.cxf.JBossWSInvoker.invoke(JBossWSInvoker.java:117)
at org.apache.cxf.interceptor.ServiceInvokerInterceptor$1.run(ServiceInvokerInterceptor.java:58)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441) [:1.6.0_07]
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303) [:1.6.0_07]
at java.util.concurrent.FutureTask.run(FutureTask.java:138) [:1.6.0_07]
at org.apache.cxf.workqueue.SynchronousExecutor.execute(SynchronousExecutor.java:37)
at org.apache.cxf.interceptor.ServiceInvokerInterceptor.handleMessage(ServiceInvokerInterceptor.java:106)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:263)
at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:118)
at org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:208)
at org.jboss.wsf.stack.cxf.RequestHandlerImpl.handleHttpRequest(RequestHandlerImpl.java:91)
at org.jboss.wsf.stack.cxf.transport.ServletHelper.callRequestHandler(ServletHelper.java:167)
at org.jboss.wsf.stack.cxf.CXFServletExt.invoke(CXFServletExt.java:87)
at org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:184)
at org.apache.cxf.transport.servlet.AbstractHTTPServlet.doPost(AbstractHTTPServlet.java:107)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:754) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
at org.jboss.wsf.stack.cxf.CXFServletExt.service(CXFServletExt.java:135)
at org.jboss.wsf.spi.deployment.WSFServlet.service(WSFServlet.java:140) [jbossws-spi-2.0.0.Beta10.jar:2.0.0.Beta10]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.2.Final.jar:7.1.0.Alpha2-SNAPSHOT]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.2.Final.jar:7.1.0.Alpha2-SNAPSHOT]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.2.Final.jar:7.1.0.Alpha2-SNAPSHOT]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.2.Final.jar:7.1.0.Alpha2-SNAPSHOT]
at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:49) [jboss-as-jpa-7.1.0.Alpha2-SNAPSHOT.jar:7.1.0.Alpha2-SNAPSHOT]
When I execute the above query in Mysql command windows, I can re-produce the same error:
Code:
mysql> select count((categoryim0_.category_id, categoryim0_.image_id)) as col_0_0_ from houseware.category_images categoryim0_ limit;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
mysql> select count((categoryim0_.category_id, categoryim0_.image_id)) as col_0_0_ from houseware.category_images categoryim0_ limit 100;
ERROR 1241 (21000): Operand should contain 1 column(s)
mysql> select count((categoryim0_.category_id, categoryim0_.image_id)) as col_0_0_ from houseware.category_images categoryim0_ limit 1;
ERROR 1241 (21000): Operand should contain 1 column(s)
mysql> select count((categoryim0_.category_id, categoryim0_.image_id)) as col_0_0_ from houseware.category_images categoryim0_;
ERROR 1241 (21000): Operand should contain 1 column(s)
mysql> select count((categoryim0_.category_id)) as col_0_0_ from houseware.category_images categoryim0_;
+----------+
| col_0_0_ |
+----------+
| 1 |
+----------+
1 row in set (0.00 sec)
Can anyone tell me how to change the count() method to deal with counting on multi columns?
Very appreciate for any suggestion and help.
Thanks
Sam