HHH-4439 - Deprecate Audited.auditParents

This commit is contained in:
Lukasz Antoniak 2012-01-06 14:14:07 +01:00
parent a49e02b239
commit 0a08780e13
14 changed files with 413 additions and 112 deletions

View File

@ -313,9 +313,10 @@
<para> <para>
If you'd like to audit properties of a superclass of an entity, which are not explicitly audited (which If you'd like to audit properties of a superclass of an entity, which are not explicitly audited (which
don't have the <literal>@Audited</literal> annotation on any properties or on the class), don't have the <literal>@Audited</literal> annotation on any properties or on the class), you can list the
you can list the superclasses in the <literal>auditParents</literal> attribute of the superclasses in the <literal>auditParents</literal> attribute of the <interfacename>@Audited</interfacename>
<interfacename>@Audited</interfacename> annotation. annotation. Please note that <literal>auditParents</literal> feature has been deprecated. Use
<literal>@AuditOverride(forClass = SomeEntity.class, isAudited = true/false)</literal> instead.
</para> </para>
</section> </section>

View File

@ -26,10 +26,11 @@ import static java.lang.annotation.RetentionPolicy.RUNTIME;
public @interface AuditOverride { public @interface AuditOverride {
/** /**
* @return <strong>Required</strong> Name of the field (or property) whose mapping * @return Name of the field (or property) whose mapping is being overridden. Allows empty value if
* is being overridden. * {@link AuditOverride} is used to change auditing behavior of all attributes inherited from
* {@link MappedSuperclass} type.
*/ */
String name(); String name() default "";
/** /**
* @return Indicates if the field (or property) is audited; defaults to {@code true}. * @return Indicates if the field (or property) is audited; defaults to {@code true}.
@ -47,5 +48,5 @@ public @interface AuditOverride {
* {@link AuditOverride} is used to change auditing behavior of attributes inherited from {@link MappedSuperclass} * {@link AuditOverride} is used to change auditing behavior of attributes inherited from {@link MappedSuperclass}
* type. * type.
*/ */
Class relatedClass() default void.class; Class forClass() default void.class;
} }

View File

@ -55,6 +55,8 @@ public @interface Audited {
* *
* If a parent type lists any of its parent types using this attribute, all properties in the specified classes * If a parent type lists any of its parent types using this attribute, all properties in the specified classes
* will also be audited. * will also be audited.
*
* @deprecated Use {@code @AuditOverride(forClass=SomeEntity.class)} instead.
*/ */
Class[] auditParents() default {}; Class[] auditParents() default {};
} }

View File

@ -2,7 +2,6 @@ package org.hibernate.envers.configuration.metadata.reader;
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -25,6 +24,7 @@ import org.hibernate.envers.NotAudited;
import org.hibernate.envers.RelationTargetAuditMode; import org.hibernate.envers.RelationTargetAuditMode;
import org.hibernate.envers.configuration.GlobalConfiguration; import org.hibernate.envers.configuration.GlobalConfiguration;
import org.hibernate.envers.tools.MappingTools; import org.hibernate.envers.tools.MappingTools;
import org.hibernate.envers.tools.StringTools;
import org.hibernate.envers.tools.Tools; import org.hibernate.envers.tools.Tools;
import org.hibernate.mapping.Component; import org.hibernate.mapping.Component;
import org.hibernate.mapping.Property; import org.hibernate.mapping.Property;
@ -60,6 +60,9 @@ public class AuditedPropertiesReader {
private final Set<XProperty> overriddenAuditedProperties; private final Set<XProperty> overriddenAuditedProperties;
private final Set<XProperty> overriddenNotAuditedProperties; private final Set<XProperty> overriddenNotAuditedProperties;
private final Set<XClass> overriddenAuditedClasses;
private final Set<XClass> overriddenNotAuditedClasses;
public AuditedPropertiesReader(ModificationStore defaultStore, public AuditedPropertiesReader(ModificationStore defaultStore,
PersistentPropertiesSource persistentPropertiesSource, PersistentPropertiesSource persistentPropertiesSource,
AuditedPropertiesHolder auditedPropertiesHolder, AuditedPropertiesHolder auditedPropertiesHolder,
@ -79,36 +82,51 @@ public class AuditedPropertiesReader {
overriddenAuditedProperties = newHashSet(); overriddenAuditedProperties = newHashSet();
overriddenNotAuditedProperties = newHashSet(); overriddenNotAuditedProperties = newHashSet();
overriddenAuditedClasses = newHashSet();
overriddenNotAuditedClasses = newHashSet();
} }
public void read() { public void read() {
// First reading the access types for the persistent properties. // First reading the access types for the persistent properties.
readPersistentPropertiesAccess(); readPersistentPropertiesAccess();
// Retrieve classes that are explicitly marked for auditing process by any superclass of currently mapped // Retrieve classes and properties that are explicitly marked for auditing process by any superclass
// entity or itself. // of currently mapped entity or itself.
XClass clazz = persistentPropertiesSource.getXClass(); XClass clazz = persistentPropertiesSource.getXClass();
Set<XClass> declaredAuditedSuperclasses = new HashSet<XClass>(); doReadOverrideAudited(clazz);
doGetDeclaredAuditedSuperclasses(clazz, declaredAuditedSuperclasses);
doReadOverrideAuditedProperties(clazz);
// Adding all properties from the given class. // Adding all properties from the given class.
addPropertiesFromClass(clazz, declaredAuditedSuperclasses); addPropertiesFromClass(clazz);
} }
/** /**
* Recursively constructs sets of audited and not audited properties which behavior has been overridden * Recursively constructs sets of audited and not audited properties and classes which behavior has been overridden
* using @AuditOverride annotation. * using @AuditOverride annotation.
* @param clazz Class that is being processed. Currently mapped entity shall be passed during first invocation. * @param clazz Class that is being processed. Currently mapped entity shall be passed during first invocation.
*/ */
private void doReadOverrideAuditedProperties(XClass clazz) { private void doReadOverrideAudited(XClass clazz) {
/* TODO: Code to remove with @Audited.auditParents - start. */
Audited allClassAudited = clazz.getAnnotation(Audited.class);
if (allClassAudited != null && allClassAudited.auditParents().length > 0) {
for (Class c : allClassAudited.auditParents()) {
XClass parentClass = reflectionManager.toXClass(c);
checkSuperclass(clazz, parentClass);
if (!overriddenNotAuditedClasses.contains(parentClass)) {
// If the class has not been marked as not audited by the subclass.
overriddenAuditedClasses.add(parentClass);
}
}
}
/* TODO: Code to remove with @Audited.auditParents - finish. */
List<AuditOverride> auditOverrides = computeAuditOverrides(clazz); List<AuditOverride> auditOverrides = computeAuditOverrides(clazz);
for (AuditOverride auditOverride : auditOverrides) { for (AuditOverride auditOverride : auditOverrides) {
if (auditOverride.relatedClass() != void.class) { if (auditOverride.forClass() != void.class) {
XClass overrideClass = reflectionManager.toXClass(auditOverride.relatedClass()); XClass overrideClass = reflectionManager.toXClass(auditOverride.forClass());
checkSuperclass(clazz, overrideClass); checkSuperclass(clazz, overrideClass);
String propertyName = auditOverride.name(); String propertyName = auditOverride.name();
if (propertyName != null) { if (!StringTools.isEmpty(propertyName)) {
// Overridden @Audited annotation on property level.
XProperty property = getProperty(overrideClass, propertyName); XProperty property = getProperty(overrideClass, propertyName);
if (auditOverride.isAudited()) { if (auditOverride.isAudited()) {
if (!overriddenNotAuditedProperties.contains(property)) { if (!overriddenNotAuditedProperties.contains(property)) {
@ -121,12 +139,25 @@ public class AuditedPropertiesReader {
overriddenNotAuditedProperties.add(property); overriddenNotAuditedProperties.add(property);
} }
} }
} else {
// Overridden @Audited annotation on class level.
if (auditOverride.isAudited()) {
if (!overriddenNotAuditedClasses.contains(overrideClass)) {
// If the class has not been marked as not audited by the subclass.
overriddenAuditedClasses.add(overrideClass);
}
} else {
if (!overriddenAuditedClasses.contains(overrideClass)) {
// If the class has not been marked as audited by the subclass.
overriddenNotAuditedClasses.add(overrideClass);
}
}
} }
} }
} }
XClass superclass = clazz.getSuperclass(); XClass superclass = clazz.getSuperclass();
if (!clazz.isInterface() && !Object.class.getName().equals(superclass.getName())) { if (!clazz.isInterface() && !Object.class.getName().equals(superclass.getName())) {
doReadOverrideAuditedProperties(superclass); doReadOverrideAudited(superclass);
} }
} }
@ -148,28 +179,6 @@ public class AuditedPropertiesReader {
return Collections.EMPTY_LIST; return Collections.EMPTY_LIST;
} }
/**
* Recursively constructs a set of classes that have been declared for auditing process.
* @param clazz Class that is being processed. Currently mapped entity shall be passed during first invocation.
* @param declaredAuditedSuperclasses Total collection of classes listed in {@link Audited#auditParents()} property
* by any superclass starting with class specified as the first argument.
*/
@SuppressWarnings("unchecked")
private void doGetDeclaredAuditedSuperclasses(XClass clazz, Set<XClass> declaredAuditedSuperclasses) {
Audited allClassAudited = clazz.getAnnotation(Audited.class);
if (allClassAudited != null && allClassAudited.auditParents().length > 0) {
for (Class c : allClassAudited.auditParents()) {
XClass parentClass = reflectionManager.toXClass(c);
checkSuperclass(clazz, parentClass);
declaredAuditedSuperclasses.add(parentClass);
}
}
XClass superclass = clazz.getSuperclass();
if (!clazz.isInterface() && !Object.class.getName().equals(superclass.getName())) {
doGetDeclaredAuditedSuperclasses(superclass, declaredAuditedSuperclasses);
}
}
/** /**
* Checks whether one class is assignable from another. If not {@link MappingException} is thrown. * Checks whether one class is assignable from another. If not {@link MappingException} is thrown.
* @param child Subclass. * @param child Subclass.
@ -229,19 +238,26 @@ public class AuditedPropertiesReader {
/** /**
* @param clazz Class which properties are currently being added. * @param clazz Class which properties are currently being added.
* @param declaredAuditedSuperclasses Collection of superclasses that have been explicitly declared to be audited.
* @return {@link Audited} annotation of specified class. If processed type hasn't been explicitly marked, method * @return {@link Audited} annotation of specified class. If processed type hasn't been explicitly marked, method
* checks whether given class exists in collection passed as the second argument. In case of success, * checks whether given class exists in {@code overriddenAuditedClasses} collection. In case of success,
* {@link Audited} configuration of currently mapped entity is returned, otherwise {@code null}. * {@link Audited} configuration of currently mapped entity is returned (or the default if {@link Audited}
* applied on property level), otherwise {@code null}. If processed type exists in
* {@code overriddenNotAuditedClasses} collection, the result is also {@code null}.
*/ */
private Audited computeAuditConfiguration(XClass clazz, Set<XClass> declaredAuditedSuperclasses) { private Audited computeAuditConfiguration(XClass clazz) {
Audited allClassAudited = clazz.getAnnotation(Audited.class); Audited allClassAudited = clazz.getAnnotation(Audited.class);
// If processed class is not explicitly marked with @Audited annotation, check whether auditing is // If processed class is not explicitly marked with @Audited annotation, check whether auditing is
// forced by any of its child entities configuration (@Audited.auditParents). // forced by any of its child entities configuration (@AuditedOverride.forClass).
if (allClassAudited == null && declaredAuditedSuperclasses.contains(clazz)) { if (allClassAudited == null && overriddenAuditedClasses.contains(clazz)) {
// Declared audited parent copies @Audited.modStore and @Audited.targetAuditMode configuration from // Declared audited parent copies @Audited.modStore and @Audited.targetAuditMode configuration from
// currently mapped entity. // currently mapped entity.
allClassAudited = persistentPropertiesSource.getXClass().getAnnotation(Audited.class); allClassAudited = persistentPropertiesSource.getXClass().getAnnotation(Audited.class);
if (allClassAudited == null) {
// If parent class declares @Audited on the field/property level.
allClassAudited = DEFAULT_AUDITED;
}
} else if (allClassAudited != null && overriddenNotAuditedClasses.contains(clazz)) {
return null;
} }
return allClassAudited; return allClassAudited;
} }
@ -249,11 +265,9 @@ public class AuditedPropertiesReader {
/** /**
* Recursively adds all audited properties of entity class and its superclasses. * Recursively adds all audited properties of entity class and its superclasses.
* @param clazz Currently processed class. * @param clazz Currently processed class.
* @param declaredAuditedSuperclasses Collection of classes that are declared to be audited
* (see {@link Audited#auditParents()}).
*/ */
private void addPropertiesFromClass(XClass clazz, Set<XClass> declaredAuditedSuperclasses) { private void addPropertiesFromClass(XClass clazz) {
Audited allClassAudited = computeAuditConfiguration(clazz, declaredAuditedSuperclasses); Audited allClassAudited = computeAuditConfiguration(clazz);
//look in the class //look in the class
addFromProperties(clazz.getDeclaredProperties("field"), "field", fieldAccessedPersistentProperties, allClassAudited); addFromProperties(clazz.getDeclaredProperties("field"), "field", fieldAccessedPersistentProperties, allClassAudited);
@ -262,7 +276,7 @@ public class AuditedPropertiesReader {
if(allClassAudited != null || !auditedPropertiesHolder.isEmpty()) { if(allClassAudited != null || !auditedPropertiesHolder.isEmpty()) {
XClass superclazz = clazz.getSuperclass(); XClass superclazz = clazz.getSuperclass();
if (!clazz.isInterface() && !"java.lang.Object".equals(superclazz.getName())) { if (!clazz.isInterface() && !"java.lang.Object".equals(superclazz.getName())) {
addPropertiesFromClass(superclazz, declaredAuditedSuperclasses); addPropertiesFromClass(superclazz);
} }
} }
} }
@ -400,34 +414,20 @@ public class AuditedPropertiesReader {
PropertyAuditingData propertyData, Audited allClassAudited) { PropertyAuditingData propertyData, Audited allClassAudited) {
// Checking if this property is explicitly audited or if all properties are. // Checking if this property is explicitly audited or if all properties are.
Audited aud = (property.isAnnotationPresent(Audited.class)) ? (property.getAnnotation(Audited.class)) : allClassAudited; Audited aud = (property.isAnnotationPresent(Audited.class)) ? (property.getAnnotation(Audited.class)) : allClassAudited;
//Audited aud = property.getAnnotation(Audited.class); if (aud == null && overriddenAuditedProperties.contains(property) && !overriddenNotAuditedProperties.contains(property)) {
// Assigning @Audited defaults. If anyone needs to customize those values in the future,
// appropriate fields shall be added to @AuditOverride annotation.
aud = DEFAULT_AUDITED;
}
if (aud != null) { if (aud != null) {
propertyData.setStore(aud.modStore()); propertyData.setStore(aud.modStore());
propertyData.setRelationTargetAuditMode(aud.targetAuditMode()); propertyData.setRelationTargetAuditMode(aud.targetAuditMode());
return true; return true;
} else if (overriddenAuditedProperties.contains(property)) {
// Filling property data with @Audited defaults. If anyone needs to customize those values in the future,
// appropriate fields shall be added to @AuditOverride annotation.
fillAuditedDefaults(propertyData);
return true;
} else { } else {
return false; return false;
} }
} }
/**
* Fills given property data with default values of @Audited.modStore and @Audited.targetAuditMode attributes.
* @param propertyData Property data.
*/
private void fillAuditedDefaults(PropertyAuditingData propertyData) {
try {
propertyData.setStore((ModificationStore) Audited.class.getMethod("modStore").getDefaultValue());
propertyData.setRelationTargetAuditMode((RelationTargetAuditMode) Audited.class.getMethod("targetAuditMode").getDefaultValue());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private void setPropertyAuditMappedBy(XProperty property, PropertyAuditingData propertyData) { private void setPropertyAuditMappedBy(XProperty property, PropertyAuditingData propertyData) {
AuditMappedBy auditMappedBy = property.getAnnotation(AuditMappedBy.class); AuditMappedBy auditMappedBy = property.getAnnotation(AuditMappedBy.class);
if (auditMappedBy != null) { if (auditMappedBy != null) {
@ -503,6 +503,13 @@ public class AuditedPropertiesReader {
return true; return true;
} }
private static Audited DEFAULT_AUDITED = new Audited() {
public ModificationStore modStore() { return ModificationStore.FULL; }
public RelationTargetAuditMode targetAuditMode() { return RelationTargetAuditMode.AUDITED; }
public Class[] auditParents() { return new Class[0]; }
public Class<? extends Annotation> annotationType() { return this.getClass(); }
};
private static AuditJoinTable DEFAULT_AUDIT_JOIN_TABLE = new AuditJoinTable() { private static AuditJoinTable DEFAULT_AUDIT_JOIN_TABLE = new AuditJoinTable() {
public String name() { return ""; } public String name() { return ""; }
public String schema() { return ""; } public String schema() { return ""; }

View File

@ -0,0 +1,78 @@
package org.hibernate.envers.test.integration.superclass.auditoverride;
import org.hibernate.ejb.Ejb3Configuration;
import org.hibernate.envers.test.AbstractEntityTest;
import org.hibernate.envers.test.Priority;
import org.hibernate.mapping.Column;
import org.hibernate.mapping.Table;
import org.hibernate.testing.TestForIssue;
import org.junit.Assert;
import org.junit.Test;
import javax.persistence.EntityManager;
/**
* @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
*/
@TestForIssue(jiraKey = "HHH-4439")
public class AuditClassOverrideTest extends AbstractEntityTest {
private Integer classAuditedEntityId = null;
private Integer classNotAuditedEntityId = null;
private Table classAuditedTable = null;
private Table classNotAuditedTable = null;
@Override
public void configure(Ejb3Configuration cfg) {
cfg.addAnnotatedClass(ClassOverrideAuditedEntity.class);
cfg.addAnnotatedClass(ClassOverrideNotAuditedEntity.class);
}
@Test
@Priority(10)
public void initData() {
EntityManager em = getEntityManager();
// Revision 1
em.getTransaction().begin();
ClassOverrideAuditedEntity classOverrideAuditedEntity = new ClassOverrideAuditedEntity("data 1", 1, "data 2");
em.persist(classOverrideAuditedEntity);
em.getTransaction().commit();
classAuditedEntityId = classOverrideAuditedEntity.getId();
// Revision 2
em.getTransaction().begin();
ClassOverrideNotAuditedEntity classOverrideNotAuditedEntity = new ClassOverrideNotAuditedEntity("data 1", 1, "data 2");
em.persist(classOverrideNotAuditedEntity);
em.getTransaction().commit();
classNotAuditedEntityId = classOverrideNotAuditedEntity.getId();
classAuditedTable = getCfg().getClassMapping("org.hibernate.envers.test.integration.superclass.auditoverride.ClassOverrideAuditedEntity_AUD").getTable();
classNotAuditedTable = getCfg().getClassMapping("org.hibernate.envers.test.integration.superclass.auditoverride.ClassOverrideNotAuditedEntity_AUD").getTable();
}
@Test
public void testAuditedProperty() {
Assert.assertNotNull(classAuditedTable.getColumn(new Column("number1")));
Assert.assertNotNull(classAuditedTable.getColumn(new Column("str1")));
Assert.assertNotNull(classAuditedTable.getColumn(new Column("str2")));
Assert.assertNotNull(classNotAuditedTable.getColumn(new Column("str2")));
}
@Test
public void testNotAuditedProperty() {
Assert.assertNull(classNotAuditedTable.getColumn(new Column("number1")));
Assert.assertNull(classNotAuditedTable.getColumn(new Column("str1")));
}
@Test
public void testHistoryOfClassOverrideAuditedEntity() {
ClassOverrideAuditedEntity ver1 = new ClassOverrideAuditedEntity("data 1", 1, classAuditedEntityId, "data 2");
Assert.assertEquals(ver1, getAuditReader().find(ClassOverrideAuditedEntity.class, classAuditedEntityId, 1));
}
@Test
public void testHistoryOfClassOverrideNotAuditedEntity() {
ClassOverrideNotAuditedEntity ver1 = new ClassOverrideNotAuditedEntity(null, null, classNotAuditedEntityId, "data 2");
Assert.assertEquals(ver1, getAuditReader().find(ClassOverrideNotAuditedEntity.class, classNotAuditedEntityId, 2));
}
}

View File

@ -15,7 +15,7 @@ import javax.persistence.EntityManager;
* @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com) * @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
*/ */
@TestForIssue(jiraKey = "HHH-4439") @TestForIssue(jiraKey = "HHH-4439")
public class AuditOverrideTest extends AbstractEntityTest { public class AuditPropertyOverrideTest extends AbstractEntityTest {
private Integer propertyEntityId = null; private Integer propertyEntityId = null;
private Integer transitiveEntityId = null; private Integer transitiveEntityId = null;
private Integer auditedEntityId = null; private Integer auditedEntityId = null;
@ -25,8 +25,8 @@ public class AuditOverrideTest extends AbstractEntityTest {
@Override @Override
public void configure(Ejb3Configuration cfg) { public void configure(Ejb3Configuration cfg) {
cfg.addAnnotatedClass(PropertyOverrideTestEntity.class); cfg.addAnnotatedClass(PropertyOverrideEntity.class);
cfg.addAnnotatedClass(TransitiveOverrideTestEntity.class); cfg.addAnnotatedClass(TransitiveOverrideEntity.class);
cfg.addAnnotatedClass(AuditedSpecialEntity.class); cfg.addAnnotatedClass(AuditedSpecialEntity.class);
} }
@ -37,14 +37,14 @@ public class AuditOverrideTest extends AbstractEntityTest {
// Revision 1 // Revision 1
em.getTransaction().begin(); em.getTransaction().begin();
PropertyOverrideTestEntity propertyEntity = new PropertyOverrideTestEntity("data 1", 1, "data 2"); PropertyOverrideEntity propertyEntity = new PropertyOverrideEntity("data 1", 1, "data 2");
em.persist(propertyEntity); em.persist(propertyEntity);
em.getTransaction().commit(); em.getTransaction().commit();
propertyEntityId = propertyEntity.getId(); propertyEntityId = propertyEntity.getId();
// Revision 2 // Revision 2
em.getTransaction().begin(); em.getTransaction().begin();
TransitiveOverrideTestEntity transitiveEntity = new TransitiveOverrideTestEntity("data 1", 1, "data 2", 2, "data 3"); TransitiveOverrideEntity transitiveEntity = new TransitiveOverrideEntity("data 1", 1, "data 2", 2, "data 3");
em.persist(transitiveEntity); em.persist(transitiveEntity);
em.getTransaction().commit(); em.getTransaction().commit();
transitiveEntityId = transitiveEntity.getId(); transitiveEntityId = transitiveEntity.getId();
@ -56,8 +56,8 @@ public class AuditOverrideTest extends AbstractEntityTest {
em.getTransaction().commit(); em.getTransaction().commit();
auditedEntityId = auditedEntity.getId(); auditedEntityId = auditedEntity.getId();
propertyTable = getCfg().getClassMapping("org.hibernate.envers.test.integration.superclass.auditoverride.PropertyOverrideTestEntity_AUD").getTable(); propertyTable = getCfg().getClassMapping("org.hibernate.envers.test.integration.superclass.auditoverride.PropertyOverrideEntity_AUD").getTable();
transitiveTable = getCfg().getClassMapping("org.hibernate.envers.test.integration.superclass.auditoverride.TransitiveOverrideTestEntity_AUD").getTable(); transitiveTable = getCfg().getClassMapping("org.hibernate.envers.test.integration.superclass.auditoverride.TransitiveOverrideEntity_AUD").getTable();
auditedTable = getCfg().getClassMapping("org.hibernate.envers.test.integration.superclass.auditoverride.AuditedSpecialEntity_AUD").getTable(); auditedTable = getCfg().getClassMapping("org.hibernate.envers.test.integration.superclass.auditoverride.AuditedSpecialEntity_AUD").getTable();
} }
@ -81,14 +81,14 @@ public class AuditOverrideTest extends AbstractEntityTest {
@Test @Test
public void testHistoryOfPropertyOverrideEntity() { public void testHistoryOfPropertyOverrideEntity() {
PropertyOverrideTestEntity ver1 = new PropertyOverrideTestEntity(null, 1, propertyEntityId, "data 2"); PropertyOverrideEntity ver1 = new PropertyOverrideEntity(null, 1, propertyEntityId, "data 2");
Assert.assertEquals(ver1, getAuditReader().find(PropertyOverrideTestEntity.class, propertyEntityId, 1)); Assert.assertEquals(ver1, getAuditReader().find(PropertyOverrideEntity.class, propertyEntityId, 1));
} }
@Test @Test
public void testHistoryOfTransitiveOverrideEntity() { public void testHistoryOfTransitiveOverrideEntity() {
TransitiveOverrideTestEntity ver1 = new TransitiveOverrideTestEntity("data 1", 1, transitiveEntityId, "data 2", 2, "data 3"); TransitiveOverrideEntity ver1 = new TransitiveOverrideEntity("data 1", 1, transitiveEntityId, "data 2", 2, "data 3");
Assert.assertEquals(ver1, getAuditReader().find(TransitiveOverrideTestEntity.class, transitiveEntityId, 2)); Assert.assertEquals(ver1, getAuditReader().find(TransitiveOverrideEntity.class, transitiveEntityId, 2));
} }
@Test @Test

View File

@ -0,0 +1,86 @@
package org.hibernate.envers.test.integration.superclass.auditoverride;
import org.hibernate.envers.Audited;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import java.io.Serializable;
/**
* @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
*/
@Audited
@MappedSuperclass
public class AuditedBaseEntity implements Serializable {
@Id
@GeneratedValue
private Integer id;
private String str1;
private Integer number1;
public AuditedBaseEntity() {
}
public AuditedBaseEntity(String str1, Integer number1, Integer id) {
this.id = id;
this.str1 = str1;
this.number1 = number1;
}
public AuditedBaseEntity(String str1, Integer number1) {
this.str1 = str1;
this.number1 = number1;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getStr1() {
return str1;
}
public void setStr1(String str1) {
this.str1 = str1;
}
public Integer getNumber1() {
return number1;
}
public void setNumber1(Integer number1) {
this.number1 = number1;
}
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof AuditedBaseEntity)) return false;
AuditedBaseEntity that = (AuditedBaseEntity) o;
if (id != null ? !id.equals(that.id) : that.id != null) return false;
if (number1 != null ? !number1.equals(that.number1) : that.number1 != null) return false;
if (str1 != null ? !str1.equals(that.str1) : that.str1 != null) return false;
return true;
}
public int hashCode() {
int result;
result = (id != null ? id.hashCode() : 0);
result = 31 * result + (str1 != null ? str1.hashCode() : 0);
result = 31 * result + (number1 != null ? number1.hashCode() : 0);
return result;
}
public String toString() {
return "AuditedBaseEntity(id = " + id + ", str1 = " + str1 + ", number1 = " + number1 + ")";
}
}

View File

@ -10,8 +10,8 @@ import javax.persistence.Entity;
* @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com) * @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
*/ */
@Entity @Entity
@AuditOverrides({@AuditOverride(relatedClass = NotAnnotatedBaseEntity.class, name = "str1", isAudited = true)}) @AuditOverrides({@AuditOverride(forClass = NotAuditedBaseEntity.class, name = "str1", isAudited = true)})
public class AuditedSpecialEntity extends NotAnnotatedBaseEntity { public class AuditedSpecialEntity extends NotAuditedBaseEntity {
@Audited @Audited
private String str2; private String str2;

View File

@ -0,0 +1,63 @@
package org.hibernate.envers.test.integration.superclass.auditoverride;
import org.hibernate.envers.AuditOverride;
import org.hibernate.envers.AuditOverrides;
import org.hibernate.envers.Audited;
import javax.persistence.Entity;
/**
* @author Lukasz Antoniak (lukasz.antoniak at gmail dot com)
*/
@Entity
@AuditOverrides({@AuditOverride(forClass = NotAuditedBaseEntity.class, isAudited = true)})
public class ClassOverrideAuditedEntity extends NotAuditedBaseEntity {
@Audited
private String str2;
public ClassOverrideAuditedEntity() {
}
public ClassOverrideAuditedEntity(String str1, Integer number, String str2) {
super(str1, number);
this.str2 = str2;
}
public ClassOverrideAuditedEntity(String str1, Integer number, Integer id, String str2) {
super(str1, number, id);
this.str2 = str2;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof ClassOverrideAuditedEntity)) return false;
if (!super.equals(o)) return false;
ClassOverrideAuditedEntity that = (ClassOverrideAuditedEntity) o;
if (str2 != null ? !str2.equals(that.str2) : that.str2 != null) return false;
return true;
}
@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + (str2 != null ? str2.hashCode() : 0);
return result;
}
@Override
public String toString() {
return "ClassOverrideAuditedEntity(" + super.toString() + ", str2 = " + str2 + ")";
}
public String getStr2() {
return str2;
}
public void setStr2(String str2) {
this.str2 = str2;
}
}

View File

@ -0,0 +1,63 @@
package org.hibernate.envers.test.integration.superclass.auditoverride;
import org.hibernate.envers.AuditOverride;
import org.hibernate.envers.AuditOverrides;
import org.hibernate.envers.Audited;
import javax.persistence.Entity;
/**
* @author Lukasz Antoniak (lukasz.antoniak at gmail dot com)
*/
@Entity
@AuditOverrides({@AuditOverride(forClass = AuditedBaseEntity.class, isAudited = false)})
public class ClassOverrideNotAuditedEntity extends AuditedBaseEntity {
@Audited
private String str2;
public ClassOverrideNotAuditedEntity() {
}
public ClassOverrideNotAuditedEntity(String str1, Integer number, String str2) {
super(str1, number);
this.str2 = str2;
}
public ClassOverrideNotAuditedEntity(String str1, Integer number, Integer id, String str2) {
super(str1, number, id);
this.str2 = str2;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof ClassOverrideNotAuditedEntity)) return false;
if (!super.equals(o)) return false;
ClassOverrideNotAuditedEntity that = (ClassOverrideNotAuditedEntity) o;
if (str2 != null ? !str2.equals(that.str2) : that.str2 != null) return false;
return true;
}
@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + (str2 != null ? str2.hashCode() : 0);
return result;
}
@Override
public String toString() {
return "ClassOverrideNotAuditedEntity(" + super.toString() + ", str2 = " + str2 + ")";
}
public String getStr2() {
return str2;
}
public void setStr2(String str2) {
this.str2 = str2;
}
}

View File

@ -11,8 +11,8 @@ import javax.persistence.MappedSuperclass;
* @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com) * @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
*/ */
@MappedSuperclass @MappedSuperclass
@AuditOverrides({@AuditOverride(relatedClass = BaseEntity.class, name = "str1", isAudited = false), @AuditOverrides({@AuditOverride(forClass = BaseEntity.class, name = "str1", isAudited = false),
@AuditOverride(relatedClass = BaseEntity.class, name = "number1", isAudited = true)}) @AuditOverride(forClass = BaseEntity.class, name = "number1", isAudited = true)})
public class ExtendedBaseEntity extends BaseEntity { public class ExtendedBaseEntity extends BaseEntity {
@Audited @Audited
private String str2; private String str2;

View File

@ -9,7 +9,7 @@ import java.io.Serializable;
* @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com) * @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
*/ */
@MappedSuperclass @MappedSuperclass
public class NotAnnotatedBaseEntity implements Serializable { public class NotAuditedBaseEntity implements Serializable {
@Id @Id
@GeneratedValue @GeneratedValue
private Integer id; private Integer id;
@ -18,16 +18,16 @@ public class NotAnnotatedBaseEntity implements Serializable {
private Integer number1; private Integer number1;
public NotAnnotatedBaseEntity() { public NotAuditedBaseEntity() {
} }
public NotAnnotatedBaseEntity(String str1, Integer number1, Integer id) { public NotAuditedBaseEntity(String str1, Integer number1, Integer id) {
this.id = id; this.id = id;
this.str1 = str1; this.str1 = str1;
this.number1 = number1; this.number1 = number1;
} }
public NotAnnotatedBaseEntity(String str1, Integer number1) { public NotAuditedBaseEntity(String str1, Integer number1) {
this.str1 = str1; this.str1 = str1;
this.number1 = number1; this.number1 = number1;
} }
@ -58,9 +58,9 @@ public class NotAnnotatedBaseEntity implements Serializable {
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) return true; if (this == o) return true;
if (!(o instanceof NotAnnotatedBaseEntity)) return false; if (!(o instanceof NotAuditedBaseEntity)) return false;
NotAnnotatedBaseEntity that = (NotAnnotatedBaseEntity) o; NotAuditedBaseEntity that = (NotAuditedBaseEntity) o;
if (id != null ? !id.equals(that.id) : that.id != null) return false; if (id != null ? !id.equals(that.id) : that.id != null) return false;
if (number1 != null ? !number1.equals(that.number1) : that.number1 != null) return false; if (number1 != null ? !number1.equals(that.number1) : that.number1 != null) return false;
@ -78,6 +78,6 @@ public class NotAnnotatedBaseEntity implements Serializable {
} }
public String toString() { public String toString() {
return "NotAnnotatedBaseEntity(id = " + id + ", str1 = " + str1 + ", number1 = " + number1 + ")"; return "NotAuditedBaseEntity(id = " + id + ", str1 = " + str1 + ", number1 = " + number1 + ")";
} }
} }

View File

@ -11,20 +11,20 @@ import javax.persistence.Entity;
*/ */
@Entity @Entity
@Audited @Audited
@AuditOverrides({@AuditOverride(relatedClass = BaseEntity.class, name = "str1", isAudited = false), @AuditOverrides({@AuditOverride(forClass = BaseEntity.class, name = "str1", isAudited = false),
@AuditOverride(relatedClass = BaseEntity.class, name = "number1", isAudited = true)}) @AuditOverride(forClass = BaseEntity.class, name = "number1", isAudited = true)})
public class PropertyOverrideTestEntity extends BaseEntity { public class PropertyOverrideEntity extends BaseEntity {
private String str2; private String str2;
public PropertyOverrideTestEntity() { public PropertyOverrideEntity() {
} }
public PropertyOverrideTestEntity(String str1, Integer number1, String str2) { public PropertyOverrideEntity(String str1, Integer number1, String str2) {
super(str1, number1); super(str1, number1);
this.str2 = str2; this.str2 = str2;
} }
public PropertyOverrideTestEntity(String str1, Integer number1, Integer id, String str2) { public PropertyOverrideEntity(String str1, Integer number1, Integer id, String str2) {
super(str1, number1, id); super(str1, number1, id);
this.str2 = str2; this.str2 = str2;
} }
@ -32,10 +32,10 @@ public class PropertyOverrideTestEntity extends BaseEntity {
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) return true; if (this == o) return true;
if (!(o instanceof PropertyOverrideTestEntity)) return false; if (!(o instanceof PropertyOverrideEntity)) return false;
if (!super.equals(o)) return false; if (!super.equals(o)) return false;
PropertyOverrideTestEntity that = (PropertyOverrideTestEntity) o; PropertyOverrideEntity that = (PropertyOverrideEntity) o;
if (str2 != null ? !str2.equals(that.str2) : that.str2 != null) return false; if (str2 != null ? !str2.equals(that.str2) : that.str2 != null) return false;
@ -51,7 +51,7 @@ public class PropertyOverrideTestEntity extends BaseEntity {
@Override @Override
public String toString() { public String toString() {
return "PropertyOverrideTestEntity(" + super.toString() + ", str2 = " + str2 + ")"; return "PropertyOverrideEntity(" + super.toString() + ", str2 = " + str2 + ")";
} }
public String getStr2() { public String getStr2() {

View File

@ -11,20 +11,20 @@ import javax.persistence.Entity;
*/ */
@Entity @Entity
@Audited @Audited
@AuditOverrides({@AuditOverride(relatedClass = BaseEntity.class, name = "str1", isAudited = true), @AuditOverrides({@AuditOverride(forClass = BaseEntity.class, name = "str1", isAudited = true),
@AuditOverride(relatedClass = ExtendedBaseEntity.class, name = "number2", isAudited = true)}) @AuditOverride(forClass = ExtendedBaseEntity.class, name = "number2", isAudited = true)})
public class TransitiveOverrideTestEntity extends ExtendedBaseEntity { public class TransitiveOverrideEntity extends ExtendedBaseEntity {
private String str3; private String str3;
public TransitiveOverrideTestEntity() { public TransitiveOverrideEntity() {
} }
public TransitiveOverrideTestEntity(String str1, Integer number1, Integer id, String str2, Integer number2, String str3) { public TransitiveOverrideEntity(String str1, Integer number1, Integer id, String str2, Integer number2, String str3) {
super(str1, number1, id, str2, number2); super(str1, number1, id, str2, number2);
this.str3 = str3; this.str3 = str3;
} }
public TransitiveOverrideTestEntity(String str1, Integer number1, String str2, Integer number2, String str3) { public TransitiveOverrideEntity(String str1, Integer number1, String str2, Integer number2, String str3) {
super(str1, number1, str2, number2); super(str1, number1, str2, number2);
this.str3 = str3; this.str3 = str3;
} }
@ -32,10 +32,10 @@ public class TransitiveOverrideTestEntity extends ExtendedBaseEntity {
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) return true; if (this == o) return true;
if (!(o instanceof TransitiveOverrideTestEntity)) return false; if (!(o instanceof TransitiveOverrideEntity)) return false;
if (!super.equals(o)) return false; if (!super.equals(o)) return false;
TransitiveOverrideTestEntity that = (TransitiveOverrideTestEntity) o; TransitiveOverrideEntity that = (TransitiveOverrideEntity) o;
if (str3 != null ? !str3.equals(that.str3) : that.str3 != null) return false; if (str3 != null ? !str3.equals(that.str3) : that.str3 != null) return false;
@ -51,7 +51,7 @@ public class TransitiveOverrideTestEntity extends ExtendedBaseEntity {
@Override @Override
public String toString() { public String toString() {
return "TransitiveOverrideTestEntity(" + super.toString() + ", str3 = " + str3 + ")"; return "TransitiveOverrideEntity(" + super.toString() + ", str3 = " + str3 + ")";
} }
public String getStr3() { public String getStr3() {