Solucionado (ver solução)
Solucionado
(ver solução)
2
respostas

HTTP Status 500 - No entity found for query

Bom dia!

Eu fiz um cadastro de "um" usuário na tabela para não gerar esse erro a primeira vez que ele ocorreu...

Porém eu fiz um teste digitando um usuário correto, porém com uma senha "errada", e o sistema ao invés de chamar a msg de erro gerou o erro acima...

No browser:

HTTP Status 500 - No entity found for query

type Exception report

message No entity found for query

description The server encountered an internal error that prevented it from fulfilling this request.

exception

javax.persistence.NoResultException: No entity found for query
    org.hibernate.jpa.internal.QueryImpl.getSingleResult(QueryImpl.java:532)
    br.com.ebf.dao.UsuarioDao.busca(UsuarioDao.java:58)
    br.com.ebf.dao.UsuarioDao$Proxy$_$$_WeldClientProxy.busca(Unknown Source)
    br.com.ebf.controller.LoginController.autentica(LoginController.java:40)
    br.com.ebf.controller.LoginController$Proxy$_$$_WeldClientProxy.autentica(Unknown Source)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    java.lang.reflect.Method.invoke(Unknown Source)
    net.vidageek.mirror.provider.java.PureJavaMethodReflectionProvider.invoke(PureJavaMethodReflectionProvider.java:38)
    net.vidageek.mirror.invoke.MethodHandlerByMethod.withArgs(MethodHandlerByMethod.java:54)
    br.com.caelum.vraptor.core.DefaultReflectionProvider.invoke(DefaultReflectionProvider.java:42)
    br.com.caelum.vraptor.core.DefaultReflectionProvider$Proxy$_$$_WeldClientProxy.invoke(Unknown Source)
    br.com.caelum.vraptor.observer.ExecuteMethod$1.call(ExecuteMethod.java:82)
    br.com.caelum.vraptor.observer.ExecuteMethod$1.call(ExecuteMethod.java:72)
    br.com.caelum.vraptor.core.Try.run(Try.java:18)
    br.com.caelum.vraptor.observer.ExecuteMethod.execute(ExecuteMethod.java:72)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    java.lang.reflect.Method.invoke(Unknown Source)
    org.jboss.weld.injection.MethodInjectionPoint.invokeOnInstanceWithSpecialValue(MethodInjectionPoint.java:93)
    org.jboss.weld.event.ObserverMethodImpl.sendEvent(ObserverMethodImpl.java:266)
    org.jboss.weld.event.ObserverMethodImpl.sendEvent(ObserverMethodImpl.java:253)
    org.jboss.weld.event.ObserverMethodImpl.notify(ObserverMethodImpl.java:232)
    org.jboss.weld.event.ObserverNotifier.notifyObserver(ObserverNotifier.java:169)
    org.jboss.weld.event.ObserverNotifier.notifyObserver(ObserverNotifier.java:165)
    org.jboss.weld.event.ObserverNotifier.notifyObservers(ObserverNotifier.java:119)
    org.jboss.weld.event.ObserverNotifier.fireEvent(ObserverNotifier.java:112)
    org.jboss.weld.event.EventImpl.fire(EventImpl.java:83)
    br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:78)
    br.com.caelum.vraptor.interceptor.AspectStyleInterceptorHandler.execute(AspectStyleInterceptorHandler.java:88)
    br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:83)
    br.com.caelum.vraptor.interceptor.FlashInterceptor.intercept(FlashInterceptor.java:98)
    br.com.caelum.vraptor.interceptor.FlashInterceptor$Proxy$_$$_WeldClientProxy.intercept(Unknown Source)
    br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler$1.call(ToInstantiateInterceptorHandler.java:71)
    br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler$1.call(ToInstantiateInterceptorHandler.java:68)
    br.com.caelum.vraptor.core.Try.run(Try.java:18)
    br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.executeSafely(ToInstantiateInterceptorHandler.java:68)
    br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:61)
    br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:83)
    br.com.caelum.vraptor.interceptor.ExceptionHandlerInterceptor.intercept(ExceptionHandlerInterceptor.java:75)
    br.com.caelum.vraptor.interceptor.ExceptionHandlerInterceptor$Proxy$_$$_WeldClientProxy.intercept(Unknown Source)
    br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler$1.call(ToInstantiateInterceptorHandler.java:71)
    br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler$1.call(ToInstantiateInterceptorHandler.java:68)
    br.com.caelum.vraptor.core.Try.run(Try.java:18)
    br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.executeSafely(ToInstantiateInterceptorHandler.java:68)
    br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:61)
    br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:83)
    br.com.caelum.vraptor.core.DefaultInterceptorStack.start(DefaultInterceptorStack.java:93)
    br.com.caelum.vraptor.core.DefaultInterceptorStack$Proxy$_$$_WeldClientProxy.start(Unknown Source)
    br.com.caelum.vraptor.observer.RequestHandlerObserver.handle(RequestHandlerObserver.java:93)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    java.lang.reflect.Method.invoke(Unknown Source)
    org.jboss.weld.injection.MethodInjectionPoint.invokeOnInstanceWithSpecialValue(MethodInjectionPoint.java:93)
    org.jboss.weld.event.ObserverMethodImpl.sendEvent(ObserverMethodImpl.java:266)
    org.jboss.weld.event.ObserverMethodImpl.sendEvent(ObserverMethodImpl.java:253)
    org.jboss.weld.event.ObserverMethodImpl.notify(ObserverMethodImpl.java:232)
    org.jboss.weld.event.ObserverNotifier.notifyObserver(ObserverNotifier.java:169)
    org.jboss.weld.event.ObserverNotifier.notifyObserver(ObserverNotifier.java:165)
    org.jboss.weld.event.ObserverNotifier.notifyObservers(ObserverNotifier.java:119)
    org.jboss.weld.event.ObserverNotifier.fireEvent(ObserverNotifier.java:112)
    org.jboss.weld.event.EventImpl.fire(EventImpl.java:83)
    br.com.caelum.vraptor.VRaptor.doFilter(VRaptor.java:123)
note The full stack trace of the root cause is available in the Apache Tomcat/8.5.8 logs.

Apache Tomcat/8.5.8

Meu método no UsuárioDao:

    public Usuario busca(String login, String senha) {
        TypedQuery<Usuario> query = manager
                .createQuery("select u from Usuario u where u.login = :login and u.senha = :senha", Usuario.class);
        query.setParameter("login", login);
        query.setParameter("senha", senha);

        return query.getSingleResult();
    }

Adicionei a anotação @Valid no método autentica do LoginController, mas msms assim não chamou a msg de erro...

    @Open
    public void autentica(@Valid String login, String senha) {
        Usuario usuario = usuarioDao.busca(login, senha);
        if (usuario != null) {
            usuarioLogado.fazLogin(usuario);
            result.redirectTo(UsuarioController.class).lista();
        } else {
            validator.add(new SimpleMessage("login_invalido", "Usuário ou senha incorretos!"));
            validator.onErrorRedirectTo(this).formularioLogin();
        }
    }
2 respostas
solução!

Oi Ewerton, essa exception está sendo lançada pois sua query não está retornando nenhum resultado.

Para tratar isso, voce pode colocar um try catch em query.getSingleResult(); devolver um null no catch, aí no controller você sabe que se o método retornou null, nenhum usuário foi encontrado.

Se você estiver usando Java 8, pode usar o Optional também, aí em vez de devolver um null pode devolver um Optional.empty

Perfeiro Philippe!!

Mais uma vez obrigado!! Abraço!