1 Reply Latest reply on Mar 20, 2009 4:08 AM by adamw

    AuditReaderFactory/EntityManager problem: Hibernate EntityMa

      Hi there,

      first of all, Envers really is a GREAT project, like it very much, keep it up!

      Ok, I have a problem with the AuditReaderFactory. I`m trying to retrieve an AuditReader in my Stateless SessionBean. But it doesn`t seem to work for me. I inject the entitymanager with the PersistenceContext annotation:

      @PersistenceContext
      private EntityManager entityManager;


      When I inspect my code and the injected entityManager with my debugger I notice the following:
      if (entityManager.getDelegate() instanceof EntityManager) {
       // true: delegate == org.hibernate.ejb.EntityManagerImpl
      }


      But, when I notice the code in the AuditFactoryReader it first checks for

      if (entityManager.getDelegate() instanceof EntityManager)


      Then it checks for:

      if (entityManager.getDelegate() instanceof Session)


      But this never seems to happen! Shouldn`t the code in the AuditFactoryReader be something like this?

      public static AuditReader get(EntityManager entityManager) throws AuditException {
      
       Object delegate = entityManager.getDelegate();
      
       if (delegate instanceof Session) {
       return get((Session) delegate);
       }
       else if (delegate instanceof EntityManager) {
      
       Object session = ((EntityManager)delegate).getDelegate();
      
       if ( session instanceof Session) {
       return get((Session)session);
       }
       }
       else {
       throw new AuditException("Hibernate EntityManager not present!");
       }
      }


      I`ve also tested my code with injection of the PersistenceUnit. In this case everything works just fine.

      @PersistenceUnit
      private EntityManagerFactory entityManagerFactory;
      
      ...
      
      EntityManager entityManager = entityManagerFactory.createEntityManager();
      AuditReader reader = AuditReaderFactory.get(entityManager);


      I`m running my code with:

      Oracle oc4j 10.1.3.3
      [INFO] org.hibernate.cfg.annotations.Version - [Hibernate Annotations 3.4.0.GA]
      [INFO] org.hibernate.cfg.Environment - [Hibernate 3.3.1.GA]
      [INFO] org.hibernate.cfg.Environment - [hibernate.properties not found]
      [INFO] org.hibernate.cfg.Environment - [Bytecode provider name : javassist]
      [INFO] org.hibernate.cfg.Environment - [using JDK 1.4 java.sql.Timestamp handling]
      [INFO] org.hibernate.annotations.common.Version - [Hibernate Commons Annotations 3.1.0.GA]
      [INFO] org.hibernate.ejb.Version - [Hibernate EntityManager 3.4.0.GA]