4 Replies Latest reply on Jul 17, 2007 3:16 PM by Chris Lenz

    Removing a detached instance

    Chris Lenz Newbie

      Maybe thats a simple thing, but I have the following situation:
      JBOSS 4.2
      JSF
      EJB3

      public class SpielplatzCrudBean {
       private static SpielplatzDAOLocal dao = (SpielplatzDAOLocal)LookupService.INSTANCE.lookup(SpielplatzDAOLocal.LOCALNAME);
       Spielplatz selectedSpielplatz;
       ............
       public String delete()
       {
       String id = FacesContext.getCurrentInstance()
       .getExternalContext().getRequestParameterMap()
       .get("selectedId");
       selectedSpielplatz = dao.findById(Long.valueOf(id), false);
       dao.merge(selectedSpielplatz);
       dao.makeTransient(selectedSpielplatz);
       selectedSpielplatz=null;
       return "list";
       }
      }
      

      when I call the delete method I always get the removie a detached instance exception.
      I tried a lot but have no more idea.
      Should I add somthing to the persistence.xml or have I to reconfigure my faces?

      Please Help.
      Chris

        • 1. Re: Removing a detached instance
          wayne baylor Apprentice

          The call to merge() returns a managed copy of the entity. You should pass this reference to makeTransient().

          ...
          selectedSpielplatz = dao.findById(Long.valueOf(id), false);
          Spielplatz mergedObject = dao.merge(selectedSpielplatz);
          dao.makeTransient(mergedObject);
          ...


          • 2. Re: Removing a detached instance
            Chris Lenz Newbie

            Still the same error:

             public String delete()
             {
             String id = FacesContext.getCurrentInstance()
             .getExternalContext().getRequestParameterMap()
             .get("selectedId");
             selectedSpielplatz = dao.findById(Long.valueOf(id), false);
             selectedSpielplatz=dao.merge(selectedSpielplatz);
             dao.makeTransient(selectedSpielplatz);
             selectedSpielplatz=null;
             return "list";
             }
            

            But I dont understand, that if I load the object(selectedspielplatz) why it is not managed.
            Why are the objects always detached?
            I tried the same thing with glassfish and toplink and I haven't this problem.
            Is it really necessary always to merge before making an operation, or is it possible to reconfigure something, maybe entity manager per session?


            • 3. Re: Removing a detached instance
              Carlo de Wolf Master

              SpeilplatzCrudBean is probably a JSF bean. In that case it's living on the other side (in web space). Thus an entity bean which is returned by the DAO is first detached. You should have this code in a session bean.

              • 4. Re: Removing a detached instance
                Chris Lenz Newbie

                Thanx, that was my problem.
                is there something like a designpattern for JSF and J2EE?

                Thanx Chris