8 Replies Latest reply on Feb 21, 2012 4:42 PM by ole .

    Seam3 REST application and EntityManager

    ole . Newbie

      Hi,

       

      I have a REST and a normal web application using Seam 3.2. I'm using a stateless bean to manage all persistence operations.

       

      @Stateless(name="UserHelper", mappedName="UserHelper")
      public class UserHelper implements Serializable{
      
          @PersistenceContext
          private EntityManager em;
      
          /**
           * Add a customer to the service database.
           * 
           * @param customer
           * @return
           */
          public boolean addUser(User user) {
              try {
                  em.persist(user);
                  em.flush();
                  return true;
              } catch (Exception e) {
                  e.printStackTrace();
                  return false;
              }
      
      private UserHelper userHelper(){
              try {
                  return InitialContext.doLookup("java:global/core/UserHelper");
              } catch (NamingException e) {
                  e.printStackTrace();
              }
              return null;
          }
      

       

      This is properly working if I'm using this helper class in a managed bean. The problem is that I cannot persist anything by calling e.g. the addUser method from one of my REST classes.

      Example:

      @Path("/customer")
      @Stateless
      public class CustomerManager implements Serializable{
      
      
          @Path("/hello")
          @GET
          public String hello() throws NamingException{
              Customer cust = new Customer();
              cust.setEmail("asd@asdaaa.de");
              cust.setPin("123");
              userHelper().addUser(cust);
              return "Hello";
          }
      
      
      

       

       

       

      I get a log output like this:

       

      INFO: Hibernate: select nextval ('hibernate_sequence')

      INFO: Hibernate: insert into Users (city, countWrongPin, country, email, firstname, houseNo, isBlocked, lastname, phone, pin, state, street, zip, DTYPE, id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'Customer', ?)

       

      But it is not persisted and I dont know why?! As already mentioned this is properly working if I call a method of the userHelper from one of my managed beans.

       

      Any ideas what I'm doing wrong?

       

      The REST jersey app is also starting properly:

       

      INFO: CDI support is enabled

      INFO: GlobalStatsProvider registered

      INFO: Initiating Jersey application, version 'Jersey: 1.9.1 09/14/2011 02:05 PM'

      INFO: Instantiated the Application class my.app.ejb.rest.RESTApplication