1 Reply Latest reply on Jan 25, 2006 5:58 PM by John Blackmore

    Value objects

    Martin Ganserer Novice

      Hello,

      I would like to support different databases in my app.
      Today I created an example app where I defined following simple classes:

      A public interface

      public interface Customer
      {
       public int getId();
       public void setId(int id);
       public String getName();
       public void setName(String name);
      }


      A value object that the client can use:

      public class CustomerVO implements Customer,Serializable
      {
       private int id;
       private String name;
       public int getId()
       {
       return this.id;
       }
       public void setId(int id)
       {
       this.id = id;
       }
      ...
      }


      and an EJB3 entity bean:

      @Entity
      @Table(name="test_cust2")
      public class CustomerMySQL implements Customer,Serializable
      {
       private int id;
       private String name;
      
       @Column(name="id")
       @Id(generate=GeneratorType.AUTO)
       public int getId()
       {
       return this.id;
       }
      
       public void setId(int id)
       {
       this.id = id;
       }
      }

      For testing purpose I created a small DAO stateless session bean
      @Stateless
      public class CustomerDAOBean implements CustomerDAO
      {
       private @PersistenceContext(unitName="test") EntityManager em;
      
       public Customer persistCustomer(Customer cust)
       {
       em.persist(cust);
       return cust;
       }
      }
      


      One might argue that I do not need a data transfer object. But in fact I need it to switch dynamically from one database to another.
      Especially when I want to work with a mysql and an oracle database.
      In both entity bean classes I must use different id generator types!
      My problem is that I can compile and deploy the app, but when it comes to persisting the entity bean I get an error that tells me that Customer is not an entity bean.

      What do you think? Should I forget about it, or is there another approach for that problem?

      Thank you!