HHH-7246 - Fix and test

This commit is contained in:
Lukasz Antoniak 2012-04-24 22:41:49 +02:00
parent 7fa7a51016
commit f7708a9c6c
2 changed files with 75 additions and 2 deletions

View File

@ -28,11 +28,13 @@ import java.util.Map;
import org.hibernate.HibernateException; import org.hibernate.HibernateException;
import org.hibernate.collection.spi.PersistentCollection; import org.hibernate.collection.spi.PersistentCollection;
import org.hibernate.dialect.Oracle8iDialect;
import org.hibernate.engine.spi.SessionImplementor; import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.envers.configuration.AuditConfiguration; import org.hibernate.envers.configuration.AuditConfiguration;
import org.hibernate.envers.entities.PropertyData; import org.hibernate.envers.entities.PropertyData;
import org.hibernate.envers.exception.AuditException; import org.hibernate.envers.exception.AuditException;
import org.hibernate.envers.reader.AuditReaderImplementor; import org.hibernate.envers.reader.AuditReaderImplementor;
import org.hibernate.envers.tools.StringTools;
import org.hibernate.envers.tools.Tools; import org.hibernate.envers.tools.Tools;
import org.hibernate.envers.tools.reflection.ReflectionTools; import org.hibernate.envers.tools.reflection.ReflectionTools;
import org.hibernate.property.DirectPropertyAccessor; import org.hibernate.property.DirectPropertyAccessor;
@ -62,8 +64,12 @@ public class SinglePropertyMapper implements PropertyMapper, SimpleMapperBuilder
public boolean mapToMapFromEntity(SessionImplementor session, Map<String, Object> data, Object newObj, Object oldObj) { public boolean mapToMapFromEntity(SessionImplementor session, Map<String, Object> data, Object newObj, Object oldObj) {
data.put(propertyData.getName(), newObj); data.put(propertyData.getName(), newObj);
boolean dbLogicallyDifferent = true;
return !Tools.objectsEqual(newObj, oldObj); if ((session.getFactory().getDialect() instanceof Oracle8iDialect) && (newObj instanceof String || oldObj instanceof String)) {
// Don't generate new revision when database replaces empty string with NULL during INSERT or UPDATE statements.
dbLogicallyDifferent = !(StringTools.isEmpty((String) newObj) && StringTools.isEmpty((String) oldObj));
}
return dbLogicallyDifferent && !Tools.objectsEqual(newObj, oldObj);
} }
@Override @Override

View File

@ -0,0 +1,67 @@
package org.hibernate.envers.test.integration.basic;
import java.util.Arrays;
import javax.persistence.EntityManager;
import junit.framework.Assert;
import org.junit.Test;
import org.hibernate.dialect.Oracle8iDialect;
import org.hibernate.envers.test.BaseEnversJPAFunctionalTestCase;
import org.hibernate.envers.test.Priority;
import org.hibernate.envers.test.entities.StrTestEntity;
import org.hibernate.testing.RequiresDialect;
import org.hibernate.testing.TestForIssue;
/**
* @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
*/
@TestForIssue(jiraKey = "HHH-7246")
@RequiresDialect(Oracle8iDialect.class)
public class EmptyStringTest extends BaseEnversJPAFunctionalTestCase {
private Integer emptyId = null;
private Integer nullId = null;
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[]{StrTestEntity.class};
}
@Test
@Priority(10)
public void initData() {
EntityManager em = getEntityManager();
// Revision 1
em.getTransaction().begin();
StrTestEntity emptyEntity = new StrTestEntity("");
em.persist(emptyEntity);
StrTestEntity nullEntity = new StrTestEntity(null);
em.persist(nullEntity);
em.getTransaction().commit();
emptyId = emptyEntity.getId();
nullId = nullEntity.getId();
em.close();
em = getEntityManager();
// Should not generate revision after NULL to "" modification and vice versa on Oracle.
em.getTransaction().begin();
emptyEntity = em.find(StrTestEntity.class, emptyId);
emptyEntity.setStr(null);
em.merge(emptyEntity);
nullEntity = em.find(StrTestEntity.class, nullId);
nullEntity.setStr("");
em.merge(nullEntity);
em.getTransaction().commit();
em.close();
}
@Test
public void testRevisionsCounts() {
Assert.assertEquals(Arrays.asList(1), getAuditReader().getRevisions(StrTestEntity.class, emptyId));
Assert.assertEquals(Arrays.asList(1), getAuditReader().getRevisions(StrTestEntity.class, nullId));
}
}