HHH-10734 - Fix envers creating unnecessary audit rows when array field types used.
This commit is contained in:
parent
d6eeb509e1
commit
82fe5926bb
|
@ -17,11 +17,13 @@ import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import org.hibernate.envers.tools.Pair;
|
import org.hibernate.envers.tools.Pair;
|
||||||
|
import org.hibernate.internal.util.compare.EqualsHelper;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Adam Warski (adam at warski dot org)
|
* @author Adam Warski (adam at warski dot org)
|
||||||
* @author HernпїЅn Chanfreau
|
* @author HernпїЅn Chanfreau
|
||||||
* @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
|
* @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
|
||||||
|
* @author Chris Cranford
|
||||||
*/
|
*/
|
||||||
public abstract class Tools {
|
public abstract class Tools {
|
||||||
public static <K, V> Map<K, V> newHashMap() {
|
public static <K, V> Map<K, V> newHashMap() {
|
||||||
|
@ -36,12 +38,14 @@ public abstract class Tools {
|
||||||
return new LinkedHashMap<K, V>();
|
return new LinkedHashMap<K, V>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated (since 5.2), use {@link EqualsHelper#areEqual(Object, Object)}.
|
||||||
|
*/
|
||||||
|
@Deprecated
|
||||||
public static boolean objectsEqual(Object obj1, Object obj2) {
|
public static boolean objectsEqual(Object obj1, Object obj2) {
|
||||||
if ( obj1 == null ) {
|
// HHH-10734
|
||||||
return obj2 == null;
|
// Delegates to core's EqualsHelper to support array and non-array types
|
||||||
}
|
return EqualsHelper.areEqual( obj1, obj2 );
|
||||||
|
|
||||||
return obj1.equals( obj2 );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T> List<T> iteratorToList(Iterator<T> iter) {
|
public static <T> List<T> iteratorToList(Iterator<T> iter) {
|
||||||
|
|
|
@ -7,12 +7,14 @@
|
||||||
package org.hibernate.envers.test.integration.data;
|
package org.hibernate.envers.test.integration.data;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
||||||
import javax.persistence.GeneratedValue;
|
import javax.persistence.GeneratedValue;
|
||||||
import javax.persistence.Id;
|
import javax.persistence.Id;
|
||||||
import javax.persistence.Lob;
|
import javax.persistence.Lob;
|
||||||
|
|
||||||
import org.hibernate.envers.Audited;
|
import org.hibernate.envers.Audited;
|
||||||
|
import org.hibernate.envers.NotAudited;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Adam Warski (adam at warski dot org)
|
* @author Adam Warski (adam at warski dot org)
|
||||||
|
@ -35,6 +37,9 @@ public class LobTestEntity {
|
||||||
@Audited
|
@Audited
|
||||||
private char[] charLob;
|
private char[] charLob;
|
||||||
|
|
||||||
|
@NotAudited
|
||||||
|
private String data;
|
||||||
|
|
||||||
public LobTestEntity() {
|
public LobTestEntity() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -83,6 +88,14 @@ public class LobTestEntity {
|
||||||
this.charLob = charLob;
|
this.charLob = charLob;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getData() {
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setData(String data) {
|
||||||
|
this.data = data;
|
||||||
|
}
|
||||||
|
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) {
|
||||||
if ( this == o ) {
|
if ( this == o ) {
|
||||||
return true;
|
return true;
|
||||||
|
@ -105,6 +118,9 @@ public class LobTestEntity {
|
||||||
if ( stringLob != null ? !stringLob.equals( that.stringLob ) : that.stringLob != null ) {
|
if ( stringLob != null ? !stringLob.equals( that.stringLob ) : that.stringLob != null ) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
if ( data != null ? !data.equals( that.data ) : that.data != null ) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -115,6 +131,7 @@ public class LobTestEntity {
|
||||||
result = 31 * result + (stringLob != null ? stringLob.hashCode() : 0);
|
result = 31 * result + (stringLob != null ? stringLob.hashCode() : 0);
|
||||||
result = 31 * result + (byteLob != null ? Arrays.hashCode( byteLob ) : 0);
|
result = 31 * result + (byteLob != null ? Arrays.hashCode( byteLob ) : 0);
|
||||||
result = 31 * result + (charLob != null ? Arrays.hashCode( charLob ) : 0);
|
result = 31 * result + (charLob != null ? Arrays.hashCode( charLob ) : 0);
|
||||||
|
result = 31 * result + (data != null ? data.hashCode() : 0);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -8,22 +8,27 @@ package org.hibernate.envers.test.integration.data;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import javax.persistence.EntityManager;
|
import javax.persistence.EntityManager;
|
||||||
|
|
||||||
import org.hibernate.dialect.PostgreSQL82Dialect;
|
import org.hibernate.dialect.PostgreSQL82Dialect;
|
||||||
import org.hibernate.envers.test.BaseEnversJPAFunctionalTestCase;
|
import org.hibernate.envers.test.BaseEnversJPAFunctionalTestCase;
|
||||||
import org.hibernate.envers.test.Priority;
|
import org.hibernate.envers.test.Priority;
|
||||||
|
|
||||||
import org.hibernate.testing.DialectChecks;
|
import org.hibernate.testing.DialectChecks;
|
||||||
import org.hibernate.testing.RequiresDialectFeature;
|
import org.hibernate.testing.RequiresDialectFeature;
|
||||||
|
import org.hibernate.testing.TestForIssue;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Adam Warski (adam at warski dot org)
|
* @author Adam Warski (adam at warski dot org)
|
||||||
|
* @author Chris Cranford
|
||||||
*/
|
*/
|
||||||
@RequiresDialectFeature(DialectChecks.SupportsExpectedLobUsagePattern.class)
|
@RequiresDialectFeature(DialectChecks.SupportsExpectedLobUsagePattern.class)
|
||||||
public class Lobs extends BaseEnversJPAFunctionalTestCase {
|
public class Lobs extends BaseEnversJPAFunctionalTestCase {
|
||||||
private Integer id1;
|
private Integer id1;
|
||||||
|
private Integer id2;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Class<?>[] getAnnotatedClasses() {
|
protected Class<?>[] getAnnotatedClasses() {
|
||||||
|
@ -55,19 +60,45 @@ public class Lobs extends BaseEnversJPAFunctionalTestCase {
|
||||||
lte.setByteLob( new byte[]{ 3, 4, 5 } );
|
lte.setByteLob( new byte[]{ 3, 4, 5 } );
|
||||||
lte.setCharLob( new char[]{ 'h', 'i', 'j' } );
|
lte.setCharLob( new char[]{ 'h', 'i', 'j' } );
|
||||||
em.getTransaction().commit();
|
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
|
@Test
|
||||||
public void testRevisionsCounts() {
|
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
|
@Test
|
||||||
public void testHistoryOfId1() {
|
public void testHistoryOfId1() {
|
||||||
LobTestEntity ver1 = new LobTestEntity( id1, "abc", new byte[]{ 0, 1, 2 }, new char[]{ 'x', 'y', 'z' } );
|
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' } );
|
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 );
|
||||||
|
}
|
||||||
|
|
||||||
assert getAuditReader().find( LobTestEntity.class, id1, 1 ).equals( ver1 );
|
@Test
|
||||||
assert getAuditReader().find( LobTestEntity.class, id1, 2 ).equals( ver2 );
|
@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 );
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue