title: JPA Criteria: Convert int to String then select from substring of resulting String
body: <p>I have a String as parameter (which is in fact a valueOf(an Integer) and want to <strong>compare it to a substring of a int value in DB</strong>. Here is my code:</p>
  
  <pre><code>ClinicPatients clp = null;
  
  // Get the criteria builder instance from entity manager
  final CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();     
  // Create criteria query and pass the value object which needs to be populated as result
  CriteriaQuery&lt;ClinicPatients&gt; criteriaQuery = cb.createQuery(ClinicPatients.class);     
  // Tell to criteria query which tables/entities you want to fetch
  final Root&lt;ClinicPatients&gt; rootClp = criteriaQuery.from(ClinicPatients.class);
  
  criteriaQuery.select(rootClp);
  
  Expression&lt;String&gt; e1 = cb.function("CONVERT", String.class, rootClp.get(idCPFieldName));
  
  Predicate p1 = cb.equal(cb.substring(e1, 1, 3), idClinicPatient);
  criteriaList.add(p1);
  
  criteriaQuery.where(p1);
  
  // Here entity manager will create actual SQL query out of criteria query
  final TypedQuery&lt;ClinicPatients&gt; query = getEntityManager().createQuery(criteriaQuery);
  
  List&lt;ClinicPatients&gt; lClps = (List&lt;ClinicPatients&gt;) query.getResultList();
  if(lClps.size() &gt; 0)
      clp = lClps.get(0);
  
  return clp;
  </code></pre>
  
  <p>I get an error and my guess is that the conversion to String is never done then Hibernate tries to substring some weird value, but i'm not sure of anything at this point. 
  Here is the trace I get: </p>
  
  <pre><code>&gt; DEBUG could not extract ResultSet [n/a] com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an
  &gt; error in your SQL syntax; check the manual that corresponds to your
  &gt; MySQL server version for the right syntax to use near '), 1, 3)='005''
  &gt; at line 1     at
  &gt; sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
  &gt;   at
  &gt; sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
  &gt;   at
  &gt; sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
  &gt;   at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
  &gt;   at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)     at
  &gt; com.mysql.jdbc.Util.getInstance(Util.java:386)    at
  &gt; com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054)    at
  &gt; com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4190)    at
  &gt; com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4122)    at
  &gt; com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2570)     at
  &gt; com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2731)  at
  &gt; com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2818)   at
  &gt; com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2157)
  &gt;   at
  &gt; com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2324)
  &gt;   at
  &gt; org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:80)
  &gt;   at org.hibernate.loader.Loader.getResultSet(Loader.java:2065)   at
  &gt; org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1862)
  &gt;   at
  &gt; org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1838)
  &gt;   at org.hibernate.loader.Loader.doQuery(Loader.java:909)     at
  &gt; org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:354)
  &gt;   at org.hibernate.loader.Loader.doList(Loader.java:2553)     at
  &gt; org.hibernate.loader.Loader.doList(Loader.java:2539)  at
  &gt; org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2369)    at
  &gt; org.hibernate.loader.Loader.list(Loader.java:2364)    at
  &gt; org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:496)   at
  &gt; org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:387)
  &gt;   at
  &gt; org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:231)
  &gt;   at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1264)   at
  &gt; org.hibernate.internal.QueryImpl.list(QueryImpl.java:103)     at
  &gt; org.hibernate.jpa.internal.QueryImpl.list(QueryImpl.java:573)     at
  &gt; org.hibernate.jpa.internal.QueryImpl.getResultList(QueryImpl.java:449)
  &gt;   at
  &gt; org.hibernate.jpa.criteria.compile.CriteriaQueryTypeQueryAdapter.getResultList(CriteriaQueryTypeQueryAdapter.java:67)
  &gt;   at
  &gt; ca.chronometriq.commons.cmqfacade.ClinicPatientsFacade.findByIDClinicPatientFirstThreeDigitsVerificationNumber(ClinicPatientsFacade.java:150)
  &gt;   at
  &gt; ca.chronometriq.commons.cmqfacade.ClinicPatientsFacade$$FastClassByCGLIB$$7cc86440.invoke(&lt;generated&gt;)
  &gt;   at
  &gt; org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
  &gt;   at
  &gt; org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:698)
  &gt;   at
  &gt; org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
  &gt;   at
  &gt; org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:96)
  &gt;   at
  &gt; org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:260)
  &gt;   at
  &gt; org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94)
  &gt;   at
  &gt; org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
  &gt;   at
  &gt; org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:631)
  &gt;   at
  &gt; ca.chronometriq.commons.cmqfacade.ClinicPatientsFacade$$EnhancerByCGLIB$$8afa98d9.findByIDClinicPatientFirstThreeDigitsVerificationNumber(&lt;generated&gt;)    at
  &gt; ca.chronometriq.webterm.restapi.AdminModule.findByThreeDig(AdminModule.java:1474)
  &gt;   at
  &gt; ca.chronometriq.webterm.restapi.AdminModule$$FastClassByCGLIB$$2e650668.invoke(&lt;generated&gt;)
  &gt;   at
  &gt; org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
  &gt;   at
  &gt; org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:627)
  &gt;   at
  &gt; ca.chronometriq.webterm.restapi.AdminModule$$EnhancerByCGLIB$$e2195e01.findByThreeDig(&lt;generated&gt;)
  &gt;   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)  at
  &gt; sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
  &gt;   at
  &gt; sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  &gt;   at java.lang.reflect.Method.invoke(Method.java:606)     at
  &gt; com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
  &gt;   at
  &gt; com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:185)
  &gt;   at
  &gt; com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
  &gt;   at
  &gt; com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:288)
  &gt;   at
  &gt; com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
  &gt;   at
  &gt; com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
  &gt;   at
  &gt; com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
  &gt;   at
  &gt; com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
  &gt;   at
  &gt; com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1469)
  &gt;   at
  &gt; com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1400)
  &gt;   at
  &gt; com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349)
  &gt;   at
  &gt; com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339)
  &gt;   at
  &gt; com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
  &gt;   at
  &gt; com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)
  &gt;   at
  &gt; com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:699)
  &gt;   at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)     at
  &gt; org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
  &gt;   at
  &gt; org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
  &gt;   at
  &gt; org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
  &gt;   at
  &gt; org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
  &gt;   at
  &gt; org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
  &gt;   at
  &gt; org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
  &gt;   at
  &gt; org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
  &gt;   at
  &gt; org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:611)
  &gt;   at
  &gt; org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
  &gt;   at
  &gt; org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
  &gt;   at
  &gt; org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
  &gt;   at
  &gt; org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
  &gt;   at
  &gt; org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:409)
  &gt;   at
  &gt; org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1044)
  &gt;   at
  &gt; org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
  &gt;   at
  &gt; org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1721)
  &gt;   at
  &gt; org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1679)
  &gt;   at
  &gt; java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
  &gt;   at
  &gt; java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
  &gt;   at java.lang.Thread.run(Thread.java:744)
  </code></pre>
  
  <p>Any idea?</p>