HHH-13770 Fix modified columns being populated with null values

This commit is contained in:
Chris Cranford 2020-01-13 19:55:14 -05:00 committed by Andrea Boriero
parent b3ea1d67c4
commit 8c52eb2eae
9 changed files with 143 additions and 4 deletions

View File

@ -40,10 +40,11 @@ public class LegacyModifiedColumnNamingStrategy implements ModifiedColumnNamingS
else { else {
columnName = propertyAuditingData.getModifiedFlagName(); columnName = propertyAuditingData.getModifiedFlagName();
} }
MetadataTools.addModifiedFlagProperty( MetadataTools.addModifiedFlagPropertyWithColumn(
parent, parent,
propertyAuditingData.getName(), propertyAuditingData.getName(),
globalCfg.getModifiedFlagSuffix(), globalCfg.getModifiedFlagSuffix(),
propertyAuditingData.getModifiedFlagName(),
columnName columnName
); );
} }

View File

@ -591,7 +591,7 @@ public class AuditedPropertiesReader {
propertyData.setRelationTargetAuditMode( aud.targetAuditMode() ); propertyData.setRelationTargetAuditMode( aud.targetAuditMode() );
propertyData.setUsingModifiedFlag( checkUsingModifiedFlag( aud ) ); propertyData.setUsingModifiedFlag( checkUsingModifiedFlag( aud ) );
propertyData.setModifiedFlagName( MetadataTools.getModifiedFlagPropertyName( propertyName, modifiedFlagSuffix ) ); propertyData.setModifiedFlagName( MetadataTools.getModifiedFlagPropertyName( propertyName, modifiedFlagSuffix ) );
if( aud.modifiedColumnName() != null && !"".equals( aud.modifiedColumnName() ) ) { if ( !StringTools.isEmpty( aud.modifiedColumnName() ) ) {
propertyData.setExplicitModifiedFlagName( aud.modifiedColumnName() ); propertyData.setExplicitModifiedFlagName( aud.modifiedColumnName() );
} }
return true; return true;

View File

@ -14,8 +14,12 @@ import org.hibernate.envers.test.Priority;
import org.hibernate.envers.test.integration.auditReader.AuditedTestEntity; import org.hibernate.envers.test.integration.auditReader.AuditedTestEntity;
import org.hibernate.envers.test.integration.auditReader.NotAuditedTestEntity; import org.hibernate.envers.test.integration.auditReader.NotAuditedTestEntity;
import org.hibernate.envers.test.integration.modifiedflags.entities.EnumEntity;
import org.hibernate.envers.test.integration.modifiedflags.entities.EnumOption;
import org.junit.Test; import org.junit.Test;
import org.hibernate.testing.TestForIssue;
import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertTrue; import static junit.framework.Assert.assertTrue;
@ -28,7 +32,7 @@ import static junit.framework.Assert.assertTrue;
public class HasChangedAPITest extends AbstractModifiedFlagsEntityTest { public class HasChangedAPITest extends AbstractModifiedFlagsEntityTest {
@Override @Override
protected Class<?>[] getAnnotatedClasses() { protected Class<?>[] getAnnotatedClasses() {
return new Class[] {AuditedTestEntity.class, NotAuditedTestEntity.class}; return new Class[] {AuditedTestEntity.class, NotAuditedTestEntity.class, EnumEntity.class};
} }
@Test @Test
@ -38,17 +42,22 @@ public class HasChangedAPITest extends AbstractModifiedFlagsEntityTest {
em.getTransaction().begin(); em.getTransaction().begin();
AuditedTestEntity ent1 = new AuditedTestEntity( 1, "str1" ); AuditedTestEntity ent1 = new AuditedTestEntity( 1, "str1" );
NotAuditedTestEntity ent2 = new NotAuditedTestEntity( 1, "str1" ); NotAuditedTestEntity ent2 = new NotAuditedTestEntity( 1, "str1" );
EnumEntity ent3 = new EnumEntity( 1, EnumOption.A );
em.persist( ent1 ); em.persist( ent1 );
em.persist( ent2 ); em.persist( ent2 );
em.persist( ent3 );
em.getTransaction().commit(); em.getTransaction().commit();
em.getTransaction().begin(); em.getTransaction().begin();
ent1 = em.find( AuditedTestEntity.class, 1 ); ent1 = em.find( AuditedTestEntity.class, 1 );
ent2 = em.find( NotAuditedTestEntity.class, 1 ); ent2 = em.find( NotAuditedTestEntity.class, 1 );
ent3 = em.find( EnumEntity.class, 1 );
ent1.setStr1( "str2" ); ent1.setStr1( "str2" );
ent2.setStr1( "str2" ); ent2.setStr1( "str2" );
ent3.setOption( EnumOption.B );
em.getTransaction().commit(); em.getTransaction().commit();
} }
@ -65,4 +74,18 @@ public class HasChangedAPITest extends AbstractModifiedFlagsEntityTest {
assertTrue( list.isEmpty() ); assertTrue( list.isEmpty() );
} }
@Test
@TestForIssue(jiraKey = "HHH-13770")
public void testHasChangedHasNotChangedEnum() {
List list = getAuditReader().createQuery().forRevisionsOfEntity( EnumEntity.class, true, true )
.add( AuditEntity.property( "option" ).hasChanged() ).getResultList();
assertEquals( 2, list.size() );
assertEquals( EnumOption.A, ( (EnumEntity) list.get( 0 ) ).getOption() );
assertEquals( EnumOption.B, ( (EnumEntity) list.get( 1 ) ).getOption() );
list = getAuditReader().createQuery().forRevisionsOfEntity( EnumEntity.class, true, true )
.add( AuditEntity.property( "option" ).hasNotChanged() ).getResultList();
assertTrue( list.isEmpty() );
}
} }

View File

@ -0,0 +1,55 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.envers.test.integration.modifiedflags.entities;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Id;
import org.hibernate.envers.Audited;
/**
* @author Chris Cranford
*/
@Entity
@Audited
public class EnumEntity {
@Id
private Integer id;
@Enumerated(EnumType.STRING)
@Column(name = "client_option")
@Audited(modifiedColumnName = "client_option_mod")
private EnumOption option;
EnumEntity() {
}
public EnumEntity(Integer id, EnumOption option) {
this.id = id;
this.option = option;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public EnumOption getOption() {
return option;
}
public void setOption(EnumOption option) {
this.option = option;
}
}

View File

@ -0,0 +1,15 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.envers.test.integration.modifiedflags.entities;
/**
* @author Chris Cranford
*/
public enum EnumOption {
A,
B,
}

View File

@ -0,0 +1,15 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.envers.test.integration.modifiedflags.naming;
/**
* @author Chris Cranford
*/
public enum ClientOption {
A,
B
}

View File

@ -44,6 +44,8 @@ public class ImprovedColumnNamingStrategyTest extends BaseEnversJPAFunctionalTes
assertNotNull( table1.getColumn( new Column( "otherEntity_MOD" ) ) ); assertNotNull( table1.getColumn( new Column( "otherEntity_MOD" ) ) );
assertNotNull( table1.getColumn( new Column( "single_id_MOD" ) ) ); assertNotNull( table1.getColumn( new Column( "single_id_MOD" ) ) );
assertNotNull( table1.getColumn( new Column( "singleIdEntity2_id_MOD" ) ) ); assertNotNull( table1.getColumn( new Column( "singleIdEntity2_id_MOD" ) ) );
assertNotNull( table1.getColumn( new Column( "client_option_MOD" ) ) );
assertNotNull( table1.getColumn( new Column( "cop_mod" ) ) );
final Table table2 = metadata().getEntityBinding( OtherEntity.class.getName() + "_AUD" ).getTable(); final Table table2 = metadata().getEntityBinding( OtherEntity.class.getName() + "_AUD" ).getTable();
assertNotNull( table2.getColumn( new Column( "d_MOD" ) ) ); assertNotNull( table2.getColumn( new Column( "d_MOD" ) ) );

View File

@ -34,6 +34,8 @@ public class LegacyColumnNamingStrategyTest extends BaseEnversJPAFunctionalTestC
assertNotNull( table1.getColumn( new Column( "otherEntity_MOD" ) ) ); assertNotNull( table1.getColumn( new Column( "otherEntity_MOD" ) ) );
assertNotNull( table1.getColumn( new Column( "singleIdEntity_MOD" ) ) ); assertNotNull( table1.getColumn( new Column( "singleIdEntity_MOD" ) ) );
assertNotNull( table1.getColumn( new Column( "singleIdEntity2_MOD" ) ) ); assertNotNull( table1.getColumn( new Column( "singleIdEntity2_MOD" ) ) );
assertNotNull( table1.getColumn( new Column( "clientOption_MOD" ) ) );
assertNotNull( table1.getColumn( new Column( "cop_mod" ) ) );
final Table table2 = metadata().getEntityBinding( OtherEntity.class.getName() + "_AUD" ).getTable(); final Table table2 = metadata().getEntityBinding( OtherEntity.class.getName() + "_AUD" ).getTable();
assertNotNull( table2.getColumn( new Column( "data_MOD" ) ) ); assertNotNull( table2.getColumn( new Column( "data_MOD" ) ) );

View File

@ -8,8 +8,9 @@ package org.hibernate.envers.test.integration.modifiedflags.naming;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Embedded; import javax.persistence.Embedded;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.JoinColumn; import javax.persistence.JoinColumn;
@ -52,6 +53,15 @@ public class TestEntity {
@OneToOne @OneToOne
private SingleIdEntity singleIdEntity2; private SingleIdEntity singleIdEntity2;
@Column(name = "client_option")
@Enumerated(EnumType.STRING)
private ClientOption clientOption;
@Column(name = "client_option2")
@Enumerated(EnumType.STRING)
@Audited(withModifiedFlag = true, modifiedColumnName = "cop_mod")
private ClientOption clientOption2;
public Integer getId() { public Integer getId() {
return id; return id;
} }
@ -123,4 +133,20 @@ public class TestEntity {
public void setSingleIdEntity2(SingleIdEntity singleIdEntity2) { public void setSingleIdEntity2(SingleIdEntity singleIdEntity2) {
this.singleIdEntity2 = singleIdEntity2; this.singleIdEntity2 = singleIdEntity2;
} }
public ClientOption getClientOption() {
return clientOption;
}
public void setClientOption(ClientOption clientOption) {
this.clientOption = clientOption;
}
public ClientOption getClientOption2() {
return clientOption2;
}
public void setClientOption2(ClientOption clientOption2) {
this.clientOption2 = clientOption2;
}
} }