0 Replies Latest reply on Jan 6, 2009 12:45 PM by Laurens Blankers

    JPA Entity attached scope?

    Laurens Blankers Newbie

      Hi,

      I'm using JBoss 5.0 with JPA for storing my data in a database. However at some point in my code my entities become detached from the persistence context when passing them to a different class.

      When exactly do entities get detached? Outside the method scope they were created in? Outside the class? Outside the JAR file they were created in? Or maybe when traversing a remote interface?

      More specific: I have an entity which looks like this:

      @Entity
      public class Customer implements java.io.Serializable {
       @Id
       private Long Id;
       private String firstName;
       [...]
      }
      


      And a interface to retrieve the entity:
      @Remote
      public interface CustomerDBAO {
       public Customer get(Long customerId);
       [...]
      }
      


      And of course a EJB to implement the interface:
      @Stateless
      public class CustomerDBAOBean implements CustomerDBAO {
       public Customer get(Long customerId) {
       return em.find(Customer.class, customerId);
       }
       [...]
      }
      


      All three are in data.jar. Then in ejb.jar with a class which retrieves the entity:

      @Stateless
      public class CustomerFacadeBean {
       @EJB
       CustomerDBAO customerDBAO;
       [...]
      }
      


      In the CustomerFacadeBean the Customer entity is detached. What do I need to change to make sure the entity stays attached? Put all classes in the same package? Change to a local interface? Or move the method to the CustomerDBAO class?

      Thanks