Author: adamw
Date: 2009-05-31 09:39:00 -0400 (Sun, 31 May 2009)
New Revision: 16641
Added:
core/trunk/envers/src/test/java/org/hibernate/envers/test/integration/inheritance/joined/ChildNullAuditing.java
Modified:
core/trunk/envers/src/main/java/org/hibernate/envers/entities/mapper/SinglePropertyMapper.java
Log:
Fixinga bug with storing null values for fields in a superclass
Modified:
core/trunk/envers/src/main/java/org/hibernate/envers/entities/mapper/SinglePropertyMapper.java
===================================================================
---
core/trunk/envers/src/main/java/org/hibernate/envers/entities/mapper/SinglePropertyMapper.java 2009-05-31
13:00:56 UTC (rev 16640)
+++
core/trunk/envers/src/main/java/org/hibernate/envers/entities/mapper/SinglePropertyMapper.java 2009-05-31
13:39:00 UTC (rev 16641)
@@ -78,13 +78,17 @@
}
private boolean isPrimitive(Setter setter, PropertyData propertyData, Class<?>
cls) {
+ if (cls == null) {
+ throw new HibernateException("No field found for property: " +
propertyData.getName());
+ }
+
if (setter instanceof DirectPropertyAccessor.DirectSetter) {
// In a direct setter, getMethod() returns null
// Trying to look up the field
try {
return cls.getDeclaredField(propertyData.getBeanName()).getType().isPrimitive();
} catch (NoSuchFieldException e) {
- throw new HibernateException(e);
+ return isPrimitive(setter, propertyData, cls.getSuperclass());
}
} else {
return setter.getMethod().getParameterTypes()[0].isPrimitive();
Copied:
core/trunk/envers/src/test/java/org/hibernate/envers/test/integration/inheritance/joined/ChildNullAuditing.java
(from rev 16611,
core/trunk/envers/src/test/java/org/hibernate/envers/test/integration/inheritance/joined/ChildAuditing.java)
===================================================================
---
core/trunk/envers/src/test/java/org/hibernate/envers/test/integration/inheritance/joined/ChildNullAuditing.java
(rev 0)
+++
core/trunk/envers/src/test/java/org/hibernate/envers/test/integration/inheritance/joined/ChildNullAuditing.java 2009-05-31
13:39:00 UTC (rev 16641)
@@ -0,0 +1,94 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors. All third-party contributions are
+ * distributed under license by Red Hat Middleware LLC.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA 02110-1301 USA
+ */
+
+package org.hibernate.envers.test.integration.inheritance.joined;
+
+import java.util.Arrays;
+import javax.persistence.EntityManager;
+
+import org.hibernate.envers.test.AbstractEntityTest;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+import org.hibernate.ejb.Ejb3Configuration;
+
+/**
+ * @author Adam Warski (adam at warski dot org)
+ */
+public class ChildNullAuditing extends AbstractEntityTest {
+ private Integer id1;
+
+ public void configure(Ejb3Configuration cfg) {
+ cfg.addAnnotatedClass(ChildEntity.class);
+ cfg.addAnnotatedClass(ParentEntity.class);
+ }
+
+ @BeforeClass(dependsOnMethods = "init")
+ public void initData() {
+ EntityManager em = getEntityManager();
+
+ id1 = 1;
+
+ // Rev 1
+ em.getTransaction().begin();
+ ChildEntity ce = new ChildEntity(id1, "x", null);
+ em.persist(ce);
+ em.getTransaction().commit();
+
+ // Rev 2
+ em.getTransaction().begin();
+ ce = em.find(ChildEntity.class, id1);
+ ce.setData(null);
+ ce.setNumber(2l);
+ em.getTransaction().commit();
+ }
+
+ @Test
+ public void testRevisionsCounts() {
+ assert Arrays.asList(1,
2).equals(getAuditReader().getRevisions(ChildEntity.class, id1));
+ }
+
+ @Test
+ public void testHistoryOfChildId1() {
+ ChildEntity ver1 = new ChildEntity(id1, "x", null);
+ ChildEntity ver2 = new ChildEntity(id1, null, 2l);
+
+ assert getAuditReader().find(ChildEntity.class, id1, 1).equals(ver1);
+ assert getAuditReader().find(ChildEntity.class, id1, 2).equals(ver2);
+
+ assert getAuditReader().find(ParentEntity.class, id1, 1).equals(ver1);
+ assert getAuditReader().find(ParentEntity.class, id1, 2).equals(ver2);
+ }
+
+ @Test
+ public void testPolymorphicQuery() {
+ ChildEntity childVer1 = new ChildEntity(id1, "x", null);
+
+ assert getAuditReader().createQuery().forEntitiesAtRevision(ChildEntity.class,
1).getSingleResult()
+ .equals(childVer1);
+
+ assert getAuditReader().createQuery().forEntitiesAtRevision(ParentEntity.class,
1).getSingleResult()
+ .equals(childVer1);
+ }
+}
\ No newline at end of file