3 Replies Latest reply on May 10, 2006 4:47 PM by blowteam

    Error: detached entity passed to persist

    fourierxform

      Hi,

      I have been scratching my head for days trying to figure out this error message. I went through the EJB3 Trailblazer using the JBoss-IDE. The error is shown below:

      [CODE]
      [[AuthorServlet]] Servlet.service() for servlet AuthorServlet threw exception
      javax.ejb.EJBException: null; CausedByException is:
      detached entity passed to persist: org.jboss.ejb3demo.Author
      org.jboss.ejb3.tx.Ejb3TxPolicy.handleExceptionInOurTx(Ejb3TxPolicy.java:46)
      org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:70)
      org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:134)
      org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:88)
      org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:72)
      org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:88)
      org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:39)
      org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:88)
      org.jboss.aspects.security.AuthenticationInterceptor.invoke(AuthenticationInterceptor.java:63)
      org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:88)
      org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:93)
      org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:88)
      org.jboss.ejb3.stateless.StatelessContainer.dynamicInvoke(StatelessContainer.java:183)
      org.jboss.aop.Dispatcher.invoke(Dispatcher.java:107)
      org.jboss.aspects.remoting.IsLocalInterceptor.invoke(IsLocalInterceptor.java:30)
      org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:88)
      org.jboss.ejb3.stateless.StatelessRemoteProxy.invoke(StatelessRemoteProxy.java:79)
      $Proxy87.addArticle(Unknown Source)
      org.jboss.ejb3demo.web.AuthorServlet.createArticles(AuthorServlet.java:66)
      org.jboss.ejb3demo.web.AuthorServlet.doTask(AuthorServlet.java:53)
      org.jboss.ejb3demo.web.AuthorServlet.doPost(AuthorServlet.java:40)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
      org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)
      [/CODE]

      Here is the relevant code:

      [B]AuthorServlet.java[/B]
      [CODE]
      package org.jboss.ejb3demo.web;

      import java.io.IOException;
      import java.io.PrintWriter;
      import java.lang.reflect.UndeclaredThrowableException;

      import javax.naming.Context;
      import javax.naming.InitialContext;
      import javax.naming.NamingException;
      import javax.servlet.ServletException;
      import javax.servlet.http.HttpServlet;
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;

      import org.jboss.ejb3demo.Article;
      import org.jboss.ejb3demo.Author;
      import org.jboss.ejb3demo.Authors;

      public class AuthorServlet extends HttpServlet {
      private Authors authorsBean;

      public void init() throws ServletException {
      try {
      InitialContext context = new InitialContext();
      authorsBean = (Authors)context.lookup(Authors.class.getName());
      } catch (NamingException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      }
      }
      protected void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException
      {
      this.doTask(req, resp);
      }

      protected void doPost(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException
      {
      this.doTask(req, resp);
      }

      private void doTask(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException
      {
      String mode = req.getParameter("mode");
      if (mode == null)
      mode = "show";

      if (mode.equals("show")) {
      this.showArticles(req, resp);
      } else if (mode.equals("create")) {
      this.createArticles(req, resp);
      }

      }

      private void createArticles(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException
      {
      try {
      Author marshall = authorsBean.addAuthor("Marshall");
      Author max = authorsBean.addAuthor("Max");

      authorsBean.addArticle(marshall, "marshall's 1st article", "this is an article!");
      authorsBean.addArticle(marshall, "marshall's 2nd article", "this is also an article!");
      authorsBean.addArticle(max, "max's 1st article", "this is an article!");
      authorsBean.addArticle(max, "max's 2nd article", "this is also an article!");

      showArticles(req, resp);
      } catch (UndeclaredThrowableException e) {
      e.getUndeclaredThrowable().printStackTrace();
      }
      }

      private void showArticles(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException
      {
      PrintWriter out = resp.getWriter();
      resp.setContentType("text/html");
      out.println("");
      out.println("");
      out.println("");
      for (Author author : authorsBean.getAllAuthors()) {
      for (Article article : author.getArticles()) {
      out.println("" + article.getTitle() + " by " + author.getName() + "");
      out.println("  " + article.getBody() + "");
      }
      }
      out.println("<form method=\"POST\" action=\"authors\">");
      out.println("<input type=\"hidden\" name=\"mode\" value=\"create\">");
      out.println("<input type=\"submit\" value=\"Create Articles\">");
      out.println("");
      out.println("");
      out.println("");
      }
      }
      [/CODE]

      [B]Authors.java[/B]
      package org.jboss.ejb3demo;

      import java.util.List;

      import javax.ejb.Remote;

      @Remote
      public interface Authors {
      public List getAllAuthors();
      public Author addAuthor(String name);
      public void addArticle(Author author, String title, String body);
      }
      [/CODE]

      [B]AuthorsBean.java[/B]
      [CODE]
      package org.jboss.ejb3demo;

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

      import javax.ejb.Stateless;
      import javax.persistence.EntityManager;
      import javax.persistence.PersistenceContext;
      import javax.persistence.Query;

      public @Stateless class AuthorsBean implements Authors {

      @PersistenceContext
      protected EntityManager manager;

      public void addArticle(Author author, String title, String body) {
      manager.persist(author);
      author.addArticle(title, body);
      }

      public Author addAuthor(String name) {
      Author author = new Author();
      author.setName(name);
      return author;
      }

      public List getAllAuthors() {
      ArrayList authors = new ArrayList();
      Query q = manager.createQuery("from Author");
      for (Object o : q.getResultList()) {
      authors.add((Author) o);
      }
      return authors;
      }

      }
      [/CODE]

      [B]Author.java[/B]
      [CODE]
      package org.jboss.ejb3demo;

      import java.util.ArrayList;
      import java.util.Collection;

      import javax.persistence.CascadeType;
      import javax.persistence.Column;
      import javax.persistence.Entity;
      import javax.persistence.FetchType;
      import javax.persistence.GeneratorType;
      import javax.persistence.Id;
      import javax.persistence.JoinColumn;
      import javax.persistence.OneToMany;
      import javax.persistence.Table;

      @Entity
      @Table(name = "AUTHORS")
      public class Author {
      private int authorId;
      private String name;
      private Collection articles;

      @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "author")
      @JoinColumn(name = "authorId")
      public Collection getArticles() {
      return articles;
      }
      public void setArticles(Collection articles) {
      this.articles = articles;
      }

      @Id (generate = GeneratorType.AUTO)
      @Column(name = "authorId")
      public int getAuthorId() {
      return authorId;
      }

      public void setAuthorId(int authorId) {
      this.authorId = authorId;
      }

      public String getName() {
      return name;
      }

      public void setName(String name) {
      this.name = name;
      }

      public void addArticle(String title, String body) {
      if (articles == null)
      articles = new ArrayList();
      Article article = new Article();
      article.setAuthor(this);
      article.setBody(body);
      article.setTitle(title);
      articles.add(article);
      }
      }
      [/CODE]

      Thanks!

        • 1. Re: Error: detached entity passed to persist

          Getting the same error. Can anyone help?

          Here are the steps I have taken:
          1. Download JBoss 4.0.3 RC1 ("all" config, installed using GUI installer) and JBoss IDE 1.5M2
          2. Download the code for JBoss IDE trailblazer 'articles' application
          3. Modified the code to replace @Inject with @PersistenceContext in AuthorsBean.java
          4. Tried to run the app, by going to http://localhost:8080/authors-web/authors and hitting "Create Articles". I get:

          19:46:43,312 ERROR [[Authors]] Servlet.service() for servlet Authors threw exception
          javax.ejb.EJBException: null; CausedByException is:
          detached entity passed to persist: org.jboss.ejb3demo.Author
          at org.jboss.ejb3.tx.Ejb3TxPolicy.handleExceptionInOurTx(Ejb3TxPolicy.java:46)
          at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:70)
          at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:134)
          at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:88)
          at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:72)
          at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:88)
          at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:39)
          at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:88)
          at org.jboss.aspects.security.AuthenticationInterceptor.invoke(AuthenticationInterceptor.java:63)
          at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:88)
          at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:93)
          at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:88)
          at org.jboss.ejb3.stateless.StatelessContainer.dynamicInvoke(StatelessContainer.java:183)
          at org.jboss.aop.Dispatcher.invoke(Dispatcher.java:107)
          at org.jboss.aspects.remoting.IsLocalInterceptor.invoke(IsLocalInterceptor.java:30)
          at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:88)
          at org.jboss.ejb3.stateless.StatelessRemoteProxy.invoke(StatelessRemoteProxy.java:79)
          at $Proxy73.addArticle(Unknown Source)
          at org.jboss.ejb3demo.web.AuthorServlet.createArticles(AuthorServlet.java:54)
          at org.jboss.ejb3demo.web.AuthorServlet.doPost(AuthorServlet.java:42)
          at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
          at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
          at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
          at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
          at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)
          at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
          at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
          at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
          at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
          at org.jboss.web.tomcat.security.CustomPrincipalValve.invoke(CustomPrincipalValve.java:39)
          at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:153)
          at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:59)
          at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
          at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
          at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
          at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
          at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856)
          at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744)
          at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
          at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112)
          at java.lang.Thread.run(Thread.java:595)
          org.hibernate.PersistentObjectException: detached entity passed to persist: org.jboss.ejb3demo.Author
          at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:79)
          at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:38)
          at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:531)
          at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:129)
          at org.jboss.ejb3.entity.InjectedEntityManager.persist(InjectedEntityManager.java:92)
          at org.jboss.ejb3demo.AuthorsBean.addArticle(AuthorsBean.java:36)
          at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
          at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
          at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
          at java.lang.reflect.Method.invoke(Method.java:585)
          at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:99)
          at org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsInterceptor.java:33)
          at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:88)
          at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:66)
          at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:134)
          at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:88)
          at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:72)
          at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:88)
          at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:39)
          at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:88)
          at org.jboss.aspects.security.AuthenticationInterceptor.invoke(AuthenticationInterceptor.java:63)
          at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:88)
          at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:93)
          at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:88)
          at org.jboss.ejb3.stateless.StatelessContainer.dynamicInvoke(StatelessContainer.java:183)
          at org.jboss.aop.Dispatcher.invoke(Dispatcher.java:107)
          at org.jboss.aspects.remoting.IsLocalInterceptor.invoke(IsLocalInterceptor.java:30)
          at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:88)
          at org.jboss.ejb3.stateless.StatelessRemoteProxy.invoke(StatelessRemoteProxy.java:79)
          at $Proxy73.addArticle(Unknown Source)
          at org.jboss.ejb3demo.web.AuthorServlet.createArticles(AuthorServlet.java:54)
          at org.jboss.ejb3demo.web.AuthorServlet.doPost(AuthorServlet.java:42)
          at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
          at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
          at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
          at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
          at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)
          at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
          at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
          at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
          at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
          at org.jboss.web.tomcat.security.CustomPrincipalValve.invoke(CustomPrincipalValve.java:39)
          at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:153)
          at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:59)
          at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
          at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
          at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
          at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
          at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856)
          at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744)
          at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
          at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112)
          at java.lang.Thread.run(Thread.java:595)



          • 2. Re: Error: detached entity passed to persist

            Never mind, it's working now.

            See http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3888831#3888831 for what I changed to make it work.

            Can someone please fix the JBoss IDE tutorial?

            While we are at it, can someone please fix the JBoss IDE so it works with Eclipse 3.1? I am using the bundled Eclipse build; the plug-in gives some problems with latest Eclipse, as you may already be aware.

            This is great stuff btw! Keep it up!

            • 3. Re: Error: detached entity passed to persist
              blowteam

              solution for this problem:

              REPLACE EntityManager.Persist(object) to EntityManager.merge(object)

              for detached objects..