1

Para não fugir ao escopo da pergunta anterior: Produto exibido seja o menor dentro do grupo (Criteria)

O problema aparentemente da questão foi resolvido, embora não foi possível verificar se realmente está correto, devido a ocorrência de um erro de exceção na classe de conversão

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import br.com.modelo.Lote;
import br.com.repository.Lotes;
import br.com.util.cdi.CDIServiceLocator;

@FacesConverter(forClass = Lote.class)
public class LoteConverter implements Converter {

//@Inject
private Lotes lotes;

public LoteConverter() {
    lotes = CDIServiceLocator.getBean(Lotes.class);
}

@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {

    Lote retorno = null;

    if (value != null) {
        String id = new String(value);
        retorno = lotes.porId(id);
    }

    return retorno;
}

@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
    if (value != null) {
        Lote lote = (Lote) value;
        return lote.getLote() == null ? null : lote.getLote();
    }

    return "";
}

}

Erro gerrado:

java.lang.ClassCastException: java.sql.Timestamp cannot be cast to br.com.modelo.Lote
at br.com.converter.LoteConverter.getAsString(LoteConverter.java:38)
at org.primefaces.component.autocomplete.AutoCompleteRenderer.encodeSuggestionsAsList(AutoCompleteRenderer.java:498)
at org.primefaces.component.autocomplete.AutoCompleteRenderer.encodeSuggestions(AutoCompleteRenderer.java:393)
at org.primefaces.component.autocomplete.AutoCompleteRenderer.encodeResults(AutoCompleteRenderer.java:123)
at org.primefaces.component.autocomplete.AutoCompleteRenderer.encodeEnd(AutoCompleteRenderer.java:105)
at javax.faces.component.UIComponentBase.encodeEnd(Unknown Source)
at javax.faces.component.UIComponent.encodeAll(Unknown Source)
at com.sun.faces.context.PartialViewContextImpl$PhaseAwareVisitCallback.visit(Unknown Source)
at com.sun.faces.component.visit.PartialVisitContext.invokeVisitCallback(Unknown Source)
at javax.faces.component.UIComponent.visitTree(Unknown Source)
at org.primefaces.component.api.UIData.visitColumnContent(UIData.java:879)
at org.primefaces.component.api.UIData.visitRows(UIData.java:853)
at org.primefaces.component.api.UIData.visitTree(UIData.java:707)
at javax.faces.component.UIComponent.visitTree(Unknown Source)
at javax.faces.component.UIComponent.visitTree(Unknown Source)
at org.primefaces.component.api.UITabPanel.visitTree(UITabPanel.java:920)
at javax.faces.component.UIComponent.visitTree(Unknown Source)
at javax.faces.component.UIForm.visitTree(Unknown Source)
at javax.faces.component.UIComponent.visitTree(Unknown Source)
at javax.faces.component.UIComponent.visitTree(Unknown Source)
at com.sun.faces.context.PartialViewContextImpl.processComponents(Unknown Source)
at com.sun.faces.context.PartialViewContextImpl.processPartial(Unknown Source)
at javax.faces.context.PartialViewContextWrapper.processPartial(Unknown Source)
at org.primefaces.context.PrimePartialViewContext.processPartial(PrimePartialViewContext.java:60)
at javax.faces.component.UIViewRoot.encodeChildren(Unknown Source)
at javax.faces.component.UIComponent.encodeAll(Unknown Source)
at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(Unknown Source)
at com.sun.faces.application.view.MultiViewHandler.renderView(Unknown Source)
at javax.faces.application.ViewHandlerWrapper.renderView(Unknown Source)
at javax.faces.application.ViewHandlerWrapper.renderView(Unknown Source)
at javax.faces.application.ViewHandlerWrapper.renderView(Unknown Source)
at com.sun.faces.lifecycle.RenderResponsePhase.execute(Unknown Source)
at com.sun.faces.lifecycle.Phase.doPhase(Unknown Source)
at com.sun.faces.lifecycle.LifecycleImpl.render(Unknown Source)
at javax.faces.webapp.FacesServlet.service(Unknown Source)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:154)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:199)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:110)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:343)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:260)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:217)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
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)

A linha do erro é a seguinte:

@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
    if (value != null) {
        Lote lote = (Lote) value; << Linha 38
        return lote.getLote() == null ? null : lote.getLote();
    }

    return "";
}

Alguma sugestão de como solucionar?

2 Respostas 2

0

Veja este exemplo

@Override
public Object getAsObject(FacesContext fc, UIComponent uic, String value) {
    if (value != null && !"".equals(value) && !(value.isEmpty())) {
        try {
            EntidadesDaoImp daoImp = new EntidadesDaoImp();
            Entidade entidade = new Entidade();

            entidade = daoImp.pesquisarId(Integer.parseInt(value));

            return entidade;
        } catch (NumberFormatException e) {
            throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Erro de Conversão", "O Valor não é válido."));
        }
    } else {
        return null;
    }
}

@Override
public String getAsString(FacesContext fc, UIComponent uic, Object object) {
    if (object != null && !"".equals(object)) {
        return String.valueOf(((Entidade) object).getId());
    }
    return null;

}
0

Olá, tente isto.

@Override
    public String getAsString(FacesContext fc, UIComponent uic, Object object) {
        if (object != null && !"".equals(object)) {
            return String.valueOf(((Lote) object).getId());
        }
        return null;

    }
1
  • Continuou com o mesmo erro de exception: java.lang.ClassCastException: java.sql.Timestamp cannot be cast to br.com.modelo.Lote acho que tem haver com a data da validade do lote. Ou pode ser que a solução da questão que está no inicio dessa pergunta, não esteja fazendo o que é para fazer. Commented 19/03/2016 às 0:03

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .