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

Erro 500 utilizando o Método DELETE

Estava utilizando o método DELETE normalmente mas depois de tentar as injeções com CDI aparece o seguinte erro.

HTTP Status 500 - net.vidageek.mirror.exception.ReflectionProviderException: Could not invoke method remove.

No console reparei isto:

17:15:47,669 DEBUG [DefaultExceptionMapper] find for exception class net.vidageek.mirror.exception.ReflectionProviderException
17:15:47,669 DEBUG [DefaultExceptionMapper] find for exception class java.lang.IllegalArgumentException
mar 25, 2017 5:15:47 PM org.apache.catalina.core.StandardWrapperValve invoke
GRAVE: Servlet.service() for servlet [default] in context with path [/vraptor-produtos] threw exception [net.vidageek.mirror.exception.ReflectionProviderException: Could not invoke method remove] with root cause
java.lang.IllegalArgumentException: Removing a detached instance br.com.caelum.vraptor.model.Produto#3
    at org.hibernate.jpa.event.internal.core.JpaDeleteEventListener.performDetachedEntityDeletionCheck(JpaDeleteEventListener.java:69)

Códigos:

JSP:


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<link rel="stylesheet" type="text/css"
    href="../bootstrap/css/bootstrap.css">
<head>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Lista de produtos</title>
</head>
<body>
    <div class="container">
        <h1>Listagem de produtos</h1>
        <table class="table table-stripped table-hover table-bordered">
            <thead>
                <tr>
                    <th>Nome</th>
                    <th>Valor</th>
                    <th>Quantidade</th>
                </tr>
            </thead>
            <tbody>
                <c:forEach items="${produtoList}" var="produto">
                    <tr>
                        <td>${produto.nome}</td>
                        <td>${produto.valor}</td>
                        <td>${produto.quantidade}</td>
                        <td><a href="<c:url value='/produto/remove?produto.id=${produto.id}'/>">Remover</a></td>
                    </tr>
                </c:forEach>
            </tbody>
        </table>


        <a href="<c:url value='/produto/formulario'/>"> Adicionar mais produtos!</a>
    <c:if test="${not empty mensagem}">
        <div class="alert alert-success">${mensagem}</div>
    </c:if>
    <a href="<c:url value='/produto/listaEmXml'/>" >Lista em xml</a>
    </div>
</body>
</html>

Controller:

package br.com.caelum.vraptor.controller;

import javax.inject.Inject;

import br.com.caelum.vraptor.Controller;
import br.com.caelum.vraptor.Get;
import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Post;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.dao.ProdutoDao;
import br.com.caelum.vraptor.model.Produto;
import br.com.caelum.vraptor.view.Results;

@Controller
public class ProdutoController {

    private final Result result;
    private final ProdutoDao dao;


    @Inject
    public ProdutoController(Result result, ProdutoDao dao) {
        this.result = result;
        this.dao = dao;
    }


    @Deprecated
    public ProdutoController() {
        this(null,null);
    }



    @Get("/") 
    public void inicio(){

    }

    @Get("/produto/lista")
    public void lista(){

        result.include("produtoList", dao.lista());

    }

    @Get("/produto/sobre")
    public void sobre(){

    }

    @Get("/produto/formulario")
    public void formulario(){

    }

    @Get
    public void listaEmXml() {
        result.use(Results.xml()).from(dao.lista()).serialize();

    }

    @Post("/produto/adiciona")
    public void adiciona(Produto produto){
        dao.adiciona(produto);;
        result.include("mensagem", "Produto adicionado com sucesso!");
        result.redirectTo(this).lista();

    }

    @Path("/produto/remove")
    public void remove(Produto produto){

        dao.remove(produto);
        result.include("mensagem", "Produto removido com sucesso!");
        result.redirectTo(this).lista();
    }
}

DAO:

package br.com.caelum.vraptor.dao;

import java.util.List;

import javax.inject.Inject;
import javax.persistence.EntityManager;

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

public class ProdutoDao {

    private final EntityManager em;

    @Inject
    public ProdutoDao(EntityManager em) {
        this.em = em;
    }

    @Deprecated
    ProdutoDao() {
        this(null); // para uso do CDI
    }

    public void adiciona(Produto produto) {
        em.getTransaction().begin();
        em.persist(produto);
        em.getTransaction().commit();
    }

    public void remove(Produto produto) {
        em.getTransaction().begin();
        em.remove(produto);
        em.getTransaction().commit();
    }

    public Produto busca(Produto produto) {
        return em.find(Produto.class, produto.getId());
    }

    @SuppressWarnings("unchecked")
    public List<Produto> lista() {
        return em.createQuery("select p from Produto p").getResultList();
    }
}

Producer

package br.com.caelum.vraptor.produces;

import javax.enterprise.context.RequestScoped;
import javax.enterprise.inject.Disposes;
import javax.enterprise.inject.Produces;
import javax.persistence.EntityManager;

import br.com.caelum.vraptor.util.JPAUtil;

public class EntityManagerProducer {

    @Produces @RequestScoped
    public EntityManager criaEntityManager(){
        return JPAUtil.criaEntityManager();
    }    

    public void close(@Disposes EntityManager manager){
        manager.close();
    }
}
12 respostas

oi Matheus

Deve explicar melhor o erro no resto da stacktrace. Copie e cole ela inteiramente pra gente!

Segue a stacktrace:

16:35:44,243 DEBUG [DefaultControllerTranslator] trying to access /produto/lista
16:35:44,250 DEBUG [DefaultControllerTranslator] found controller [DefaultControllerMethod: public void br.com.caelum.vraptor.controller.ProdutoController.lista()]
16:35:44,301 DEBUG [CDIBasedContainer   ] asking cdi to get instance for class br.com.caelum.vraptor.controller.ProdutoController
16:35:44,302 DEBUG [CDIBasedContainer   ] beans for class br.com.caelum.vraptor.controller.ProdutoController is [Managed Bean [class br.com.caelum.vraptor.controller.ProdutoController] with qualifiers [@Any @Default]]
16:35:44,354 DEBUG [IogiParametersProvider] IogiParametersProvider is up
16:35:44,524 DEBUG [CDIBasedContainer   ] asking cdi to get instance for class br.com.caelum.vraptor.interceptor.ExceptionHandlerInterceptor
16:35:44,524 DEBUG [CDIBasedContainer   ] beans for class br.com.caelum.vraptor.interceptor.ExceptionHandlerInterceptor is [Managed Bean [class br.com.caelum.vraptor.interceptor.ExceptionHandlerInterceptor] with qualifiers [@Any @Default]]
16:35:44,578 DEBUG [ToInstantiateInterceptorHandler] Invoking interceptor ExceptionHandlerInterceptor$Proxy$_$$_WeldClientProxy
16:35:44,581 DEBUG [CDIBasedContainer   ] asking cdi to get instance for class br.com.caelum.vraptor.interceptor.FlashInterceptor
16:35:44,582 DEBUG [CDIBasedContainer   ] beans for class br.com.caelum.vraptor.interceptor.FlashInterceptor is [Managed Bean [class br.com.caelum.vraptor.interceptor.FlashInterceptor] with qualifiers [@Any @Default]]
16:35:44,612 DEBUG [ToInstantiateInterceptorHandler] Invoking interceptor FlashInterceptor$Proxy$_$$_WeldClientProxy
16:35:44,710 DEBUG [MethodValidator     ] method [DefaultControllerMethod: public void br.com.caelum.vraptor.controller.ProdutoController.lista()] has no parameters, skipping
16:35:44,723 DEBUG [ParanamerNameProvider] Found parameter names with paranamer for public void br.com.caelum.vraptor.controller.ProdutoController.lista() as []
16:35:44,743 DEBUG [ExecuteMethod       ] Invoking public void br.com.caelum.vraptor.controller.ProdutoController.lista()
16:35:45,211 DEBUG [DefaultControllerTranslator] trying to access /produto/lista
16:35:45,211 DEBUG [DefaultControllerTranslator] found controller [DefaultControllerMethod: public void br.com.caelum.vraptor.controller.ProdutoController.lista()]
16:35:45,211 DEBUG [CDIBasedContainer   ] asking cdi to get instance for class br.com.caelum.vraptor.controller.ProdutoController
16:35:45,212 DEBUG [CDIBasedContainer   ] beans for class br.com.caelum.vraptor.controller.ProdutoController is [Managed Bean [class br.com.caelum.vraptor.controller.ProdutoController] with qualifiers [@Any @Default]]
16:35:45,212 DEBUG [CDIBasedContainer   ] asking cdi to get instance for class br.com.caelum.vraptor.interceptor.ExceptionHandlerInterceptor
16:35:45,213 DEBUG [CDIBasedContainer   ] beans for class br.com.caelum.vraptor.interceptor.ExceptionHandlerInterceptor is [Managed Bean [class br.com.caelum.vraptor.interceptor.ExceptionHandlerInterceptor] with qualifiers [@Any @Default]]
16:35:45,213 DEBUG [ToInstantiateInterceptorHandler] Invoking interceptor ExceptionHandlerInterceptor$Proxy$_$$_WeldClientProxy
16:35:45,213 DEBUG [CDIBasedContainer   ] asking cdi to get instance for class br.com.caelum.vraptor.interceptor.FlashInterceptor
16:35:45,213 DEBUG [CDIBasedContainer   ] beans for class br.com.caelum.vraptor.interceptor.FlashInterceptor is [Managed Bean [class br.com.caelum.vraptor.interceptor.FlashInterceptor] with qualifiers [@Any @Default]]
16:35:45,213 DEBUG [ToInstantiateInterceptorHandler] Invoking interceptor FlashInterceptor$Proxy$_$$_WeldClientProxy
16:35:45,214 DEBUG [MethodValidator     ] method [DefaultControllerMethod: public void br.com.caelum.vraptor.controller.ProdutoController.lista()] has no parameters, skipping
16:35:45,215 DEBUG [ParanamerNameProvider] Found parameter names with paranamer for public void br.com.caelum.vraptor.controller.ProdutoController.lista() as []
16:35:45,215 DEBUG [ExecuteMethod       ] Invoking public void br.com.caelum.vraptor.controller.ProdutoController.lista()
16:35:45,640 DEBUG [DefaultResult       ] including attribute produtoList: [br.com.caelum.vraptor.model.Produto@611cfaea, br.com.caelum.vraptor.model.Produto@577d25e9, br.com.caelum.vraptor.model.Produto@220e3339]
16:35:45,640 DEBUG [DefaultResult       ] including attribute produtoList: [br.com.caelum.vraptor.model.Produto@1c3d9ee, br.com.caelum.vraptor.model.Produto@52b02361, br.com.caelum.vraptor.model.Produto@4da39c70]
16:35:45,653 DEBUG [JstlLocalization    ] couldn't find message bundle, creating an empty one
16:35:45,653 DEBUG [JstlLocalization    ] couldn't find message bundle, creating an empty one
16:35:45,655 DEBUG [DefaultInterceptorStack] All registered interceptors have been called. End of VRaptor Request Execution.
16:35:45,656 DEBUG [ForwardToDefaultView] forwarding to the dafault page for this logic
16:35:45,656 DEBUG [CDIBasedContainer   ] asking cdi to get instance for interface br.com.caelum.vraptor.view.PageResult
16:35:45,657 DEBUG [CDIBasedContainer   ] beans for interface br.com.caelum.vraptor.view.PageResult is [Managed Bean [class br.com.caelum.vraptor.view.DefaultPageResult] with qualifiers [@Any @Default]]
16:35:45,658 DEBUG [DefaultInterceptorStack] All registered interceptors have been called. End of VRaptor Request Execution.
16:35:45,658 DEBUG [ForwardToDefaultView] forwarding to the dafault page for this logic
16:35:45,658 DEBUG [CDIBasedContainer   ] asking cdi to get instance for interface br.com.caelum.vraptor.view.PageResult
16:35:45,658 DEBUG [CDIBasedContainer   ] beans for interface br.com.caelum.vraptor.view.PageResult is [Managed Bean [class br.com.caelum.vraptor.view.DefaultPageResult] with qualifiers [@Any @Default]]
16:35:45,666 DEBUG [DefaultPathResolver ] Resolving path for [DefaultControllerMethod: public void br.com.caelum.vraptor.controller.ProdutoController.lista()]
16:35:45,667 DEBUG [DefaultPathResolver ] Resolving path for [DefaultControllerMethod: public void br.com.caelum.vraptor.controller.ProdutoController.lista()]
16:35:45,683 DEBUG [DefaultPathResolver ] Returning path /WEB-INF/jsp/produto/lista.jsp for [DefaultControllerMethod: public void br.com.caelum.vraptor.controller.ProdutoController.lista()]
16:35:45,683 DEBUG [DefaultPathResolver ] Returning path /WEB-INF/jsp/produto/lista.jsp for [DefaultControllerMethod: public void br.com.caelum.vraptor.controller.ProdutoController.lista()]
16:35:45,684 DEBUG [DefaultPageResult   ] forwarding to /WEB-INF/jsp/produto/lista.jsp
16:35:45,684 DEBUG [DefaultPageResult   ] forwarding to /WEB-INF/jsp/produto/lista.jsp
16:35:45,702 DEBUG [DefaultStaticContentHandler] Deferring request to container: /vraptor-produtos/WEB-INF/jsp/produto/lista.jsp 
16:35:45,702 DEBUG [DefaultStaticContentHandler] Deferring request to container: /vraptor-produtos/WEB-INF/jsp/produto/lista.jsp 
16:35:46,056 DEBUG [VRaptor             ] VRaptor ended the request
16:35:46,058 DEBUG [VRaptor             ] VRaptor ended the request
16:35:49,004 DEBUG [DefaultControllerTranslator] trying to access /produto/remove
16:35:49,005 DEBUG [DefaultControllerTranslator] found controller [DefaultControllerMethod: public void br.com.caelum.vraptor.controller.ProdutoController.remove(br.com.caelum.vraptor.model.Produto)]
16:35:49,005 DEBUG [CDIBasedContainer   ] asking cdi to get instance for class br.com.caelum.vraptor.controller.ProdutoController
16:35:49,005 DEBUG [CDIBasedContainer   ] beans for class br.com.caelum.vraptor.controller.ProdutoController is [Managed Bean [class br.com.caelum.vraptor.controller.ProdutoController] with qualifiers [@Any @Default]]
16:35:49,007 DEBUG [ParanamerNameProvider] Found parameter names with paranamer for public void br.com.caelum.vraptor.controller.ProdutoController.remove(br.com.caelum.vraptor.model.Produto) as [produto]
16:35:49,019 DEBUG [IogiParametersProvider] IogiParametersProvider is up
16:35:49,020 DEBUG [ParanamerNameProvider] Found parameter names with paranamer for public void br.com.caelum.vraptor.controller.ProdutoController.remove(br.com.caelum.vraptor.model.Produto) as [produto]
16:35:49,021 DEBUG [IogiParametersProvider] getParametersFor() called with parameters Parameters(Parameter(produto.id -> 3)) and targets [Target(name=produto, type=class br.com.caelum.vraptor.model.Produto)].
16:35:49,041 DEBUG [ParanamerNameProvider] Found parameter names with paranamer for public br.com.caelum.vraptor.model.Produto() as []
16:35:49,043 DEBUG [ParanamerNameProvider] Found parameter names with paranamer for public br.com.caelum.vraptor.model.Produto(java.lang.String,java.lang.Double,java.lang.Integer) as [nome, valor, quantidade]
16:35:49,048 DEBUG [CDIBasedContainer   ] asking cdi to get instance for class java.lang.String
16:35:49,048 DEBUG [CDIBasedContainer   ] beans for class java.lang.String is []
16:35:49,065 DEBUG [CDIBasedContainer   ] asking cdi to get instance for class br.com.caelum.vraptor.converter.LongConverter
16:35:49,065 DEBUG [CDIBasedContainer   ] beans for class br.com.caelum.vraptor.converter.LongConverter is [Managed Bean [class br.com.caelum.vraptor.converter.LongConverter] with qualifiers [@Any @Default]]
16:35:49,070 DEBUG [JstlLocalization    ] couldn't find message bundle, creating an empty one
16:35:49,070 DEBUG [ParametersInstantiator] Conversion errors: []
16:35:49,071 DEBUG [ParametersInstantiator] Parameter values for [DefaultControllerMethod: public void br.com.caelum.vraptor.controller.ProdutoController.remove(br.com.caelum.vraptor.model.Produto)] are [br.com.caelum.vraptor.model.Produto@5109637]
16:35:49,071 DEBUG [CDIBasedContainer   ] asking cdi to get instance for class br.com.caelum.vraptor.interceptor.ExceptionHandlerInterceptor
16:35:49,071 DEBUG [CDIBasedContainer   ] beans for class br.com.caelum.vraptor.interceptor.ExceptionHandlerInterceptor is [Managed Bean [class br.com.caelum.vraptor.interceptor.ExceptionHandlerInterceptor] with qualifiers [@Any @Default]]
16:35:49,071 DEBUG [ToInstantiateInterceptorHandler] Invoking interceptor ExceptionHandlerInterceptor$Proxy$_$$_WeldClientProxy
16:35:49,071 DEBUG [CDIBasedContainer   ] asking cdi to get instance for class br.com.caelum.vraptor.interceptor.FlashInterceptor
16:35:49,071 DEBUG [CDIBasedContainer   ] beans for class br.com.caelum.vraptor.interceptor.FlashInterceptor is [Managed Bean [class br.com.caelum.vraptor.interceptor.FlashInterceptor] with qualifiers [@Any @Default]]
16:35:49,071 DEBUG [ToInstantiateInterceptorHandler] Invoking interceptor FlashInterceptor$Proxy$_$$_WeldClientProxy
16:35:49,103 DEBUG [ExecuteMethod       ] Invoking public void br.com.caelum.vraptor.controller.ProdutoController.remove(br.com.caelum.vraptor.model.Produto)
16:35:49,304 DEBUG [DefaultExceptionMapper] find for exception class net.vidageek.mirror.exception.ReflectionProviderException
16:35:49,304 DEBUG [DefaultExceptionMapper] find for exception class java.lang.IllegalArgumentException
mar 27, 2017 4:35:49 PM org.apache.catalina.core.StandardWrapperValve invoke
GRAVE: Servlet.service() for servlet [default] in context with path [/vraptor-produtos] threw exception [net.vidageek.mirror.exception.ReflectionProviderException: Could not invoke method remove] with root cause
java.lang.IllegalArgumentException: Removing a detached instance br.com.caelum.vraptor.model.Produto#3
    at org.hibernate.jpa.event.internal.core.JpaDeleteEventListener.performDetachedEntityDeletionCheck(JpaDeleteEventListener.java:69)
    at org.hibernate.event.internal.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:106)
    at org.hibernate.event.internal.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:73)
    at org.hibernate.internal.SessionImpl.fireDelete(SessionImpl.java:916)
    at org.hibernate.internal.SessionImpl.delete(SessionImpl.java:892)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.remove(AbstractEntityManagerImpl.java:1214)
    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 org.jboss.weld.bean.proxy.AbstractBeanInstance.invoke(AbstractBeanInstance.java:40)
    at org.jboss.weld.bean.proxy.ProxyMethodHandler.invoke(ProxyMethodHandler.java:100)
    at org.jboss.weld.proxies.EntityManager$463022876$Proxy$_$$_WeldClientProxy.remove(Unknown Source)
    at br.com.caelum.vraptor.dao.ProdutoDao.remove(ProdutoDao.java:32)
    at br.com.caelum.vraptor.controller.ProdutoController.remove(ProdutoController.java:141)
    at br.com.caelum.vraptor.controller.ProdutoController$Proxy$_$$_WeldClientProxy.remove(Unknown Source)
    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 net.vidageek.mirror.provider.java.PureJavaMethodReflectionProvider.invoke(PureJavaMethodReflectionProvider.java:38)
    at net.vidageek.mirror.invoke.MethodHandlerByMethod.withArgs(MethodHandlerByMethod.java:54)
    at br.com.caelum.vraptor.observer.ExecuteMethod.execute(ExecuteMethod.java:87)
    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 org.jboss.weld.injection.MethodInjectionPoint.invokeOnInstanceWithSpecialValue(MethodInjectionPoint.java:93)
    at org.jboss.weld.event.ObserverMethodImpl.sendEvent(ObserverMethodImpl.java:266)
    at org.jboss.weld.event.ObserverMethodImpl.sendEvent(ObserverMethodImpl.java:253)
    at org.jboss.weld.event.ObserverMethodImpl.notify(ObserverMethodImpl.java:232)
    at org.jboss.weld.event.ObserverNotifier.notifyObserver(ObserverNotifier.java:169)
    at org.jboss.weld.event.ObserverNotifier.notifyObserver(ObserverNotifier.java:165)
    at org.jboss.weld.event.ObserverNotifier.notifyObservers(ObserverNotifier.java:119)
    at org.jboss.weld.event.ObserverNotifier.fireEvent(ObserverNotifier.java:112)
    at org.jboss.weld.event.EventImpl.fire(EventImpl.java:83)
    at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:78)
    at br.com.caelum.vraptor.interceptor.FlashInterceptor.intercept(FlashInterceptor.java:98)
    at br.com.caelum.vraptor.interceptor.FlashInterceptor$Proxy$_$$_WeldClientProxy.intercept(Unknown Source)
    at br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:58)
    at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:83)
    at br.com.caelum.vraptor.interceptor.ExceptionHandlerInterceptor.intercept(ExceptionHandlerInterceptor.java:75)
    at br.com.caelum.vraptor.interceptor.ExceptionHandlerInterceptor$Proxy$_$$_WeldClientProxy.intercept(Unknown Source)
    at br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:58)
    at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:83)
    at br.com.caelum.vraptor.core.DefaultInterceptorStack.start(DefaultInterceptorStack.java:93)
    at br.com.caelum.vraptor.core.DefaultInterceptorStack$Proxy$_$$_WeldClientProxy.start(Unknown Source)
    at br.com.caelum.vraptor.observer.RequestHandlerObserver.handle(RequestHandlerObserver.java:86)
    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 org.jboss.weld.injection.MethodInjectionPoint.invokeOnInstanceWithSpecialValue(MethodInjectionPoint.java:93)
    at org.jboss.weld.event.ObserverMethodImpl.sendEvent(ObserverMethodImpl.java:266)
    at org.jboss.weld.event.ObserverMethodImpl.sendEvent(ObserverMethodImpl.java:253)
    at org.jboss.weld.event.ObserverMethodImpl.notify(ObserverMethodImpl.java:232)
    at org.jboss.weld.event.ObserverNotifier.notifyObserver(ObserverNotifier.java:169)
    at org.jboss.weld.event.ObserverNotifier.notifyObserver(ObserverNotifier.java:165)
    at org.jboss.weld.event.ObserverNotifier.notifyObservers(ObserverNotifier.java:119)
    at org.jboss.weld.event.ObserverNotifier.fireEvent(ObserverNotifier.java:112)
    at org.jboss.weld.event.EventImpl.fire(EventImpl.java:83)
    at br.com.caelum.vraptor.VRaptor.doFilter(VRaptor.java:116)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:474)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:624)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:495)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:767)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1347)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    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)

Oi Matheus, tudo bem?

Isso está acontecendo porque a instancia do produto que você recebe no controller está em um estado do hibernate conhecido como detached: ou seja, é um valor que possívelmente existe no banco de dados, mas não há garantia disso para o seu EntityManager.

Uma forma simples pra resolver nesse seu caso seria dando o load do objeto, antes de apagar. Assim ele entra no estado managed, e pode ser gerenciado pelo EM.

public void remove(Produto produto) {
     em.getTransaction().begin();
     load(produto); // a entidade entra em estado managed
     em.remove(produto);
     em.getTransaction().commit();
}

experimenta e nos diz se funcionou?

ps: se quiser saber mais sobre isso, tem esse post bem legal do blog da caelum que explica em detalhes todos esses estados do hibernate.

http://blog.caelum.com.br/entidades-managed-transient-e-detached-no-hibernate-e-jpa/

É necessário improtar alguma biblioteca para utilizar o método load?

O meu ProdutoDao não reconhece o método.

perdão Matheus, no seu caso o método é o "busca", do ProdutoDao, que carrega o objeto do banco.

O mesmo problema.

Eu coloco o código load(produto) e o eclipse não reconhece o método.

Será que está faltando alguma biblioteca ou a instalação de alguma coisa?

public Produto busca(Produto produto) {
        load(produto);
        return em.find(Produto.class, produto.getId());
    }

experimente deixar seu código assim:

public Produto busca(Produto produto) {
    return em.find(Produto.class, produto.getId());
}

public void remove(Produto produto) {
     em.getTransaction().begin();
     busca(produto); // aqui o método se chama busca, não load.
     em.remove(produto);
     em.getTransaction().commit();
}

Ainda continua o mesmo problema, vou mandar a stacktrace.

18:34:01,415 DEBUG [DefaultControllerTranslator] trying to access /produto/remove
18:34:01,415 DEBUG [DefaultControllerTranslator] found controller [DefaultControllerMethod: public void br.com.caelum.vraptor.controller.ProdutoController.remove(br.com.caelum.vraptor.model.Produto)]
18:34:01,416 DEBUG [CDIBasedContainer   ] asking cdi to get instance for class br.com.caelum.vraptor.controller.ProdutoController
18:34:01,416 DEBUG [CDIBasedContainer   ] beans for class br.com.caelum.vraptor.controller.ProdutoController is [Managed Bean [class br.com.caelum.vraptor.controller.ProdutoController] with qualifiers [@Any @Default]]
18:34:01,419 DEBUG [ParanamerNameProvider] Found parameter names with paranamer for public void br.com.caelum.vraptor.controller.ProdutoController.remove(br.com.caelum.vraptor.model.Produto) as [produto]
18:34:01,437 DEBUG [IogiParametersProvider] IogiParametersProvider is up
18:34:01,438 DEBUG [ParanamerNameProvider] Found parameter names with paranamer for public void br.com.caelum.vraptor.controller.ProdutoController.remove(br.com.caelum.vraptor.model.Produto) as [produto]
18:34:01,440 DEBUG [IogiParametersProvider] getParametersFor() called with parameters Parameters(Parameter(produto.id -> 3)) and targets [Target(name=produto, type=class br.com.caelum.vraptor.model.Produto)].
18:34:01,476 DEBUG [ParanamerNameProvider] Found parameter names with paranamer for public br.com.caelum.vraptor.model.Produto() as []
18:34:01,478 DEBUG [ParanamerNameProvider] Found parameter names with paranamer for public br.com.caelum.vraptor.model.Produto(java.lang.String,java.lang.Double,java.lang.Integer) as [nome, valor, quantidade]
18:34:01,489 DEBUG [CDIBasedContainer   ] asking cdi to get instance for class java.lang.String
18:34:01,489 DEBUG [CDIBasedContainer   ] beans for class java.lang.String is []
18:34:01,514 DEBUG [CDIBasedContainer   ] asking cdi to get instance for class br.com.caelum.vraptor.converter.LongConverter
18:34:01,514 DEBUG [CDIBasedContainer   ] beans for class br.com.caelum.vraptor.converter.LongConverter is [Managed Bean [class br.com.caelum.vraptor.converter.LongConverter] with qualifiers [@Any @Default]]
18:34:01,521 DEBUG [JstlLocalization    ] couldn't find message bundle, creating an empty one
18:34:01,522 DEBUG [ParametersInstantiator] Conversion errors: []
18:34:01,523 DEBUG [ParametersInstantiator] Parameter values for [DefaultControllerMethod: public void br.com.caelum.vraptor.controller.ProdutoController.remove(br.com.caelum.vraptor.model.Produto)] are [br.com.caelum.vraptor.model.Produto@5d51a0bb]
18:34:01,523 DEBUG [CDIBasedContainer   ] asking cdi to get instance for class br.com.caelum.vraptor.interceptor.ExceptionHandlerInterceptor
18:34:01,523 DEBUG [CDIBasedContainer   ] beans for class br.com.caelum.vraptor.interceptor.ExceptionHandlerInterceptor is [Managed Bean [class br.com.caelum.vraptor.interceptor.ExceptionHandlerInterceptor] with qualifiers [@Any @Default]]
18:34:01,524 DEBUG [ToInstantiateInterceptorHandler] Invoking interceptor ExceptionHandlerInterceptor$Proxy$_$$_WeldClientProxy
18:34:01,524 DEBUG [CDIBasedContainer   ] asking cdi to get instance for class br.com.caelum.vraptor.interceptor.FlashInterceptor
18:34:01,524 DEBUG [CDIBasedContainer   ] beans for class br.com.caelum.vraptor.interceptor.FlashInterceptor is [Managed Bean [class br.com.caelum.vraptor.interceptor.FlashInterceptor] with qualifiers [@Any @Default]]
18:34:01,524 DEBUG [ToInstantiateInterceptorHandler] Invoking interceptor FlashInterceptor$Proxy$_$$_WeldClientProxy
18:34:01,582 DEBUG [ExecuteMethod       ] Invoking public void br.com.caelum.vraptor.controller.ProdutoController.remove(br.com.caelum.vraptor.model.Produto)
18:34:01,813 DEBUG [DefaultExceptionMapper] find for exception class net.vidageek.mirror.exception.ReflectionProviderException
18:34:01,813 DEBUG [DefaultExceptionMapper] find for exception class java.lang.IllegalArgumentException
mar 27, 2017 6:34:01 PM org.apache.catalina.core.StandardWrapperValve invoke
GRAVE: Servlet.service() for servlet [default] in context with path [/vraptor-produtos] threw exception [net.vidageek.mirror.exception.ReflectionProviderException: Could not invoke method remove] with root cause
java.lang.IllegalArgumentException: Removing a detached instance br.com.caelum.vraptor.model.Produto#3
    at org.hibernate.jpa.event.internal.core.JpaDeleteEventListener.performDetachedEntityDeletionCheck(JpaDeleteEventListener.java:69)
    at org.hibernate.event.internal.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:106)
    at org.hibernate.event.internal.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:73)
    at org.hibernate.internal.SessionImpl.fireDelete(SessionImpl.java:916)
    at org.hibernate.internal.SessionImpl.delete(SessionImpl.java:892)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.remove(AbstractEntityManagerImpl.java:1214)
    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 org.jboss.weld.bean.proxy.AbstractBeanInstance.invoke(AbstractBeanInstance.java:40)
    at org.jboss.weld.bean.proxy.ProxyMethodHandler.invoke(ProxyMethodHandler.java:100)
    at org.jboss.weld.proxies.EntityManager$463022876$Proxy$_$$_WeldClientProxy.remove(Unknown Source)
    at br.com.caelum.vraptor.dao.ProdutoDao.remove(ProdutoDao.java:33)
    at br.com.caelum.vraptor.controller.ProdutoController.remove(ProdutoController.java:141)
    at br.com.caelum.vraptor.controller.ProdutoController$Proxy$_$$_WeldClientProxy.remove(Unknown Source)
    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 net.vidageek.mirror.provider.java.PureJavaMethodReflectionProvider.invoke(PureJavaMethodReflectionProvider.java:38)
    at net.vidageek.mirror.invoke.MethodHandlerByMethod.withArgs(MethodHandlerByMethod.java:54)
    at br.com.caelum.vraptor.observer.ExecuteMethod.execute(ExecuteMethod.java:87)
    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 org.jboss.weld.injection.MethodInjectionPoint.invokeOnInstanceWithSpecialValue(MethodInjectionPoint.java:93)
    at org.jboss.weld.event.ObserverMethodImpl.sendEvent(ObserverMethodImpl.java:266)
    at org.jboss.weld.event.ObserverMethodImpl.sendEvent(ObserverMethodImpl.java:253)
    at org.jboss.weld.event.ObserverMethodImpl.notify(ObserverMethodImpl.java:232)
    at org.jboss.weld.event.ObserverNotifier.notifyObserver(ObserverNotifier.java:169)
    at org.jboss.weld.event.ObserverNotifier.notifyObserver(ObserverNotifier.java:165)
    at org.jboss.weld.event.ObserverNotifier.notifyObservers(ObserverNotifier.java:119)
    at org.jboss.weld.event.ObserverNotifier.fireEvent(ObserverNotifier.java:112)
    at org.jboss.weld.event.EventImpl.fire(EventImpl.java:83)
    at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:78)
    at br.com.caelum.vraptor.interceptor.FlashInterceptor.intercept(FlashInterceptor.java:98)
    at br.com.caelum.vraptor.interceptor.FlashInterceptor$Proxy$_$$_WeldClientProxy.intercept(Unknown Source)
    at br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:58)
    at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:83)
    at br.com.caelum.vraptor.interceptor.ExceptionHandlerInterceptor.intercept(ExceptionHandlerInterceptor.java:75)
    at br.com.caelum.vraptor.interceptor.ExceptionHandlerInterceptor$Proxy$_$$_WeldClientProxy.intercept(Unknown Source)
    at br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:58)
    at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:83)
    at br.com.caelum.vraptor.core.DefaultInterceptorStack.start(DefaultInterceptorStack.java:93)
    at br.com.caelum.vraptor.core.DefaultInterceptorStack$Proxy$_$$_WeldClientProxy.start(Unknown Source)
    at br.com.caelum.vraptor.observer.RequestHandlerObserver.handle(RequestHandlerObserver.java:86)
    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 org.jboss.weld.injection.MethodInjectionPoint.invokeOnInstanceWithSpecialValue(MethodInjectionPoint.java:93)
    at org.jboss.weld.event.ObserverMethodImpl.sendEvent(ObserverMethodImpl.java:266)
    at org.jboss.weld.event.ObserverMethodImpl.sendEvent(ObserverMethodImpl.java:253)
    at org.jboss.weld.event.ObserverMethodImpl.notify(ObserverMethodImpl.java:232)
    at org.jboss.weld.event.ObserverNotifier.notifyObserver(ObserverNotifier.java:169)
    at org.jboss.weld.event.ObserverNotifier.notifyObserver(ObserverNotifier.java:165)
    at org.jboss.weld.event.ObserverNotifier.notifyObservers(ObserverNotifier.java:119)
    at org.jboss.weld.event.ObserverNotifier.fireEvent(ObserverNotifier.java:112)
    at org.jboss.weld.event.EventImpl.fire(EventImpl.java:83)
    at br.com.caelum.vraptor.VRaptor.doFilter(VRaptor.java:116)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:474)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:624)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:495)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:767)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1347)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    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)

Os dois métodos do DAO ficaram assim:

public void remove(Produto produto) {
        em.getTransaction().begin();
        busca(produto);
        em.remove(produto);
        em.getTransaction().commit();
    }

    public Produto busca(Produto produto) {
        return em.find(Produto.class, produto.getId());
    }
solução!

experimenta trocar o método pra ficar assim?

public void remove(Produto produto) {
        em.getTransaction().begin();
        produto = busca(produto);
        em.remove(produto);
        em.getTransaction().commit();
}

em hibernate puro bastaria o load. em JPA parece precisar reatribuir.

O problema do delete foi resolvido mas eu vi que o método autentica está com o mesmo problema.

Posso colocar os códigos aqui mesmo ou preciso abrir outro tópico?

Matheus, como está totalmente relacionado, acho que pode colocar aqui sim! Mas a solução sera a mesma se o erro está falando de detached: para deletar, voce precisa estar com um objeto vindo da session, entao faça o load dele mesmo

Já consegui resolver, obrigado pela ajuda.