HHH-5580 - Naming
This commit is contained in:
parent
dd55a99b00
commit
1878b23850
|
@ -499,7 +499,7 @@ public class ExtendedRevisionEntity extends DefaultTrackingModifiedTypesRevision
|
|||
</listitem>
|
||||
<listitem>
|
||||
Mark an appropriate field of a custom revision entity with
|
||||
<interfacename>@org.hibernate.envers.ModifiedEntityNames</interfacename> annotation. The property is
|
||||
<interfacename>@org.hibernate.envers.ModifiedEntityTypes</interfacename> annotation. The property is
|
||||
required to be of <literal><![CDATA[Set<String>]]></literal> type.
|
||||
<programlisting>
|
||||
<![CDATA[@Entity
|
||||
|
@ -509,9 +509,9 @@ public class AnnotatedTrackingRevisionEntity {
|
|||
|
||||
@ElementCollection
|
||||
@JoinTable(name = "REVENTITY", joinColumns = @JoinColumn(name = "REV"))
|
||||
@Column(name = "ENTITYNAME")
|
||||
@ModifiedEntityNames
|
||||
private Set<String> modifiedEntityNames;
|
||||
@Column(name = "ENTITYTYPE")
|
||||
@ModifiedEntityTypes
|
||||
private Set<String> modifiedEntityTypes;
|
||||
|
||||
...
|
||||
}]]></programlisting>
|
||||
|
@ -539,7 +539,7 @@ public class CustomEntityTrackingRevisionListener implements EntityTrackingRevis
|
|||
@Override
|
||||
public void entityChanged(Class entityClass, String entityName, Serializable entityId, RevisionType revisionType,
|
||||
Object revisionEntity) {
|
||||
((CustomTrackingRevisionEntity)revisionEntity).addModifiedEntityName(entityClass.getName());
|
||||
((CustomTrackingRevisionEntity)revisionEntity).addModifiedEntityType(entityClass.getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -561,24 +561,20 @@ public class CustomTrackingRevisionEntity {
|
|||
private long customTimestamp;
|
||||
|
||||
@OneToMany(mappedBy="revision", cascade={CascadeType.PERSIST, CascadeType.REMOVE})
|
||||
private Set<ModifiedEntityNameEntity> modifiedEntityNames = new HashSet<ModifiedEntityNameEntity>();
|
||||
private Set<ModifiedEntityTypeEntity> modifiedEntityTypes = new HashSet<ModifiedEntityTypeEntity>();
|
||||
|
||||
public void addModifiedEntityName(String entityName) {
|
||||
modifiedEntityNames.add(new ModifiedEntityNameEntity(this, entityName));
|
||||
}
|
||||
|
||||
public void removeModifiedEntityName(String entityName) {
|
||||
modifiedEntityNames.remove(new ModifiedEntityNameEntity(this, entityName));
|
||||
public void addModifiedEntityType(String entityClassName) {
|
||||
modifiedEntityTypes.add(new ModifiedEntityTypeEntity(this, entityClassName));
|
||||
}
|
||||
|
||||
...
|
||||
}
|
||||
]]></programlisting>
|
||||
<programlisting>
|
||||
<filename>ModifiedEntityNameEntity.java</filename>
|
||||
<filename>ModifiedEntityTypeEntity.java</filename>
|
||||
<![CDATA[
|
||||
@Entity
|
||||
public class ModifiedEntityNameEntity {
|
||||
public class ModifiedEntityTypeEntity {
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Integer id;
|
||||
|
@ -586,14 +582,14 @@ public class ModifiedEntityNameEntity {
|
|||
@ManyToOne
|
||||
private CustomTrackingRevisionEntity revision;
|
||||
|
||||
private String entityName;
|
||||
private String entityClassName;
|
||||
|
||||
...
|
||||
}
|
||||
]]></programlisting>
|
||||
<programlisting><![CDATA[CustomTrackingRevisionEntity revEntity =
|
||||
getAuditReader().findRevision(CustomTrackingRevisionEntity.class, revisionNumber);
|
||||
List<String> modifiedEntityNames = revEntity.getModifiedEntityNames()]]></programlisting>
|
||||
Set<ModifiedEntityTypeEntity> modifiedEntityTypes = revEntity.getModifiedEntityTypes()]]></programlisting>
|
||||
</example>
|
||||
</section>
|
||||
|
||||
|
@ -812,7 +808,7 @@ query.add(AuditEntity.relatedId("address").eq(relatedEntityId));]]></programlist
|
|||
is a number of different entity classes modified within specified revision.
|
||||
</listitem>
|
||||
<listitem>
|
||||
<firstterm><methodname><![CDATA[Map<RevisionType, List>]]> findEntitiesChangedInRevisionGroupByRevisionType(Number)</methodname></firstterm>
|
||||
<firstterm><methodname><![CDATA[Map<RevisionType, List<Object>>]]> findEntitiesChangedInRevisionGroupByRevisionType(Number)</methodname></firstterm>
|
||||
- Returns a map containing lists of entity snapshots grouped by modification operation (e.g.
|
||||
addition, update and removal). Executes <literal>3n+1</literal> SQL queries, where <literal>n</literal>
|
||||
is a number of different entity classes modified within specified revision.
|
||||
|
|
|
@ -223,7 +223,7 @@ public interface AuditReader {
|
|||
* <li>Custom revision entity (annotated with {@link RevisionEntity})
|
||||
* extends {@link DefaultTrackingModifiedTypesRevisionEntity} base class.</li>
|
||||
* <li>Custom revision entity (annotated with {@link RevisionEntity}) encapsulates a field
|
||||
* marked with {@link ModifiedEntityNames} interface.</li>
|
||||
* marked with {@link ModifiedEntityTypes} interface.</li>
|
||||
* </ul>
|
||||
*/
|
||||
List<Object> findEntitiesChangedInRevision(Number revision)
|
||||
|
@ -244,7 +244,7 @@ public interface AuditReader {
|
|||
* <li>Custom revision entity (annotated with {@link RevisionEntity})
|
||||
* extends {@link DefaultTrackingModifiedTypesRevisionEntity} base class.</li>
|
||||
* <li>Custom revision entity (annotated with {@link RevisionEntity}) encapsulates a field
|
||||
* marked with {@link ModifiedEntityNames} interface.</li>
|
||||
* marked with {@link ModifiedEntityTypes} interface.</li>
|
||||
* </ul>
|
||||
*/
|
||||
List<Object> findEntitiesChangedInRevision(Number revision, RevisionType revisionType)
|
||||
|
@ -268,7 +268,7 @@ public interface AuditReader {
|
|||
* <li>Custom revision entity (annotated with {@link RevisionEntity})
|
||||
* extends {@link DefaultTrackingModifiedTypesRevisionEntity} base class.</li>
|
||||
* <li>Custom revision entity (annotated with {@link RevisionEntity}) encapsulates a field
|
||||
* marked with {@link ModifiedEntityNames} interface.</li>
|
||||
* marked with {@link ModifiedEntityTypes} interface.</li>
|
||||
* </ul>
|
||||
*/
|
||||
Map<RevisionType, List<Object>> findEntitiesChangedInRevisionGroupByRevisionType(Number revision)
|
||||
|
@ -287,7 +287,7 @@ public interface AuditReader {
|
|||
* <li>Custom revision entity (annotated with {@link RevisionEntity})
|
||||
* extends {@link DefaultTrackingModifiedTypesRevisionEntity} base class.</li>
|
||||
* <li>Custom revision entity (annotated with {@link RevisionEntity}) encapsulates a field
|
||||
* marked with {@link ModifiedEntityNames} interface.</li>
|
||||
* marked with {@link ModifiedEntityTypes} interface.</li>
|
||||
* </ul>
|
||||
*/
|
||||
Set<Class> findEntityTypesChangedInRevision(Number revision)
|
||||
|
|
|
@ -13,7 +13,7 @@ import java.util.Set;
|
|||
* <ul>
|
||||
* <li><code>org.hibernate.envers.track_entities_changed_in_revision</code> parameter is set to <code>true</code>.</li>
|
||||
* <li>Custom revision entity (annotated with {@link RevisionEntity}) extends {@link DefaultTrackingModifiedTypesRevisionEntity}.</li>
|
||||
* <li>Custom revision entity (annotated with {@link RevisionEntity}) encapsulates a field marked with {@link ModifiedEntityNames}.</li>
|
||||
* <li>Custom revision entity (annotated with {@link RevisionEntity}) encapsulates a field marked with {@link ModifiedEntityTypes}.</li>
|
||||
* </ul>
|
||||
* @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
|
||||
*/
|
||||
|
@ -21,17 +21,17 @@ import java.util.Set;
|
|||
public class DefaultTrackingModifiedTypesRevisionEntity extends DefaultRevisionEntity {
|
||||
@ElementCollection(fetch = FetchType.EAGER)
|
||||
@JoinTable(name = "REVENTITY", joinColumns = @JoinColumn(name = "REV"))
|
||||
@Column(name = "ENTITYNAME")
|
||||
@Column(name = "ENTITYTYPE")
|
||||
@Fetch(FetchMode.JOIN)
|
||||
@ModifiedEntityNames
|
||||
private Set<String> modifiedEntityNames = new HashSet<String>();
|
||||
@ModifiedEntityTypes
|
||||
private Set<String> modifiedEntityTypes = new HashSet<String>();
|
||||
|
||||
public Set<String> getModifiedEntityNames() {
|
||||
return modifiedEntityNames;
|
||||
public Set<String> getModifiedEntityTypes() {
|
||||
return modifiedEntityTypes;
|
||||
}
|
||||
|
||||
public void setModifiedEntityNames(Set<String> modifiedEntityNames) {
|
||||
this.modifiedEntityNames = modifiedEntityNames;
|
||||
public void setModifiedEntityTypes(Set<String> modifiedEntityTypes) {
|
||||
this.modifiedEntityTypes = modifiedEntityTypes;
|
||||
}
|
||||
|
||||
public boolean equals(Object o) {
|
||||
|
@ -41,19 +41,19 @@ public class DefaultTrackingModifiedTypesRevisionEntity extends DefaultRevisionE
|
|||
|
||||
DefaultTrackingModifiedTypesRevisionEntity that = (DefaultTrackingModifiedTypesRevisionEntity) o;
|
||||
|
||||
if (modifiedEntityNames != null ? !modifiedEntityNames.equals(that.modifiedEntityNames)
|
||||
: that.modifiedEntityNames != null) return false;
|
||||
if (modifiedEntityTypes != null ? !modifiedEntityTypes.equals(that.modifiedEntityTypes)
|
||||
: that.modifiedEntityTypes != null) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
int result = super.hashCode();
|
||||
result = 31 * result + (modifiedEntityNames != null ? modifiedEntityNames.hashCode() : 0);
|
||||
result = 31 * result + (modifiedEntityTypes != null ? modifiedEntityTypes.hashCode() : 0);
|
||||
return result;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return "DefaultTrackingModifiedTypesRevisionEntity(" + super.toString() + ", modifiedEntityNames = " + modifiedEntityNames.toString() + ")";
|
||||
return "DefaultTrackingModifiedTypesRevisionEntity(" + super.toString() + ", modifiedEntityTypes = " + modifiedEntityTypes.toString() + ")";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,5 +12,5 @@ import java.lang.annotation.Target;
|
|||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.METHOD, ElementType.FIELD})
|
||||
public @interface ModifiedEntityNames {
|
||||
public @interface ModifiedEntityTypes {
|
||||
}
|
|
@ -52,7 +52,7 @@ public class RevisionInfoConfiguration {
|
|||
private String revisionInfoEntityName;
|
||||
private PropertyData revisionInfoIdData;
|
||||
private PropertyData revisionInfoTimestampData;
|
||||
private PropertyData modifiedEntityNamesData;
|
||||
private PropertyData modifiedEntityTypesData;
|
||||
private Type revisionInfoTimestampType;
|
||||
private GlobalConfiguration globalCfg;
|
||||
|
||||
|
@ -64,7 +64,7 @@ public class RevisionInfoConfiguration {
|
|||
revisionInfoEntityName = "org.hibernate.envers.DefaultRevisionEntity";
|
||||
revisionInfoIdData = new PropertyData("id", "id", "field", null);
|
||||
revisionInfoTimestampData = new PropertyData("timestamp", "timestamp", "field", null);
|
||||
modifiedEntityNamesData = new PropertyData("modifiedEntityNames", "modifiedEntityNames", "field", null);
|
||||
modifiedEntityTypesData = new PropertyData("modifiedEntityTypes", "modifiedEntityTypes", "field", null);
|
||||
revisionInfoTimestampType = new LongType();
|
||||
|
||||
revisionPropType = "integer";
|
||||
|
@ -87,7 +87,7 @@ public class RevisionInfoConfiguration {
|
|||
MetadataTools.addColumn(timestampProperty, "REVTSTMP", null, 0, 0, null, null, null, false);
|
||||
|
||||
if (globalCfg.isTrackEntitiesChangedInRevisionEnabled()) {
|
||||
generateEntityNamesTrackingTableMapping(class_mapping, "modifiedEntityNames", "REVENTITY", "REV", "ENTITYNAME", "string");
|
||||
generateEntityNamesTrackingTableMapping(class_mapping, "modifiedEntityTypes", "REVENTITY", "REV", "ENTITYTYPE", "string");
|
||||
}
|
||||
|
||||
return document;
|
||||
|
@ -137,11 +137,11 @@ public class RevisionInfoConfiguration {
|
|||
|
||||
private void searchForRevisionInfoCfgInProperties(XClass clazz, ReflectionManager reflectionManager,
|
||||
MutableBoolean revisionNumberFound, MutableBoolean revisionTimestampFound,
|
||||
MutableBoolean modifiedEntityNamesFound, String accessType) {
|
||||
MutableBoolean modifiedEntityTypesFound, String accessType) {
|
||||
for (XProperty property : clazz.getDeclaredProperties(accessType)) {
|
||||
RevisionNumber revisionNumber = property.getAnnotation(RevisionNumber.class);
|
||||
RevisionTimestamp revisionTimestamp = property.getAnnotation(RevisionTimestamp.class);
|
||||
ModifiedEntityNames modifiedEntityNames = property.getAnnotation(ModifiedEntityNames.class);
|
||||
ModifiedEntityTypes modifiedEntityTypes = property.getAnnotation(ModifiedEntityTypes.class);
|
||||
|
||||
if (revisionNumber != null) {
|
||||
if (revisionNumberFound.isSet()) {
|
||||
|
@ -192,17 +192,17 @@ public class RevisionInfoConfiguration {
|
|||
}
|
||||
}
|
||||
|
||||
if (modifiedEntityNames != null) {
|
||||
if (modifiedEntityNamesFound.isSet()) {
|
||||
throw new MappingException("Only one property may be annotated with @ModifiedEntityNames!");
|
||||
if (modifiedEntityTypes != null) {
|
||||
if (modifiedEntityTypesFound.isSet()) {
|
||||
throw new MappingException("Only one property may be annotated with @ModifiedEntityTypes!");
|
||||
}
|
||||
XClass modifiedEntityNamesClass = property.getType();
|
||||
if (reflectionManager.equals(modifiedEntityNamesClass, Set.class) &&
|
||||
XClass modifiedEntityTypesClass = property.getType();
|
||||
if (reflectionManager.equals(modifiedEntityTypesClass, Set.class) &&
|
||||
reflectionManager.equals(property.getElementClass(), String.class)) {
|
||||
modifiedEntityNamesData = new PropertyData(property.getName(), property.getName(), accessType, null);
|
||||
modifiedEntityNamesFound.set();
|
||||
modifiedEntityTypesData = new PropertyData(property.getName(), property.getName(), accessType, null);
|
||||
modifiedEntityTypesFound.set();
|
||||
} else {
|
||||
throw new MappingException("The field annotated with @ModifiedEntityNames must be of type Set<String>.");
|
||||
throw new MappingException("The field annotated with @ModifiedEntityTypes must be of type Set<String>.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -210,16 +210,16 @@ public class RevisionInfoConfiguration {
|
|||
|
||||
private void searchForRevisionInfoCfg(XClass clazz, ReflectionManager reflectionManager,
|
||||
MutableBoolean revisionNumberFound, MutableBoolean revisionTimestampFound,
|
||||
MutableBoolean modifiedEntityNamesFound) {
|
||||
MutableBoolean modifiedEntityTypesFound) {
|
||||
XClass superclazz = clazz.getSuperclass();
|
||||
if (!"java.lang.Object".equals(superclazz.getName())) {
|
||||
searchForRevisionInfoCfg(superclazz, reflectionManager, revisionNumberFound, revisionTimestampFound, modifiedEntityNamesFound);
|
||||
searchForRevisionInfoCfg(superclazz, reflectionManager, revisionNumberFound, revisionTimestampFound, modifiedEntityTypesFound);
|
||||
}
|
||||
|
||||
searchForRevisionInfoCfgInProperties(clazz, reflectionManager, revisionNumberFound, revisionTimestampFound,
|
||||
modifiedEntityNamesFound, "field");
|
||||
modifiedEntityTypesFound, "field");
|
||||
searchForRevisionInfoCfgInProperties(clazz, reflectionManager, revisionNumberFound, revisionTimestampFound,
|
||||
modifiedEntityNamesFound, "property");
|
||||
modifiedEntityTypesFound, "property");
|
||||
}
|
||||
|
||||
public RevisionInfoConfigurationResult configure(Configuration cfg, ReflectionManager reflectionManager) {
|
||||
|
@ -253,9 +253,9 @@ public class RevisionInfoConfiguration {
|
|||
|
||||
MutableBoolean revisionNumberFound = new MutableBoolean();
|
||||
MutableBoolean revisionTimestampFound = new MutableBoolean();
|
||||
MutableBoolean modifiedEntityNamesFound = new MutableBoolean();
|
||||
MutableBoolean modifiedEntityTypesFound = new MutableBoolean();
|
||||
|
||||
searchForRevisionInfoCfg(clazz, reflectionManager, revisionNumberFound, revisionTimestampFound, modifiedEntityNamesFound);
|
||||
searchForRevisionInfoCfg(clazz, reflectionManager, revisionNumberFound, revisionTimestampFound, modifiedEntityTypesFound);
|
||||
|
||||
if (!revisionNumberFound.isSet()) {
|
||||
throw new MappingException("An entity annotated with @RevisionEntity must have a field annotated " +
|
||||
|
@ -273,12 +273,12 @@ public class RevisionInfoConfiguration {
|
|||
revisionInfoTimestampType = pc.getProperty(revisionInfoTimestampData.getName()).getType();
|
||||
if (globalCfg.isTrackEntitiesChangedInRevisionEnabled() ||
|
||||
DefaultTrackingModifiedTypesRevisionEntity.class.isAssignableFrom(revisionInfoClass) ||
|
||||
modifiedEntityNamesFound.isSet()) {
|
||||
modifiedEntityTypesFound.isSet()) {
|
||||
// If tracking modified entities parameter is enabled, custom revision info entity is a subtype
|
||||
// of DefaultTrackingModifiedTypesRevisionEntity class, or @ModifiedEntityNames annotation is used.
|
||||
// of DefaultTrackingModifiedTypesRevisionEntity class, or @ModifiedEntityTypes annotation is used.
|
||||
revisionInfoGenerator = new DefaultTrackingModifiedTypesRevisionInfoGenerator(revisionInfoEntityName,
|
||||
revisionInfoClass, revisionEntity.value(), revisionInfoTimestampData, isTimestampAsDate(),
|
||||
modifiedEntityNamesData);
|
||||
modifiedEntityTypesData);
|
||||
globalCfg.setTrackEntitiesChangedInRevisionEnabled(true);
|
||||
} else {
|
||||
revisionInfoGenerator = new DefaultRevisionInfoGenerator(revisionInfoEntityName, revisionInfoClass,
|
||||
|
@ -295,7 +295,7 @@ public class RevisionInfoConfiguration {
|
|||
revisionInfoClass = DefaultTrackingModifiedTypesRevisionEntity.class;
|
||||
revisionInfoEntityName = DefaultTrackingModifiedTypesRevisionEntity.class.getName();
|
||||
revisionInfoGenerator = new DefaultTrackingModifiedTypesRevisionInfoGenerator(revisionInfoEntityName, revisionInfoClass,
|
||||
RevisionListener.class, revisionInfoTimestampData, isTimestampAsDate(), modifiedEntityNamesData);
|
||||
RevisionListener.class, revisionInfoTimestampData, isTimestampAsDate(), modifiedEntityTypesData);
|
||||
} else {
|
||||
revisionInfoClass = DefaultRevisionEntity.class;
|
||||
revisionInfoGenerator = new DefaultRevisionInfoGenerator(revisionInfoEntityName, revisionInfoClass,
|
||||
|
|
|
@ -32,7 +32,7 @@ import org.hibernate.HibernateException;
|
|||
import org.hibernate.NonUniqueResultException;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.engine.SessionImplementor;
|
||||
import org.hibernate.envers.ModifiedEntityNames;
|
||||
import org.hibernate.envers.ModifiedEntityTypes;
|
||||
import org.hibernate.envers.RevisionType;
|
||||
import org.hibernate.envers.configuration.AuditConfiguration;
|
||||
import org.hibernate.envers.exception.AuditException;
|
||||
|
@ -283,7 +283,7 @@ public class AuditReaderImpl implements AuditReaderImplementor {
|
|||
checkSession();
|
||||
if (!verCfg.getGlobalCfg().isTrackEntitiesChangedInRevisionEnabled()) {
|
||||
throw new AuditException("This query is designed for Envers default mechanism of tracking entities modified in a given revision."
|
||||
+ " Extend DefaultTrackingModifiedTypesRevisionEntity, utilize @ModifiedEntityNames annotation or set "
|
||||
+ " Extend DefaultTrackingModifiedTypesRevisionEntity, utilize @ModifiedEntityTypes annotation or set "
|
||||
+ "'org.hibernate.envers.track_entities_changed_in_revision' parameter to true.");
|
||||
}
|
||||
Set<Number> revisions = new HashSet<Number>(1);
|
||||
|
@ -292,12 +292,12 @@ public class AuditReaderImpl implements AuditReaderImplementor {
|
|||
Object revisionInfo = query.uniqueResult();
|
||||
if (revisionInfo != null) {
|
||||
// If revision exists
|
||||
// Only one field can be marked with @ModifiedEntityNames annotation
|
||||
Set<String> modifiedEntityNames = (Set<String>) ReflectionTools.getAnnotatedMembersValues(revisionInfo, ModifiedEntityNames.class).values().toArray()[0];
|
||||
Set<Class> result = new HashSet<Class>(modifiedEntityNames.size());
|
||||
for (String entityName : modifiedEntityNames) {
|
||||
// Only one field can be marked with @ModifiedEntityTypes annotation
|
||||
Set<String> modifiedEntityTypes = (Set<String>) ReflectionTools.getAnnotatedMembersValues(revisionInfo, ModifiedEntityTypes.class).values().toArray()[0];
|
||||
Set<Class> result = new HashSet<Class>(modifiedEntityTypes.size());
|
||||
for (String entityClassName : modifiedEntityTypes) {
|
||||
try {
|
||||
result.add(Class.forName(entityName));
|
||||
result.add(Class.forName(entityClassName));
|
||||
} catch (ClassNotFoundException e) {
|
||||
// This shall never happen
|
||||
throw new RuntimeException(e);
|
||||
|
|
|
@ -13,22 +13,22 @@ import java.util.Set;
|
|||
|
||||
/**
|
||||
* Automatically adds entity names changed during current revision.
|
||||
* @see org.hibernate.envers.ModifiedEntityNames
|
||||
* @see org.hibernate.envers.ModifiedEntityTypes
|
||||
* @see org.hibernate.envers.DefaultTrackingModifiedTypesRevisionEntity
|
||||
* @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
|
||||
*/
|
||||
public class DefaultTrackingModifiedTypesRevisionInfoGenerator extends DefaultRevisionInfoGenerator {
|
||||
private final Setter modifiedEntityNamesSetter;
|
||||
private final Getter modifiedEntityNamesGetter;
|
||||
private final Setter modifiedEntityTypesSetter;
|
||||
private final Getter modifiedEntityTypesGetter;
|
||||
|
||||
public DefaultTrackingModifiedTypesRevisionInfoGenerator(String revisionInfoEntityName,
|
||||
Class<?> revisionInfoClass,
|
||||
Class<? extends RevisionListener> listenerClass,
|
||||
PropertyData revisionInfoTimestampData, boolean timestampAsDate,
|
||||
PropertyData modifiedEntityNamesData) {
|
||||
PropertyData modifiedEntityTypesData) {
|
||||
super(revisionInfoEntityName, revisionInfoClass, listenerClass, revisionInfoTimestampData, timestampAsDate);
|
||||
modifiedEntityNamesSetter = ReflectionTools.getSetter(revisionInfoClass, modifiedEntityNamesData);
|
||||
modifiedEntityNamesGetter = ReflectionTools.getGetter(revisionInfoClass, modifiedEntityNamesData);
|
||||
modifiedEntityTypesSetter = ReflectionTools.getSetter(revisionInfoClass, modifiedEntityTypesData);
|
||||
modifiedEntityTypesGetter = ReflectionTools.getGetter(revisionInfoClass, modifiedEntityTypesData);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -36,11 +36,11 @@ public class DefaultTrackingModifiedTypesRevisionInfoGenerator extends DefaultRe
|
|||
public void entityChanged(Class entityClass, String entityName, Serializable entityId, RevisionType revisionType,
|
||||
Object revisionEntity) {
|
||||
super.entityChanged(entityClass, entityName, entityId, revisionType, revisionEntity);
|
||||
Set<String> modifiedEntityNames = (Set<String>) modifiedEntityNamesGetter.get(revisionEntity);
|
||||
if (modifiedEntityNames == null) {
|
||||
modifiedEntityNames = new HashSet<String>();
|
||||
modifiedEntityNamesSetter.set(revisionEntity, modifiedEntityNames, null);
|
||||
Set<String> modifiedEntityTypes = (Set<String>) modifiedEntityTypesGetter.get(revisionEntity);
|
||||
if (modifiedEntityTypes == null) {
|
||||
modifiedEntityTypes = new HashSet<String>();
|
||||
modifiedEntityTypesSetter.set(revisionEntity, modifiedEntityTypes, null);
|
||||
}
|
||||
modifiedEntityNames.add(entityClass.getName());
|
||||
modifiedEntityTypes.add(entityClass.getName());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,16 +1,15 @@
|
|||
package org.hibernate.envers.test.entities.reventity.trackmodifiedentities;
|
||||
|
||||
import org.hibernate.envers.ModifiedEntityNames;
|
||||
import org.hibernate.envers.ModifiedEntityTypes;
|
||||
import org.hibernate.envers.RevisionEntity;
|
||||
import org.hibernate.envers.RevisionNumber;
|
||||
import org.hibernate.envers.RevisionTimestamp;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Sample revision entity that uses {@link ModifiedEntityNames} annotation.
|
||||
* Sample revision entity that uses {@link ModifiedEntityTypes} annotation.
|
||||
* @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
|
||||
*/
|
||||
@Entity
|
||||
|
@ -26,9 +25,9 @@ public class AnnotatedTrackingRevisionEntity {
|
|||
|
||||
@ElementCollection
|
||||
@JoinTable(name = "REVENTITY", joinColumns = @JoinColumn(name = "REV"))
|
||||
@Column(name = "ENTITYNAME")
|
||||
@ModifiedEntityNames
|
||||
private Set<String> entityNames;
|
||||
@Column(name = "ENTITYTYPE")
|
||||
@ModifiedEntityTypes
|
||||
private Set<String> entityTypes;
|
||||
|
||||
public int getCustomId() {
|
||||
return customId;
|
||||
|
@ -46,12 +45,12 @@ public class AnnotatedTrackingRevisionEntity {
|
|||
this.customTimestamp = customTimestamp;
|
||||
}
|
||||
|
||||
public Set<String> getEntityNames() {
|
||||
return entityNames;
|
||||
public Set<String> getEntityTypes() {
|
||||
return entityTypes;
|
||||
}
|
||||
|
||||
public void setEntityNames(Set<String> entityNames) {
|
||||
this.entityNames = entityNames;
|
||||
public void setEntityTypes(Set<String> entityTypes) {
|
||||
this.entityTypes = entityTypes;
|
||||
}
|
||||
|
||||
public boolean equals(Object o) {
|
||||
|
@ -62,6 +61,7 @@ public class AnnotatedTrackingRevisionEntity {
|
|||
|
||||
if (customId != that.customId) return false;
|
||||
if (customTimestamp != that.customTimestamp) return false;
|
||||
if (entityTypes != null ? !entityTypes.equals(that.entityTypes) : that.entityTypes != null) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -69,11 +69,12 @@ public class AnnotatedTrackingRevisionEntity {
|
|||
public int hashCode() {
|
||||
int result = customId;
|
||||
result = 31 * result + (int) (customTimestamp ^ (customTimestamp >>> 32));
|
||||
result = 31 * result + (entityTypes != null ? entityTypes.hashCode() : 0);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "AnnotatedTrackingRevisionEntity(customId = " + customId + ", customTimestamp = " + customTimestamp + ")";
|
||||
return "AnnotatedTrackingRevisionEntity(customId = " + customId + ", customTimestamp = " + customTimestamp + ", entityTypes=" + entityTypes + ")";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,7 +12,7 @@ public class CustomEntityTrackingRevisionListener implements EntityTrackingRevis
|
|||
@Override
|
||||
public void entityChanged(Class entityClass, String entityName, Serializable entityId, RevisionType revisionType,
|
||||
Object revisionEntity) {
|
||||
((CustomTrackingRevisionEntity)revisionEntity).addModifiedEntityName(entityClass.getName());
|
||||
((CustomTrackingRevisionEntity)revisionEntity).addModifiedEntityType(entityClass.getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -9,7 +9,7 @@ import java.util.HashSet;
|
|||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Revision entity which {@code modifiedEntityNames} field is manually populated by {@link CustomEntityTrackingRevisionListener}.
|
||||
* Revision entity which {@code modifiedEntityTypes} field is manually populated by {@link CustomEntityTrackingRevisionListener}.
|
||||
* @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
|
||||
*/
|
||||
@Entity
|
||||
|
@ -24,7 +24,7 @@ public class CustomTrackingRevisionEntity {
|
|||
private long customTimestamp;
|
||||
|
||||
@OneToMany(mappedBy="revision", cascade={CascadeType.PERSIST, CascadeType.REMOVE})
|
||||
private Set<ModifiedEntityNameEntity> modifiedEntityNames = new HashSet<ModifiedEntityNameEntity>();
|
||||
private Set<ModifiedEntityTypeEntity> modifiedEntityTypes = new HashSet<ModifiedEntityTypeEntity>();
|
||||
|
||||
public int getCustomId() {
|
||||
return customId;
|
||||
|
@ -42,20 +42,16 @@ public class CustomTrackingRevisionEntity {
|
|||
this.customTimestamp = customTimestamp;
|
||||
}
|
||||
|
||||
public Set<ModifiedEntityNameEntity> getModifiedEntityNames() {
|
||||
return modifiedEntityNames;
|
||||
public Set<ModifiedEntityTypeEntity> getModifiedEntityTypes() {
|
||||
return modifiedEntityTypes;
|
||||
}
|
||||
|
||||
public void setModifiedEntityNames(Set<ModifiedEntityNameEntity> modifiedEntityNames) {
|
||||
this.modifiedEntityNames = modifiedEntityNames;
|
||||
public void setModifiedEntityTypes(Set<ModifiedEntityTypeEntity> modifiedEntityTypes) {
|
||||
this.modifiedEntityTypes = modifiedEntityTypes;
|
||||
}
|
||||
|
||||
public void addModifiedEntityName(String entityName) {
|
||||
modifiedEntityNames.add(new ModifiedEntityNameEntity(this, entityName));
|
||||
}
|
||||
|
||||
public void removeModifiedEntityName(String entityName) {
|
||||
modifiedEntityNames.remove(new ModifiedEntityNameEntity(this, entityName));
|
||||
public void addModifiedEntityType(String entityClassName) {
|
||||
modifiedEntityTypes.add(new ModifiedEntityTypeEntity(this, entityClassName));
|
||||
}
|
||||
|
||||
public boolean equals(Object o) {
|
||||
|
|
|
@ -1,70 +0,0 @@
|
|||
package org.hibernate.envers.test.entities.reventity.trackmodifiedentities;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
|
||||
/**
|
||||
* Custom detail of revision entity.
|
||||
* @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
|
||||
*/
|
||||
@Entity
|
||||
public class ModifiedEntityNameEntity {
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Integer id;
|
||||
|
||||
@ManyToOne
|
||||
private CustomTrackingRevisionEntity revision;
|
||||
|
||||
private String entityName;
|
||||
|
||||
public ModifiedEntityNameEntity() {
|
||||
}
|
||||
|
||||
public ModifiedEntityNameEntity(String entityName) {
|
||||
this.entityName = entityName;
|
||||
}
|
||||
|
||||
public ModifiedEntityNameEntity(CustomTrackingRevisionEntity revision, String entityName) {
|
||||
this.revision = revision;
|
||||
this.entityName = entityName;
|
||||
}
|
||||
|
||||
public CustomTrackingRevisionEntity getRevision() {
|
||||
return revision;
|
||||
}
|
||||
|
||||
public void setRevision(CustomTrackingRevisionEntity revision) {
|
||||
this.revision = revision;
|
||||
}
|
||||
|
||||
public String getEntityName() {
|
||||
return entityName;
|
||||
}
|
||||
|
||||
public void setEntityName(String entityName) {
|
||||
this.entityName = entityName;
|
||||
}
|
||||
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (!(o instanceof ModifiedEntityNameEntity)) return false;
|
||||
|
||||
ModifiedEntityNameEntity that = (ModifiedEntityNameEntity) o;
|
||||
|
||||
if (entityName != null ? !entityName.equals(that.entityName) : that.entityName != null) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return entityName != null ? entityName.hashCode() : 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "CustomTrackingRevisionEntity(entityName = " + entityName + ")";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,70 @@
|
|||
package org.hibernate.envers.test.entities.reventity.trackmodifiedentities;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
|
||||
/**
|
||||
* Custom detail of revision entity.
|
||||
* @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
|
||||
*/
|
||||
@Entity
|
||||
public class ModifiedEntityTypeEntity {
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Integer id;
|
||||
|
||||
@ManyToOne
|
||||
private CustomTrackingRevisionEntity revision;
|
||||
|
||||
private String entityClassName;
|
||||
|
||||
public ModifiedEntityTypeEntity() {
|
||||
}
|
||||
|
||||
public ModifiedEntityTypeEntity(String entityClassName) {
|
||||
this.entityClassName = entityClassName;
|
||||
}
|
||||
|
||||
public ModifiedEntityTypeEntity(CustomTrackingRevisionEntity revision, String entityClassName) {
|
||||
this.revision = revision;
|
||||
this.entityClassName = entityClassName;
|
||||
}
|
||||
|
||||
public CustomTrackingRevisionEntity getRevision() {
|
||||
return revision;
|
||||
}
|
||||
|
||||
public void setRevision(CustomTrackingRevisionEntity revision) {
|
||||
this.revision = revision;
|
||||
}
|
||||
|
||||
public String getEntityClassName() {
|
||||
return entityClassName;
|
||||
}
|
||||
|
||||
public void setEntityClassName(String entityClassName) {
|
||||
this.entityClassName = entityClassName;
|
||||
}
|
||||
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (!(o instanceof ModifiedEntityTypeEntity)) return false;
|
||||
|
||||
ModifiedEntityTypeEntity that = (ModifiedEntityTypeEntity) o;
|
||||
|
||||
if (entityClassName != null ? !entityClassName.equals(that.entityClassName) : that.entityClassName != null) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return entityClassName != null ? entityClassName.hashCode() : 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "CustomTrackingRevisionEntity(entityClassName = " + entityClassName + ")";
|
||||
}
|
||||
}
|
|
@ -1,11 +1,11 @@
|
|||
package org.hibernate.envers.test.integration.reventity.trackmodifiedentities;
|
||||
|
||||
import org.hibernate.ejb.Ejb3Configuration;
|
||||
import org.hibernate.envers.ModifiedEntityNames;
|
||||
import org.hibernate.envers.ModifiedEntityTypes;
|
||||
import org.hibernate.envers.test.entities.reventity.trackmodifiedentities.AnnotatedTrackingRevisionEntity;
|
||||
|
||||
/**
|
||||
* Tests proper behavior of revision entity that utilizes {@link ModifiedEntityNames} annotation.
|
||||
* Tests proper behavior of revision entity that utilizes {@link ModifiedEntityTypes} annotation.
|
||||
* @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
|
||||
*/
|
||||
public class AnnotatedTrackingEntitiesTest extends DefaultTrackingEntitiesTest {
|
||||
|
|
|
@ -10,7 +10,7 @@ import org.hibernate.envers.test.entities.StrIntTestEntity;
|
|||
import org.hibernate.envers.test.entities.StrTestEntity;
|
||||
import org.hibernate.envers.test.entities.reventity.trackmodifiedentities.CustomEntityTrackingRevisionListener;
|
||||
import org.hibernate.envers.test.entities.reventity.trackmodifiedentities.CustomTrackingRevisionEntity;
|
||||
import org.hibernate.envers.test.entities.reventity.trackmodifiedentities.ModifiedEntityNameEntity;
|
||||
import org.hibernate.envers.test.entities.reventity.trackmodifiedentities.ModifiedEntityTypeEntity;
|
||||
import org.hibernate.envers.test.tools.TestTools;
|
||||
import org.junit.Test;
|
||||
|
||||
|
@ -28,7 +28,7 @@ public class CustomTrackingEntitiesTest extends AbstractEntityTest {
|
|||
|
||||
@Override
|
||||
public void configure(Ejb3Configuration cfg) {
|
||||
cfg.addAnnotatedClass(ModifiedEntityNameEntity.class);
|
||||
cfg.addAnnotatedClass(ModifiedEntityTypeEntity.class);
|
||||
cfg.addAnnotatedClass(CustomTrackingRevisionEntity.class);
|
||||
cfg.addAnnotatedClass(StrTestEntity.class);
|
||||
cfg.addAnnotatedClass(StrIntTestEntity.class);
|
||||
|
@ -66,40 +66,40 @@ public class CustomTrackingEntitiesTest extends AbstractEntityTest {
|
|||
|
||||
@Test
|
||||
public void testTrackAddedEntities() {
|
||||
ModifiedEntityNameEntity steDescriptor = new ModifiedEntityNameEntity(StrTestEntity.class.getName());
|
||||
ModifiedEntityNameEntity siteDescriptor = new ModifiedEntityNameEntity(StrIntTestEntity.class.getName());
|
||||
ModifiedEntityTypeEntity steDescriptor = new ModifiedEntityTypeEntity(StrTestEntity.class.getName());
|
||||
ModifiedEntityTypeEntity siteDescriptor = new ModifiedEntityTypeEntity(StrIntTestEntity.class.getName());
|
||||
|
||||
AuditReader vr = getAuditReader();
|
||||
CustomTrackingRevisionEntity ctre = vr.findRevision(CustomTrackingRevisionEntity.class, 1);
|
||||
|
||||
assert ctre.getModifiedEntityNames() != null;
|
||||
assert ctre.getModifiedEntityNames().size() == 2;
|
||||
assert TestTools.makeSet(steDescriptor, siteDescriptor).equals(ctre.getModifiedEntityNames());
|
||||
assert ctre.getModifiedEntityTypes() != null;
|
||||
assert ctre.getModifiedEntityTypes().size() == 2;
|
||||
assert TestTools.makeSet(steDescriptor, siteDescriptor).equals(ctre.getModifiedEntityTypes());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTrackModifiedEntities() {
|
||||
ModifiedEntityNameEntity siteDescriptor = new ModifiedEntityNameEntity(StrIntTestEntity.class.getName());
|
||||
ModifiedEntityTypeEntity siteDescriptor = new ModifiedEntityTypeEntity(StrIntTestEntity.class.getName());
|
||||
|
||||
AuditReader vr = getAuditReader();
|
||||
CustomTrackingRevisionEntity ctre = vr.findRevision(CustomTrackingRevisionEntity.class, 2);
|
||||
|
||||
assert ctre.getModifiedEntityNames() != null;
|
||||
assert ctre.getModifiedEntityNames().size() == 1;
|
||||
assert TestTools.makeSet(siteDescriptor).equals(ctre.getModifiedEntityNames());
|
||||
assert ctre.getModifiedEntityTypes() != null;
|
||||
assert ctre.getModifiedEntityTypes().size() == 1;
|
||||
assert TestTools.makeSet(siteDescriptor).equals(ctre.getModifiedEntityTypes());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTrackDeletedEntities() {
|
||||
ModifiedEntityNameEntity steDescriptor = new ModifiedEntityNameEntity(StrTestEntity.class.getName());
|
||||
ModifiedEntityNameEntity siteDescriptor = new ModifiedEntityNameEntity(StrIntTestEntity.class.getName());
|
||||
ModifiedEntityTypeEntity steDescriptor = new ModifiedEntityTypeEntity(StrTestEntity.class.getName());
|
||||
ModifiedEntityTypeEntity siteDescriptor = new ModifiedEntityTypeEntity(StrIntTestEntity.class.getName());
|
||||
|
||||
AuditReader vr = getAuditReader();
|
||||
CustomTrackingRevisionEntity ctre = vr.findRevision(CustomTrackingRevisionEntity.class, 3);
|
||||
|
||||
assert ctre.getModifiedEntityNames() != null;
|
||||
assert ctre.getModifiedEntityNames().size() == 2;
|
||||
assert TestTools.makeSet(steDescriptor, siteDescriptor).equals(ctre.getModifiedEntityNames());
|
||||
assert ctre.getModifiedEntityTypes() != null;
|
||||
assert ctre.getModifiedEntityTypes().size() == 2;
|
||||
assert TestTools.makeSet(steDescriptor, siteDescriptor).equals(ctre.getModifiedEntityTypes());
|
||||
}
|
||||
|
||||
@Test(expected = AuditException.class)
|
||||
|
|
|
@ -71,7 +71,7 @@ public class DefaultTrackingEntitiesTest extends AbstractEntityTest {
|
|||
if ("REVENTITY".equals(table.getName())) {
|
||||
assert table.getColumnSpan() == 2;
|
||||
assert table.getColumn(new Column("REV")) != null;
|
||||
assert table.getColumn(new Column("ENTITYNAME")) != null;
|
||||
assert table.getColumn(new Column("ENTITYTYPE")) != null;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue