HHH-10841 - Fix columns with generated annotation not being tracked by Envers.

This commit is contained in:
Chris Cranford 2016-06-13 19:13:32 -05:00
parent 2ea122aedd
commit dde7a5d1a5
3 changed files with 160 additions and 1 deletions

View File

@ -39,6 +39,9 @@ import org.hibernate.mapping.SyntheticProperty;
import org.hibernate.mapping.Table;
import org.hibernate.mapping.Value;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.tuple.GeneratedValueGeneration;
import org.hibernate.tuple.GenerationTiming;
import org.hibernate.tuple.ValueGeneration;
import org.hibernate.type.CollectionType;
import org.hibernate.type.ComponentType;
import org.hibernate.type.ManyToOneType;
@ -383,7 +386,7 @@ public final class AuditMetadataGenerator {
entityName,
xmlMappingData,
propertyAuditingData,
property.isInsertable(),
isPropertyInsertable( property ),
firstPass,
true
);
@ -391,6 +394,20 @@ public final class AuditMetadataGenerator {
}
}
private boolean isPropertyInsertable(Property property) {
if ( !property.isInsertable() ) {
final ValueGeneration generation = property.getValueGenerationStrategy();
if ( generation instanceof GeneratedValueGeneration ) {
final GeneratedValueGeneration valueGeneration = (GeneratedValueGeneration) generation;
if ( GenerationTiming.INSERT == valueGeneration.getGenerationTiming()
|| GenerationTiming.ALWAYS == valueGeneration.getGenerationTiming() ) {
return true;
}
}
}
return property.isInsertable();
}
private boolean checkPropertiesAudited(Iterator<Property> properties, ClassAuditingData auditingData) {
while ( properties.hasNext() ) {
final Property property = properties.next();

View File

@ -0,0 +1,85 @@
/*
* 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.generated;
import javax.persistence.EntityManager;
import org.hibernate.envers.test.BaseEnversJPAFunctionalTestCase;
import org.hibernate.envers.test.Priority;
import org.hibernate.testing.TestForIssue;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
/**
* @author Chris Cranford
*/
@TestForIssue(jiraKey = "HHH-10841")
public class GeneratedColumnTest extends BaseEnversJPAFunctionalTestCase {
private Integer entityId;
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] { SimpleEntity.class };
}
@Test
@Priority(10)
public void initData() {
EntityManager entityManager = getOrCreateEntityManager();
try {
// Revision 1
SimpleEntity se = new SimpleEntity();
se.setData( "data" );
entityManager.getTransaction().begin();
entityManager.persist( se );
entityManager.getTransaction().commit();
entityManager.clear();
entityId = se.getId();
// Revision 2
entityManager.getTransaction().begin();
se = entityManager.find( SimpleEntity.class, se.getId() );
se.setData( "data2" );
entityManager.merge( se );
entityManager.getTransaction().commit();
// Revision 3
entityManager.getTransaction().begin();
se = entityManager.find( SimpleEntity.class, se.getId() );
entityManager.remove( se );
entityManager.getTransaction().commit();
}
finally {
entityManager.close();
}
}
@Test
public void getRevisionCounts() {
assertEquals( 3, getAuditReader().getRevisions( SimpleEntity.class, entityId ).size() );
}
@Test
public void testRevisionHistory() {
// revision - insertion
final SimpleEntity rev1 = getAuditReader().find( SimpleEntity.class, entityId, 1 );
assertEquals( "data", rev1.getData() );
assertEquals( 1, rev1.getCaseNumberInsert() );
// revision - update
final SimpleEntity rev2 = getAuditReader().find( SimpleEntity.class, entityId, 2 );
assertEquals( "data2", rev2.getData() );
assertEquals( 1, rev2.getCaseNumberInsert() );
// revision - deletion
final SimpleEntity rev3 = getAuditReader().find( SimpleEntity.class, entityId, 3 );
assertEquals( "data2", rev2.getData() );
assertEquals( 1, rev2.getCaseNumberInsert() );
}
}

View File

@ -0,0 +1,57 @@
/*
* 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.generated;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import org.hibernate.annotations.Generated;
import org.hibernate.annotations.GenerationTime;
import org.hibernate.envers.Audited;
/**
* @author Chris Cranford
*/
@Entity
@Audited
public class SimpleEntity {
@Id
@GeneratedValue
private Integer id;
private String data;
@Generated(GenerationTime.INSERT)
@Column(columnDefinition = "integer auto_increment")
private int caseNumberInsert;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
public int getCaseNumberInsert() {
return caseNumberInsert;
}
public void setCaseNumberInsert(int caseNumberInsert) {
this.caseNumberInsert = caseNumberInsert;
}
}