Problem with Like for Criteria

Asked

Viewed 50 times

0

Good morning.

I have a table in the following format:

inserir a descrição da imagem aqui

I need to select to return the sum of infrastructure values grouped by year. I created this query in SQL:

select YEAR(dataCompraInfra), sum(valorCompraInfra), sum(valorVendaInfra) from 
concessaoinfraestrutura WHERE dataCompraInfra LIKE '2011%' GROUP BY YEAR(dataCompraInfra);

The query worked, and returns the values contained in the image below. However, when I tried to adapt this in a Criteria, an exception is thrown.

Upshot

inserir a descrição da imagem aqui

Criteria

    Criteria crit = sessao.createCriteria(Infraestrutura.class);

    ProjectionList proj = Projections.projectionList();
    proj.add(Projections.sum("this.valorCompraInfra"));
    proj.add(Projections.sum("this.valorVendaInfra"));
    crit.add(Restrictions.like("this.dataCompraInfra", "2011", MatchMode.START));

    crit.setProjection(proj);

    lista = crit.list();

Exception:

ADVERTÊNCIA: java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Date
javax.el.ELException: java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Date
    at com.sun.el.parser.AstValue.invoke(AstValue.java:238)
    at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:297)
    at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:149)
    at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88)
    at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:818)
    at javax.faces.component.UICommand.broadcast(UICommand.java:300)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at br.com.somore.control.filter.ConexaoHibernateFilter.doFilter(ConexaoHibernateFilter.java:27)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:957)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:620)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Date
    at org.hibernate.type.TimestampType.toString(TimestampType.java:78)
    at org.hibernate.type.NullableType.nullSafeToString(NullableType.java:119)
    at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:160)
    at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:138)
    at org.hibernate.loader.Loader.bindPositionalParameters(Loader.java:1769)
    at org.hibernate.loader.Loader.bindParameterValues(Loader.java:1740)
    at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1612)
    at org.hibernate.loader.Loader.doQuery(Loader.java:717)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:270)
    at org.hibernate.loader.Loader.doList(Loader.java:2294)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2172)
    at org.hibernate.loader.Loader.list(Loader.java:2167)
    at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:119)
    at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1706)
    at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:347)
    at br.com.somore.control.dao.concessao.InfraestruturaDAO.listarResumo(InfraestruturaDAO.java:158)
    at br.com.somore.control.beans.CenarioBean.calcularResumo(CenarioBean.java:111)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.sun.el.parser.AstValue.invoke(AstValue.java:234)
    ... 34 more

Set 05, 2015 10:45:54 AM com.sun.faces.context.AjaxExceptionHandlerImpl handlePartialResponseError
GRAVE: java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Date
    at org.hibernate.type.TimestampType.toString(TimestampType.java:78)
    at org.hibernate.type.NullableType.nullSafeToString(NullableType.java:119)
    at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:160)
    at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:138)
    at org.hibernate.loader.Loader.bindPositionalParameters(Loader.java:1769)
    at org.hibernate.loader.Loader.bindParameterValues(Loader.java:1740)
    at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1612)
    at org.hibernate.loader.Loader.doQuery(Loader.java:717)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:270)
    at org.hibernate.loader.Loader.doList(Loader.java:2294)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2172)
    at org.hibernate.loader.Loader.list(Loader.java:2167)
    at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:119)
    at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1706)
    at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:347)
    at br.com.somore.control.dao.concessao.InfraestruturaDAO.listarResumo(InfraestruturaDAO.java:158)
    at br.com.somore.control.beans.CenarioBean.calcularResumo(CenarioBean.java:111)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.sun.el.parser.AstValue.invoke(AstValue.java:234)
    at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:297)
    at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:149)
    at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88)
    at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:818)
    at javax.faces.component.UICommand.broadcast(UICommand.java:300)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at br.com.somore.control.filter.ConexaoHibernateFilter.doFilter(ConexaoHibernateFilter.java:27)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:957)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:620)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)

Reading the exception, I conclude that the problem is in: crit.add(Restrictions.like("this.dataCompraInfra", "2011", MatchMode.START));

Can someone give a light on how I could return the values per year?

  • Put the instance of the list object there please, Cast Exception happens because the Hibernate return is not of the same type as the instantiated class for the list object... I think you’ll have to specify Transform for Hibernate. Put the guy there that I help you with

1 answer

2


try like this:

crit.add(Restrictions.ge("this.dataCompraInfra", sDate));
crit.add(Restrictions.lt("this.dataCompraInfra", fDate));

where sDate is a Date with value 1/1/2011 and fDate is a Date with value 31/12/2011.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.