4
respostas

HTTP Status 500 – Internal Server Error

Boa Tarde, meu problema está quando tento inicar qualquer tipo de busca. Ele aponta HTTP Status 500 – Internal Server Error. A pagina inicial aparece normalmente.

package br.com.caelum.dao;

import java.util.ArrayList; import java.util.List;

import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.persistence.TypedQuery; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.Path; import javax.persistence.criteria.Predicate; import javax.persistence.criteria.Root;

import org.springframework.stereotype.Repository;

import br.com.caelum.model.Loja; import br.com.caelum.model.Produto;

@Repository public class ProdutoDao {

@PersistenceContext
private EntityManager em;

public List<Produto> getProdutos() {
    return em.createQuery("from Produto", Produto.class).getResultList();
}

public Produto getProduto(Integer id) {
    Produto produto = em.find(Produto.class, id);
    return produto;
}

public List<Produto> getProdutos(String nome, Integer categoriaId, Integer lojaId) {

    CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
    CriteriaQuery<Produto> query = criteriaBuilder.createQuery(Produto.class);
    Root<Produto> root = query.from(Produto.class);

    Path<String> nomePath = root.<String>get("nome");
    Path<Integer> lojaPath = root.<Loja>get("loja").<Integer>get("id");
    Path<Integer> categoriaPath = root.<Loja>get("categorias").<Integer>get("id");

    List<Predicate> predicates = new ArrayList<>();

    if (!nome.isEmpty()) {
        Predicate nomeIgual = criteriaBuilder.like(nomePath, nome);
        predicates.add(nomeIgual);            
    }
    if(categoriaId !=null) {
        Predicate categoriaIgual = criteriaBuilder.equal(categoriaPath, categoriaId);
        predicates.add(categoriaIgual);
    }
    if(lojaId !=null) {
        Predicate lojaIgual = criteriaBuilder.equal(lojaPath, lojaId);
        predicates.add(lojaIgual);
    }

    query.where(predicates.toArray(new Predicate[0]));    

    TypedQuery<Produto> typedQuery = em.createQuery(query);
    typedQuery.getResultList();

    return typedQuery.getResultList();

}

public void insere (Produto produto) {
    if(produto.getId()==null)
        em.persist(produto);
    else
        em.merge(produto);

}

}

4 respostas

HTTP Status 500 – Internal Server Error Type Exception Report

Message Request processing failed; nested exception is java.lang.IllegalStateException: Illegal attempt to dereference path source [null.categorias] of basic type

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalStateException: Illegal attempt to dereference path source [null.categorias] of basic type org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978) org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:868) javax.servlet.http.HttpServlet.service(HttpServlet.java:661) org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842) javax.servlet.http.HttpServlet.service(HttpServlet.java:742) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) Root Cause

java.lang.IllegalStateException: Illegal attempt to dereference path source [null.categorias] of basic type org.hibernate.jpa.criteria.path.AbstractPathImpl.illegalDereference(AbstractPathImpl.java:98) org.hibernate.jpa.criteria.path.AbstractPathImpl.get(AbstractPathImpl.java:191) br.com.caelum.dao.ProdutoDao.getProdutos(ProdutoDao.java:43) br.com.caelum.controller.ProdutoController.buscarPor(ProdutoController.java:69) br.com.caelum.controller.ProdutoController$$FastClassBySpringCGLIB$$888d3969.invoke() org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:649) br.com.caelum.controller.ProdutoController$$EnhancerBySpringCGLIB$$ed8d0dcf.buscarPor() sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) java.lang.reflect.Method.invoke(Method.java:498) org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221) org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137) org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110) org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:776) org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:705) org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959) org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893) org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966) org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:868) javax.servlet.http.HttpServlet.service(HttpServlet.java:661) org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842) javax.servlet.http.HttpServlet.service(HttpServlet.java:742) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) Note The full stack trace of the root cause is available in the server logs.

Olha esta parte do log de erro:

[null.categorias] 

Aparentemente esta sua linha está com erro:

 Path<Integer> categoriaPath = root.<Loja>get("categorias").<Integer>get("id");

Se ela estiver certo, dá um revisada mapeamento da sua entidade, no campo categorias.

o Correto é isso aqui, eu vacilei nisso...

Path categoriaPath = root.join("categorias").get("id");

Mas agora estou no seguinte erro: Exception Report

Message Request processing failed; nested exception is java.lang.IllegalStateException: EntityManager is closed

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalStateException: EntityManager is closed org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978) org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:868) javax.servlet.http.HttpServlet.service(HttpServlet.java:661) org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842) javax.servlet.http.HttpServlet.service(HttpServlet.java:742) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

Root Cause java.lang.IllegalStateException: EntityManager is closed org.hibernate.jpa.internal.EntityManagerImpl.checkOpen(EntityManagerImpl.java:105) org.hibernate.jpa.internal.QueryImpl.getResultList(QueryImpl.java:445) org.hibernate.jpa.criteria.compile.CriteriaQueryTypeQueryAdapter.getResultList(CriteriaQueryTypeQueryAdapter.java:67) sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) java.lang.reflect.Method.invoke(Method.java:498) org.springframework.orm.jpa.SharedEntityManagerCreator$DeferredQueryInvocationHandler.invoke(SharedEntityManagerCreator.java:362) com.sun.proxy.$Proxy47.getResultList(Unknown Source) br.com.caelum.dao.ProdutoDao.getProdutos(ProdutoDao.java:65) br.com.caelum.controller.ProdutoController.buscarPor(ProdutoController.java:69) br.com.caelum.controller.ProdutoController$$FastClassBySpringCGLIB$$888d3969.invoke() org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:649) br.com.caelum.controller.ProdutoController$$EnhancerBySpringCGLIB$$be49e576.buscarPor() sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) java.lang.reflect.Method.invoke(Method.java:498) org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221) org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137) org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110) org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:776) org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:705) org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959) org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893) org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966) org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:868) javax.servlet.http.HttpServlet.service(HttpServlet.java:661) org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842) javax.servlet.http.HttpServlet.service(HttpServlet.java:742) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

Note The full stack trace of the root cause is available in the server logs.

Apache Tomcat/8.5.38

A busca ainda n funciona