Error: detached entity passed to persist
fourierxform Jul 28, 2005 4:33 PMHi,
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!