HHH-10734 - Fix envers creating unnecessary audit rows when array field types used.

This commit is contained in:
Chris Cranford 2016-05-12 15:55:57 -05:00
parent dc44ef9378
commit 4ebad67bb1
3 changed files with 69 additions and 17 deletions

View File

@ -17,11 +17,13 @@ import java.util.Map;
import java.util.Set;
import org.hibernate.envers.tools.Pair;
import org.hibernate.internal.util.compare.EqualsHelper;
/**
* @author Adam Warski (adam at warski dot org)
* @author HernпїЅn Chanfreau
* @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
* @author Chris Cranford
*/
public abstract class Tools {
public static <K, V> Map<K, V> newHashMap() {
@ -36,12 +38,14 @@ public abstract class Tools {
return new LinkedHashMap<K, V>();
}
/**
* @deprecated (since 5.2), use {@link EqualsHelper#areEqual(Object, Object)}.
*/
@Deprecated
public static boolean objectsEqual(Object obj1, Object obj2) {
if ( obj1 == null ) {
return obj2 == null;
}
return obj1.equals( obj2 );
// HHH-10734
// Delegates to core's EqualsHelper to support array and non-array types
return EqualsHelper.areEqual( obj1, obj2 );
}
public static <T> List<T> iteratorToList(Iterator<T> iter) {

View File

@ -7,12 +7,14 @@
package org.hibernate.envers.test.integration.data;
import java.util.Arrays;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Lob;
import org.hibernate.envers.Audited;
import org.hibernate.envers.NotAudited;
/**
* @author Adam Warski (adam at warski dot org)
@ -35,6 +37,9 @@ public class LobTestEntity {
@Audited
private char[] charLob;
@NotAudited
private String data;
public LobTestEntity() {
}
@ -83,6 +88,14 @@ public class LobTestEntity {
this.charLob = charLob;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
public boolean equals(Object o) {
if ( this == o ) {
return true;
@ -105,6 +118,9 @@ public class LobTestEntity {
if ( stringLob != null ? !stringLob.equals( that.stringLob ) : that.stringLob != null ) {
return false;
}
if ( data != null ? !data.equals( that.data ) : that.data != null ) {
return false;
}
return true;
}
@ -115,6 +131,7 @@ public class LobTestEntity {
result = 31 * result + (stringLob != null ? stringLob.hashCode() : 0);
result = 31 * result + (byteLob != null ? Arrays.hashCode( byteLob ) : 0);
result = 31 * result + (charLob != null ? Arrays.hashCode( charLob ) : 0);
result = 31 * result + (data != null ? data.hashCode() : 0);
return result;
}
}

View File

@ -8,26 +8,31 @@ package org.hibernate.envers.test.integration.data;
import java.util.Arrays;
import java.util.Map;
import javax.persistence.EntityManager;
import org.hibernate.dialect.PostgreSQL82Dialect;
import org.hibernate.envers.test.BaseEnversJPAFunctionalTestCase;
import org.hibernate.envers.test.Priority;
import org.hibernate.testing.DialectChecks;
import org.hibernate.testing.RequiresDialectFeature;
import org.hibernate.testing.TestForIssue;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
/**
* @author Adam Warski (adam at warski dot org)
* @author Chris Cranford
*/
@RequiresDialectFeature(DialectChecks.SupportsExpectedLobUsagePattern.class)
public class Lobs extends BaseEnversJPAFunctionalTestCase {
private Integer id1;
private Integer id2;
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class[] {LobTestEntity.class};
return new Class[]{ LobTestEntity.class };
}
@Override
@ -44,7 +49,7 @@ public class Lobs extends BaseEnversJPAFunctionalTestCase {
public void initData() {
EntityManager em = getEntityManager();
em.getTransaction().begin();
LobTestEntity lte = new LobTestEntity( "abc", new byte[] {0, 1, 2}, new char[] {'x', 'y', 'z'} );
LobTestEntity lte = new LobTestEntity( "abc", new byte[]{ 0, 1, 2 }, new char[]{ 'x', 'y', 'z' } );
em.persist( lte );
id1 = lte.getId();
em.getTransaction().commit();
@ -52,22 +57,48 @@ public class Lobs extends BaseEnversJPAFunctionalTestCase {
em.getTransaction().begin();
lte = em.find( LobTestEntity.class, id1 );
lte.setStringLob( "def" );
lte.setByteLob( new byte[] {3, 4, 5} );
lte.setCharLob( new char[] {'h', 'i', 'j'} );
lte.setByteLob( new byte[]{ 3, 4, 5 } );
lte.setCharLob( new char[]{ 'h', 'i', 'j' } );
em.getTransaction().commit();
// this creates a revision history for a Lob-capable entity but the change is on a non-audited
// field and so it should only generate 1 revision, the initial persist.
em.getTransaction().begin();
LobTestEntity lte2 = new LobTestEntity( "abc", new byte[]{ 0, 1, 2 }, new char[]{ 'x', 'y', 'z' } );
lte2.setData( "Hi" );
em.persist( lte2 );
em.getTransaction().commit();
id2 = lte2.getId();
em.getTransaction().begin();
lte2 = em.find( LobTestEntity.class, id2 );
lte2.setData( "Hello World" );
em.getTransaction().commit();
}
@Test
public void testRevisionsCounts() {
assert Arrays.asList( 1, 2 ).equals( getAuditReader().getRevisions( LobTestEntity.class, id1 ) );
assertEquals( Arrays.asList( 1, 2 ), getAuditReader().getRevisions( LobTestEntity.class, id1 ) );
}
@Test
public void testHistoryOfId1() {
LobTestEntity ver1 = new LobTestEntity( id1, "abc", new byte[] {0, 1, 2}, new char[] {'x', 'y', 'z'} );
LobTestEntity ver2 = new LobTestEntity( id1, "def", new byte[] {3, 4, 5}, new char[] {'h', 'i', 'j'} );
assert getAuditReader().find( LobTestEntity.class, id1, 1 ).equals( ver1 );
assert getAuditReader().find( LobTestEntity.class, id1, 2 ).equals( ver2 );
LobTestEntity ver1 = new LobTestEntity( id1, "abc", new byte[]{ 0, 1, 2 }, new char[]{ 'x', 'y', 'z' } );
LobTestEntity ver2 = new LobTestEntity( id1, "def", new byte[]{ 3, 4, 5 }, new char[]{ 'h', 'i', 'j' } );
assertEquals( getAuditReader().find( LobTestEntity.class, id1, 1 ), ver1 );
assertEquals( getAuditReader().find( LobTestEntity.class, id1, 2 ), ver2 );
}
}
@Test
@TestForIssue(jiraKey = "HHH-10734")
public void testRevisionsCountsForAuditedArraysWithNoChanges() {
assertEquals( Arrays.asList( 3 ), getAuditReader().getRevisions( LobTestEntity.class, id2 ) );
}
@Test
@TestForIssue(jiraKey = "HHH-10734")
public void testHistoryOfId2() {
LobTestEntity ver1 = new LobTestEntity( id2, "abc", new byte[]{ 0, 1, 2 }, new char[]{ 'x', 'y', 'z' } );
assertEquals( getAuditReader().find( LobTestEntity.class, id2, 3 ), ver1 );
}
}