3 Replies Latest reply on May 11, 2006 10:25 AM by Norman Richards

    Using existing ejb3 entity beans in seam

    Rene Sprotte Newbie

      Hi,

      I would like to know how to use existing ejb3 entity beans in seam as a seam component? Lets assume the following scenario:

      I have a simple ejb3 package deployed in JBoss, which provides a persistent context (unitName = "openep") and a simple entity bean named User. Lets call this package PACKAGE_A.

      The User entity in PACKAGE_A has no @Name and @Scope annotations, so it is not a seam component. For the User entity in PACKAGE_A I don't want to have any dependencies to seam. It is just a plain entity bean.

      In a second package (PACKAGE_B), which is actually a seam application packaged as an ear, I want to use the existing entity bean from PACKAGE_A as a seam component. My application in PACKAGE_B is basically equivalent to the seam registration example except that I want to use my existing User entity from PACKAGE_A.

      My naive approach to make this existing User entity a seam component was to create a SeamUser class, which extends the existing User entity from PACKAGE_A.

      @Name("user")
      @Scope(SESSION)
      public class SeamUser extends User {
      
       private static final long serialVersionUID = -6805341268455291354L;
      
      }
      


      My registration action in PACKAGE_B looks like this:

      @Stateless
      @Name("register")
      public class RegisterAction implements Register {
      
       @In
       @Valid
       private SeamUser user;
      
       @PersistenceContext(unitName = "openep")
       private EntityManager em;
      
       @IfInvalid(outcome = Outcome.REDISPLAY)
       public String register() {
       em.persist(user);
       return "success";
       }
      
      }
      


      The deployment of PACKAGE_B works but when I try to register a new user I will get a

       javax.ejb.EJBException: java.lang.IllegalArgumentException: Unknown entity: com.openep.registration.SeamUser
      


      exception when calling em.persist(user). I tried to cast SeamUser to User but this leads to the same exception.

      ...
      em.persist((User)user);
      ...
      


      The following code works and does exacly what I want, but I like to know if there is a more elegant way to do it?

      @Stateless
      @Name("register")
      public class RegisterAction implements Register {
      
       @In
       @Valid
       private SeamUser user;
      
       @PersistenceContext(unitName = "openep")
       private EntityManager em;
      
       @IfInvalid(outcome = Outcome.REDISPLAY)
       public String register() {
       User myUser = new User(user.getUsername(), user.getPassword());
       em.persist(myUser);
       return "success";
       }
      
      }
      


      I hope that someone can give me a hint.

      Best regards, René