4 Replies Latest reply on May 6, 2010 11:35 AM by amit Goyal

    AuditReader throwing exception in case of eager loading for many-to-one relationships

    amit Goyal Newbie

      Lets say we have many-to-one relationship between address and employee such the lazy=false for employee. When the address is retrieved using the following query

       

      List results = reader.createQuery().forRevisionsOfEntity(Address.class,true,true).add(AuditEntity.revisionNumber().in(new Number[]{421})).getResultList();

       

      following exception is shown :-

       

      Exception in thread "main" java.lang.NullPointerException

            at org.hibernate.tuple.entity.AbstractEntityTuplizer.createProxy(AbstractEntityTuplizer.java:402)

            at org.hibernate.persister.entity.AbstractEntityPersister.createProxy(AbstractEntityPersister.java:3483)

            at org.hibernate.envers.entities.mapper.relation.ToOneIdMapper.mapToEntityFromMap(ToOneIdMapper.java:91)

            at org.hibernate.envers.entities.mapper.MultiPropertyMapper.mapToEntityFromMap(MultiPropertyMapper.java:118)

            at org.hibernate.envers.entities.EntityInstantiator.createInstanceFromVersionsEntity(EntityInstantiator.java:93)

            at org.hibernate.envers.query.impl.RevisionsOfEntityQuery.list(RevisionsOfEntityQuery.java:126)

            at org.hibernate.envers.query.impl.AbstractAuditQuery.getResultList(AbstractAuditQuery.java:100)

            at com.thirdpillar.beacon.core.service.ProgramCRUDTest.main(ProgramCRUDTest.java:612)

       

      Attachment contains the test scenario(hbms and the java files) and we are using 3.3.1.ga version of hibernate.

      As a workaround we have currently added the following code inside API mapToEntityFromMap() of ToOneIdMapper.java

      try{
                          value = versionsReader.getSessionImplementor().getFactory().getEntityPersister(referencedEntityName).
                          createProxy((Serializable)entityId, new ToOneDelegateSessionImplementor(versionsReader, entityClass, entityId, revision, verCfg));
                      }
                      catch(Exception e){
                          value = new ToOneDelegateSessionImplementor(versionsReader, entityClass, entityId, revision, verCfg).doImmediateLoad(referencedEntityName);
                      }

      Looking forward for a better solution.