0 Replies Latest reply on Jan 11, 2005 4:15 PM by Troy Schulz

    inconsistent state of entity bean using auto-increment

    Troy Schulz Newbie

      JBoss Version : 3.2.4,3.2.5,3.2.6

      We are using auto-increment fields with SQL Server and have enabled insert-after-ejb-post-create in order to support NOT NULL FK constraints.

      From a session bean, a call such as:

      User u = userHome.create("first","last", "email@here.there");

      returns an object that is inconsistent. A call to u.getId() will return the correct auto-increment value. However, a call to u.getPrimaryKey() returns a primary key object that has the value of 0.

      It appears to me that the code that retrieves the auto-increment value from the sql insert statement and sets the id property value does not in turn fix the Primary Key object for the bean.

      For most things this is not a problem, but in addition to creating the user, we want to create an address for them at the same time. ie.

      User u = userHome.create("first","last", "email@here.there");
      Address a = addressHome.create("street", "city", "state", u);

      This address create fails since JBoss attempts to use the id of 0 as the FK value.

      The work around we have discovered is to reload the user prior to adding the address:

      User u = userHome.create("first","last", "email@here.there");
      //u.getId() and u.getPrimaryKey() are NOT consistent
      u = userHome.findByPrimaryKey(new UserPK(u.getId));
      //u.getId() and u.getPrimaryKey() are consistent
      Address a = addressHome.create("street", "city", "state", u);

      This new user object is no longer inconsistent and the create of the address works as expected.

      Is the PrimaryKey object not being updated at the same time as the id property for a reason, or is this a bug? It seems to me that the two should be synchronized at the time the id is updated.