0 Replies Latest reply on Aug 15, 2007 12:10 AM by Danny Chen

    @In EntityManager is null when create an object from named b

    Danny Chen Newbie

      If there is a bean named database, which @In entity manager:

      @Name("database")
      @AutoCreate
      public class Database {
      
       @In
       private EntityManager entityManager;
      
       public Table createTable() {
       return new Table(this);
       }
      
       public User getUser(Long id) {
       return this.entityManager.find(User.class, id);
       }
      }


      And a table (like DAO, just work with User table):
      public class Table {
      
       private Database database;
      
       public Table(Database database) {
       this.database = database;
       }
      
       public User getUser() {
       return this.database.getUser(0L);
       }
      }


      If create table directly in bean, it will be ok, but if create table from Database, the entityManager in Database will be null. The following is test code:

      public class Test() {
      
       @In
       private Database database;
      
       public void execute() {
       // The following code execute successfully
       Table t1 = new Table(this.database);
       User user1 = t1.getUser();
       System.out.println(user1.toString());
      
       // The following code will cause error
       // The entitymanager of database is null
       Table t2 = this.database.createTable();
       User user2 = t2.getUser();
       }
      }


      Is there limited that we can not create a child bean from parent bean if child bean reference to parent bean?