HHH-8769 gracefully handle the lack of dynamic mode support in Envers

This commit is contained in:
Brett Meyer 2015-03-22 00:49:08 -04:00
parent 8d3d259963
commit bea160a7ad
3 changed files with 94 additions and 94 deletions

View File

@ -23,14 +23,12 @@
*/ */
package org.hibernate.envers.configuration.internal; package org.hibernate.envers.configuration.internal;
import java.io.ByteArrayOutputStream; import org.dom4j.Document;
import java.io.IOException; import org.dom4j.DocumentException;
import java.io.PrintWriter; import org.dom4j.Element;
import java.io.Writer; import org.dom4j.io.DOMWriter;
import java.util.HashMap; import org.dom4j.io.OutputFormat;
import java.util.Iterator; import org.dom4j.io.XMLWriter;
import java.util.Map;
import org.hibernate.MappingException; import org.hibernate.MappingException;
import org.hibernate.annotations.common.reflection.ReflectionManager; import org.hibernate.annotations.common.reflection.ReflectionManager;
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService; import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
@ -46,12 +44,13 @@ import org.hibernate.envers.internal.tools.graph.GraphTopologicalSort;
import org.hibernate.envers.strategy.AuditStrategy; import org.hibernate.envers.strategy.AuditStrategy;
import org.hibernate.mapping.PersistentClass; import org.hibernate.mapping.PersistentClass;
import org.dom4j.Document; import java.io.ByteArrayOutputStream;
import org.dom4j.DocumentException; import java.io.IOException;
import org.dom4j.Element; import java.io.PrintWriter;
import org.dom4j.io.DOMWriter; import java.io.Writer;
import org.dom4j.io.OutputFormat; import java.util.HashMap;
import org.dom4j.io.XMLWriter; import java.util.Iterator;
import java.util.Map;
/** /**
* @author Adam Warski (adam at warski dot org) * @author Adam Warski (adam at warski dot org)
@ -77,12 +76,15 @@ public class EntitiesConfigurator {
while ( classes.hasNext() ) { while ( classes.hasNext() ) {
final PersistentClass pc = classes.next(); final PersistentClass pc = classes.next();
// Ensure we're in POJO, not dynamic model, mapping.
if (pc.getClassName() != null) {
// Collecting information from annotations on the persistent class pc // Collecting information from annotations on the persistent class pc
final AnnotationsMetadataReader annotationsMetadataReader = final AnnotationsMetadataReader annotationsMetadataReader =
new AnnotationsMetadataReader( globalCfg, reflectionManager, pc ); new AnnotationsMetadataReader(globalCfg, reflectionManager, pc);
final ClassAuditingData auditData = annotationsMetadataReader.getAuditData(); final ClassAuditingData auditData = annotationsMetadataReader.getAuditData();
classesAuditingData.addClassAuditingData( pc, auditData ); classesAuditingData.addClassAuditingData(pc, auditData);
}
} }
// Now that all information is read we can update the calculated fields. // Now that all information is read we can update the calculated fields.

View File

@ -23,11 +23,8 @@
*/ */
package org.hibernate.envers.configuration.internal; package org.hibernate.envers.configuration.internal;
import java.util.Date; import org.dom4j.Document;
import java.util.Iterator; import org.dom4j.Element;
import java.util.Set;
import javax.persistence.Column;
import org.hibernate.MappingException; import org.hibernate.MappingException;
import org.hibernate.annotations.common.reflection.ReflectionManager; import org.hibernate.annotations.common.reflection.ReflectionManager;
import org.hibernate.annotations.common.reflection.XClass; import org.hibernate.annotations.common.reflection.XClass;
@ -58,8 +55,10 @@ import org.hibernate.mapping.PersistentClass;
import org.hibernate.type.LongType; import org.hibernate.type.LongType;
import org.hibernate.type.Type; import org.hibernate.type.Type;
import org.dom4j.Document; import javax.persistence.Column;
import org.dom4j.Element; import java.util.Date;
import java.util.Iterator;
import java.util.Set;
/** /**
* @author Adam Warski (adam at warski dot org) * @author Adam Warski (adam at warski dot org)
@ -319,23 +318,24 @@ public class RevisionInfoConfiguration {
final Iterator<PersistentClass> classes = cfg.getClassMappings(); final Iterator<PersistentClass> classes = cfg.getClassMappings();
while ( classes.hasNext() ) { while ( classes.hasNext() ) {
PersistentClass pc = classes.next(); PersistentClass pc = classes.next();
// Ensure we're in POJO, not dynamic model, mapping.
if (pc.getClassName() != null) {
XClass clazz; XClass clazz;
try { try {
clazz = reflectionManager.classForName( pc.getClassName(), this.getClass() ); clazz = reflectionManager.classForName(pc.getClassName(), this.getClass());
} } catch (ClassNotFoundException e) {
catch (ClassNotFoundException e) { throw new MappingException(e);
throw new MappingException( e );
} }
final RevisionEntity revisionEntity = clazz.getAnnotation( RevisionEntity.class ); final RevisionEntity revisionEntity = clazz.getAnnotation(RevisionEntity.class);
if ( revisionEntity != null ) { if (revisionEntity != null) {
if ( revisionEntityFound ) { if (revisionEntityFound) {
throw new MappingException( "Only one entity may be annotated with @RevisionEntity!" ); throw new MappingException("Only one entity may be annotated with @RevisionEntity!");
} }
// Checking if custom revision entity isn't audited // Checking if custom revision entity isn't audited
if ( clazz.getAnnotation( Audited.class ) != null ) { if (clazz.getAnnotation(Audited.class) != null) {
throw new MappingException( "An entity annotated with @RevisionEntity cannot be audited!" ); throw new MappingException("An entity annotated with @RevisionEntity cannot be audited!");
} }
revisionEntityFound = true; revisionEntityFound = true;
@ -352,14 +352,14 @@ public class RevisionInfoConfiguration {
modifiedEntityNamesFound modifiedEntityNamesFound
); );
if ( !revisionNumberFound.isSet() ) { if (!revisionNumberFound.isSet()) {
throw new MappingException( throw new MappingException(
"An entity annotated with @RevisionEntity must have a field annotated " + "An entity annotated with @RevisionEntity must have a field annotated " +
"with @RevisionNumber!" "with @RevisionNumber!"
); );
} }
if ( !revisionTimestampFound.isSet() ) { if (!revisionTimestampFound.isSet()) {
throw new MappingException( throw new MappingException(
"An entity annotated with @RevisionEntity must have a field annotated " + "An entity annotated with @RevisionEntity must have a field annotated " +
"with @RevisionTimestamp!" "with @RevisionTimestamp!"
@ -368,14 +368,14 @@ public class RevisionInfoConfiguration {
revisionInfoEntityName = pc.getEntityName(); revisionInfoEntityName = pc.getEntityName();
revisionInfoClass = pc.getMappedClass(); revisionInfoClass = pc.getMappedClass();
final Class<? extends RevisionListener> revisionListenerClass = getRevisionListenerClass( revisionEntity.value() ); final Class<? extends RevisionListener> revisionListenerClass = getRevisionListenerClass(revisionEntity.value());
revisionInfoTimestampType = pc.getProperty( revisionInfoTimestampData.getName() ).getType(); revisionInfoTimestampType = pc.getProperty(revisionInfoTimestampData.getName()).getType();
if ( globalCfg.isTrackEntitiesChangedInRevision() if (globalCfg.isTrackEntitiesChangedInRevision()
|| (globalCfg.isUseRevisionEntityWithNativeId() && DefaultTrackingModifiedEntitiesRevisionEntity.class || (globalCfg.isUseRevisionEntityWithNativeId() && DefaultTrackingModifiedEntitiesRevisionEntity.class
.isAssignableFrom( revisionInfoClass )) .isAssignableFrom(revisionInfoClass))
|| (!globalCfg.isUseRevisionEntityWithNativeId() && SequenceIdTrackingModifiedEntitiesRevisionEntity.class || (!globalCfg.isUseRevisionEntityWithNativeId() && SequenceIdTrackingModifiedEntitiesRevisionEntity.class
.isAssignableFrom( revisionInfoClass )) .isAssignableFrom(revisionInfoClass))
|| modifiedEntityNamesFound.isSet() ) { || modifiedEntityNamesFound.isSet()) {
// If tracking modified entities parameter is enabled, custom revision info entity is a subtype // If tracking modified entities parameter is enabled, custom revision info entity is a subtype
// of DefaultTrackingModifiedEntitiesRevisionEntity class, or @ModifiedEntityNames annotation is used. // of DefaultTrackingModifiedEntitiesRevisionEntity class, or @ModifiedEntityNames annotation is used.
revisionInfoGenerator = new DefaultTrackingModifiedEntitiesRevisionInfoGenerator( revisionInfoGenerator = new DefaultTrackingModifiedEntitiesRevisionInfoGenerator(
@ -383,9 +383,8 @@ public class RevisionInfoConfiguration {
revisionInfoClass, revisionListenerClass, revisionInfoTimestampData, isTimestampAsDate(), revisionInfoClass, revisionListenerClass, revisionInfoTimestampData, isTimestampAsDate(),
modifiedEntityNamesData modifiedEntityNamesData
); );
globalCfg.setTrackEntitiesChangedInRevision( true ); globalCfg.setTrackEntitiesChangedInRevision(true);
} } else {
else {
revisionInfoGenerator = new DefaultRevisionInfoGenerator( revisionInfoGenerator = new DefaultRevisionInfoGenerator(
revisionInfoEntityName, revisionInfoClass, revisionInfoEntityName, revisionInfoClass,
revisionListenerClass, revisionInfoTimestampData, isTimestampAsDate() revisionListenerClass, revisionInfoTimestampData, isTimestampAsDate()
@ -393,6 +392,7 @@ public class RevisionInfoConfiguration {
} }
} }
} }
}
// In case of a custom revision info generator, the mapping will be null. // In case of a custom revision info generator, the mapping will be null.
Document revisionInfoXmlMapping = null; Document revisionInfoXmlMapping = null;

View File

@ -1,13 +1,12 @@
package org.hibernate.envers.test.integration.dynamicmodel; package org.hibernate.envers.test.integration.dynamicmodel;
import static org.junit.Assert.assertNotNull; import org.hibernate.envers.test.BaseEnversJPAFunctionalTestCase;
import org.hibernate.testing.TestForIssue;
import org.junit.Test;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import org.hibernate.envers.test.BaseEnversJPAFunctionalTestCase; import static org.junit.Assert.assertNotNull;
import org.hibernate.testing.FailureExpected;
import org.hibernate.testing.TestForIssue;
import org.junit.Test;
/** /**
* @author Felix Feisst (feisst dot felix at gmail dot com) * @author Felix Feisst (feisst dot felix at gmail dot com)
@ -24,7 +23,6 @@ public class DynamicModelTest extends BaseEnversJPAFunctionalTestCase {
* Tests that an EntityManager can be created when using a dynamic model mapping. * Tests that an EntityManager can be created when using a dynamic model mapping.
*/ */
@Test @Test
@FailureExpected(jiraKey = "HHH-8769")
public void testDynamicModelMapping() { public void testDynamicModelMapping() {
EntityManager entityManager = getOrCreateEntityManager(); EntityManager entityManager = getOrCreateEntityManager();
assertNotNull( "Expected an entity manager to be returned", entityManager ); assertNotNull( "Expected an entity manager to be returned", entityManager );