4 Replies Latest reply on Aug 12, 2009 10:49 AM by Adam Warski

    Audited Null Embeddable objects not returned as null

    Aslak Knutsen Master

      There seems to be a difference between how Hibernate and Envers handles Embeddable objects.

      Hibernate will return a null Embedded object when all of the properties are null, Envers on the other hand return
      a non null Embedded object with all null properties.

      ParentEntity.child<EmbeddedChild> -> ComponentPropertyMapper.mapToEntityFromMap
      

      Creates a new Instance of the embeddable object and Sets it on the parent, then moves on to the next
      mapper.

      ParentEntity.Embeddable -> MultiPropertyMapper.mapToEntityFromMap
      

      Moves on to the next mapper.

      ParentEntity.child.name -> SinglePropertyMapper.mapToEntityFromMap
      

      Mappes a null value to the Embeddable objects property.



      In this case the ComponentPropertyMapper needs to know if the sub mappers actually have something to map, and
      based on that decide to set the object or not.


      package org.hibernate.envers.test.bug.Hxxxx;
      
      import javax.persistence.EntityManager;
      
      import org.hibernate.ejb.Ejb3Configuration;
      import org.hibernate.envers.test.AbstractEntityTest;
      import org.testng.annotations.BeforeClass;
      import org.testng.annotations.Test;
      
      public class EmbeddedNull extends AbstractEntityTest {
      
       @Override
       public void configure(Ejb3Configuration cfg) {
       cfg.addAnnotatedClass(ParentEntity.class);
       cfg.addAnnotatedClass(ParentEntity.EmbeddedChild.class);
       }
      
       @BeforeClass(dependsOnMethods = "init")
       public void initData() {
       EntityManager em = getEntityManager();
      
       em.getTransaction().begin();
      
       ParentEntity parent = new ParentEntity("1");
       em.persist(parent);
       em.getTransaction().commit();
       }
      
       @Test
       public void shouldReturnNullEmbeddedObject() throws Exception {
      
       ParentEntity hibernateEntity = getEntityManager().find(ParentEntity.class, "1");
       assert hibernateEntity != null;
       assert hibernateEntity.getChild() == null;
      
       ParentEntity enversEntity = getAuditReader().find(ParentEntity.class, "1", 1);
       assert enversEntity != null;
       assert enversEntity.getChild() == null : "Child should be null, all of its values are..";
       }
      }
      
      package org.hibernate.envers.test.bug.Hxxxx;
      
      import java.io.Serializable;
      
      import javax.persistence.Embeddable;
      import javax.persistence.Entity;
      import javax.persistence.Id;
      
      import org.hibernate.envers.Audited;
      
      @Entity
      @Audited
      public class ParentEntity implements Serializable {
      
       private static final long serialVersionUID = 1L;
      
       @Id
       private String id;
      
       private EmbeddedChild child;
      
       protected ParentEntity() {
       }
      
       public ParentEntity(String id) {
       this.id = id;
       }
      
       public String getId() {
       return id;
       }
      
       public void setChild(EmbeddedChild child) {
       this.child = child;
       }
      
       public EmbeddedChild getChild() {
       return child;
       }
      
       @Embeddable
       public static class EmbeddedChild implements Serializable {
      
       private static final long serialVersionUID = 1L;
      
       private String name;
      
       protected EmbeddedChild() {
       }
      
       public EmbeddedChild(String name) {
       this.name = name;
       }
      
       public String getName() {
       return name;
       }
       }
      }