2 Replies Latest reply on Sep 26, 2007 9:16 AM by Terry

    Inserting multiple records using Home Entity

    Terry Apprentice

      Is it possible to insert more than one record in to database using the EntityHome object? I get following error, and not sure how to go around it.

      javax.persistence.PersistenceException: org.hibernate.HibernateException: identifier of an instance of au.edu.tisc.entity.OrgUserActivityLog was altered from 10001 to 10002
      at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:629)
      at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:299)

      In my example, I am trying to insert user activity records in to the table during the authentication process called from Authenticator.authenticate. If I insert only one record it works ok; but if I try to insert another I get error on .persist() call.

      @Name("authenticator")
      public class Authenticator {

      public boolean authenticate() {

      @In(value = "activityLogger", required = false, create = true)
      private ActivityLogger activityLog;

      ...
      ... validate user details ...
      ...
      activityLog.log(...); <--insert first record - works ok
      ...
      ...
      activityLog.log(...); <--try to insert another - causes error
      ...
      }
      }


      @Name("activityLogger")
      public class ActivityLogger {

      public void log(OrgUser orgUser, String action) {

      OrgUserActivityLogHome orgUserActivityLogHome = (OrgUserActivityLogHome) Component.getInstance(OrgUserActivityLogHome.class, true);

      OrgUserActivityLog orgUserActivityLog = orgUserActivityLogHome.getInstance();
      orgUserActivityLog.setId(KeyGenerator.getKey());
      orgUserActivityLog.setAction(action);
      orgUserActivityLog.setOrganisationUser(orgUser);
      orgUserActivityLogHome.persist(); <-- Above error thrown here on 2nd insert.
      }
      }

      I am not sure why, error occurs on 2nd call; since first record insert had already called .persist() on the Entity Home object.