title: Java & EJB3: How to correctly pass @Entity objects from Client to Server?
body: <p>I'm currently trying to pass an @Entity object from a stand-alone client to the server enterprise application by calling a business method on a facade that was auto-generated by Netbeans (Session Beans for Entity classes Wizard).</p>
  
  <p>The @Entity class consists of two String fields. But when the object arrives on the server side, all fields are "null", although I correctly initialized the entity object correctly on the client side before calling the remote method of the facade which in turn should store the object to my MySQL database (in fact it's stored, but with those null references). There are no errors and everything works just fine except passing @Entity object from client to server.</p>
  
  <p>The reverse way works. I manually added a row in my table and call the remote business method find(Object id) of the facade in order to get the entry of the db back to the client. Marshalling works just fine in this direction.</p>
  
  <p>Basically it consists of 3 projects (classes attached on the bottom):</p>
  
  <ol>
  <li>Enterprise-App/EJB: holds the implementation of the facade and JDBC connection to MySQL</li>
  <li>Java Class-Library: holds the interfaces for the remote business methods and the entity-bean (this one is shared (referenced) by the server- and client side)</li>
  <li>Java Application: builds InitialContext and executes the JNDI lookup to get the references to the remote business methods of the facade</li>
  </ol>
  
  <p>I face this problem now for two days and nobody has any clue how to solve. Internet search completely failed. Thus, any ideas are highly appreciated - Many thanks in advance!</p>
  
  <p>Any ideas highly appreciated.</p>
  
  <p>A1) EJB/Enterprise-App: Here's the abstract facade class (autogenerated by Netbeans):</p>
  
  <pre><code>public abstract class AbstractFacade&lt;T&gt; {
      private Class&lt;T&gt; entityClass;
  
      public AbstractFacade(Class&lt;T&gt; entityClass) {
          this.entityClass = entityClass;
      }
  
      protected abstract EntityManager getEntityManager();
  
      public void create(T entity) {
          getEntityManager().persist(entity);
      }
  
      public void edit(T entity) {
          getEntityManager().merge(entity);
      }
  
      public void remove(T entity) {
          getEntityManager().remove(getEntityManager().merge(entity));
      }
  
      public T find(Object id) {
          return getEntityManager().find(entityClass, id);
      }
  }
  </code></pre>
  
  <p>A2) EJB/Enterprise-App: the facade implementation (autogenerated by Netbeans):</p>
  
  <pre><code>@Stateless
  public class UserFacade extends AbstractFacade&lt;User&gt; implements UserFacadeRemote {
  
      @PersistenceContext(unitName = "LawSuiteEE-ejbPU")
      private EntityManager em;
  
      @Override
      protected EntityManager getEntityManager() {
          return em;
      }
  
      public UserFacade() {
          super(User.class);
      }
  
  }
  </code></pre>
  
  <p>B1) Shared Class-Library: Here's the entity class:</p>
  
  <pre><code>@Entity
  public class User implements Serializable {
  
      private static final long serialVersionUID = 1L;
  
      @Id
      @GeneratedValue(strategy = GenerationType.SEQUENCE)
      private Long id;
      private String username;
      private String password;
  
      public User() { }
  
      public User(Long id) {
          this.id = id;
      }
  
      public Long getId() {
          return id;
      }
  
      public void setId(Long id) {
          this.id = id;
      }
  
      public Integer getUsername() {
          return username;
      }
  
      public void setUsername(String username) {
          this.username = username;
      }
  
      public String getPassword() {
          return password;
      }
  
      public void setPassword(String password) {
          this.password = password;
      }
  
  }
  </code></pre>
  
  <p>B2) Shared Class-Library: the remote facade interface:</p>
  
  <pre><code>@Remote
  public interface UserFacadeRemote {
      void create(User user);
      void edit(User user);
      void remove(User user);
      User find(Object id);
  }
  </code></pre>
  
  <p>C) Client Java Application:</p>
  
  <pre><code>public class LawSuiteSE {
  
      private static UserFacadeRemote ctrlUser;
  
      public LawSuiteSE() {
          try {
              Properties props = new Properties();
              props.put("org.omg.CORBA.ORBInitialHost", "192.168.1.6");
              props.put("org.omg.CORBA.ORBInitialPort", "3700");
              InitialContext ctx = new InitialContext(props);
              ctrlBenutzer = (BenutzerFacadeRemote)ctx.lookup("java:global/LawSuiteEE/LawSuiteEE-ejb/UserFacade!control.UserFacadeRemote");
              User user1 = new User();
              user1.setUsername("testusr");
              user1.setPassword("testpwd");
              ctrlUser.create(user1);
              User user2 = ctrlUser.find(1L);
              System.out.println("username: "+user2.getUsername());
              System.out.println("password: "+user2.getPassword());
          } catch (NamingException ex) {
              System.err.println(ex.getMessage());
          }
      }
  
      public static void main(String[] args) {
          LawSuiteSE lsse = new LawSuiteSE();
      }
  
  }
  </code></pre>