[jboss-user] [EJB 3.0] - IllegalArgumentException on remove

Quake4Ialdaris do-not-reply at jboss.com
Sat Aug 4 21:34:23 EDT 2007


Hey guys, I'm trying to remove an entity. here's the code

Entity class:

@Entity
public class NewsEntity implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String title;
    private String body;

    public NewsEntity() {
    }
    public Long getId() {
        return this.id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (this.getId() != null ? this.getId().hashCode() : 0);
        return hash;
    }
    @Override
    public boolean equals(Object object)
    {
        if (!(object instanceof NewsEntity)) {
            return false;
        }
        NewsEntity other = (NewsEntity)object;
        if (this.getId() != other.getId() && (this.getId() == null || !this.getId().equals(other.getId()))) return false;
        return true;
    }
    public String getTitle()
    {
        return title;
    }
    public void setTitle(String title)
    {
        this.title = title;
    }
    public String getBody()
    {
        return body;
    }
    public void setBody(String body)
    {
        this.body = body;
    }
}

Here's my facade interface:

@Stateless
public class NewsEntityFacade implements NewsEntityFacadeLocal
{
   @PersistenceContext
   private EntityManager em;
   private Logger logger = Logger.getLogger (this.getClass ());
   public NewsEntityFacade ()
   {
   }
   public void create (NewsEntity newsEntity)
   {
      em.persist (newsEntity);
   }
   public void edit (NewsEntity newsEntity)
   {
      em.merge (newsEntity);
   }
   public void destroy (NewsEntity newsEntity)
   {
      em.merge (newsEntity);
      if( em.contains (newsEntity))
         logger.info ("The Entity is attached to current Persistence Context");
      else
         logger.error ("Entity is not attached!");
         /* throws exception */
         em.remove (newsEntity);
   }
   public NewsEntity find (Object pk)
   {
      return (NewsEntity) em.find (NewsEntity.class, pk);
   }
   public List findAll ()
   {
      return em.createQuery ("select object(o) from NewsEntity as o").getResultList ();
   }
}

Here is the code that looks up the proxy and performs the delete operation:

      NewsEntityFacadeLocal newsEntFacLocal = null;
      try
      {
         InitialContext ctx = new InitialContext ();
         newsEntFacLocal = (NewsEntityFacadeLocal)ctx.lookup
                 ("NewsApp/NewsEntityFacade/local");
      }
      catch(Exception ex)
      {
         logger.error ("Error in looking up NewsEntityFacadeLocal through");
         logger.error ("JDNI context " +JNDI_NEWS_ENTITY_EJB);
         ex.printStackTrace ();
      }
      
         //this record exists in data store
         NewsEntity ne = newsEntFacLocal.find (new Long(3));
         logger.info ("the entity found is: " + ne.toString ());
         nef.destroy (ne);

When I try this code I get an exception in the destroy method:

javax.ejb.EJBException: java.lang.IllegalArgumentException: Removing a detached instance ejb.NewsEntity#3
	org.jboss.ejb3.tx.Ejb3TxPolicy.handleExceptionInOurTx(Ejb3TxPolicy.java:69)
	org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:83)
	org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:191)
	org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
	org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:76)
	org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
	org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:62)
	org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
	org.jboss.aspects.security.AuthenticationInterceptor.invoke(AuthenticationInterceptor.java:77)
	org.jboss.ejb3.security.Ejb3AuthenticationInterceptor.invoke(Ejb3AuthenticationInterceptor.java:102)
	org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
	org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:47)
	org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
	org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:106)
	org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
	org.jboss.ejb3.stateless.StatelessContainer.localInvoke(StatelessContainer.java:211)
	org.jboss.ejb3.stateless.StatelessLocalProxy.invoke(StatelessLocalProxy.java:79)
	$Proxy286.destroy(Unknown Source)
	web.DeleteMessage.processRequest(DeleteMessage.java:59)
	web.DeleteMessage.doGet(DeleteMessage.java:123)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:697)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
	org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)

I can't see why it wouldn't be attached as the EntityManager javadocs say that merge attaches the entity in the PersistenceContext.

thanks

alessandro ferrucci :)

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4070917#4070917

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4070917



More information about the jboss-user mailing list