Resolving HHH-5588: Improve support for entityNames in envers.
Adding methods for knowing if an entityName is marked to be resolved by core (@Audited(targetAuditMode='NOT_AUDITED') isEntityNameNotAudited() Adding method to resolve the entityName for an entity retrieved previously by envers. getEntityName()
This commit is contained in:
parent
dc91727742
commit
328c098ab9
|
@ -28,13 +28,14 @@ import java.util.List;
|
|||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.hibernate.envers.exception.EnversException;
|
||||
import org.hibernate.envers.exception.NotAuditedException;
|
||||
import org.hibernate.envers.exception.RevisionDoesNotExistException;
|
||||
import org.hibernate.envers.query.AuditQueryCreator;
|
||||
|
||||
/**
|
||||
* @author Adam Warski (adam at warski dot org)
|
||||
* @author Hern<EFBFBD>n Chanfreau
|
||||
* @author Hernán Chanfreau
|
||||
*/
|
||||
public interface AuditReader {
|
||||
/**
|
||||
|
@ -177,10 +178,58 @@ public interface AuditReader {
|
|||
AuditQueryCreator createQuery();
|
||||
|
||||
/**
|
||||
* Checks if the entityClass was configured to be audited.
|
||||
* Checks if the entityClass was configured to be audited. Calling
|
||||
* isEntityNameAudited() with the string of the class name will return the
|
||||
* same value.
|
||||
*
|
||||
* @param entityClass Class of the entity asking for audit support
|
||||
* @param entityClass
|
||||
* Class of the entity asking for audit support
|
||||
* @return true if the entityClass is audited.
|
||||
*/
|
||||
boolean isEntityClassAudited(Class<?> entityClass);
|
||||
|
||||
/**
|
||||
* Checks if the entityName was configured to be audited.
|
||||
*
|
||||
* @param entityClass
|
||||
* EntityName of the entity asking for audit support.
|
||||
* @return true if the entityName is audited.
|
||||
*/
|
||||
boolean isEntityNameAudited(String entityName);
|
||||
|
||||
/**
|
||||
* Checks if the entityClass was configured to be a audited in a relation
|
||||
* with the targetAuditMode as NOT_AUDITED . Calling
|
||||
* isEntityNameNotAudited() with the string of the class name will return
|
||||
* the same value.
|
||||
*
|
||||
* @param entityClass
|
||||
* Class of the entity asking for
|
||||
* @Audit(targetAuditMode=...NOT_AUDITED) support
|
||||
* @return true if the entityClass is marked in a relation as NOT_AUDITED.
|
||||
*/
|
||||
boolean isEntityClassNotAudited(Class<?> entityClass);
|
||||
|
||||
/**
|
||||
* Checks if the entityName was configured be a audited in a relation
|
||||
* with the targetAuditMode as NOT_AUDITED .
|
||||
*
|
||||
* @param entityClass
|
||||
* Class of the entity asking for
|
||||
* @Audit(targetAuditMode=...NOT_AUDITED) support
|
||||
* @return true if the entityClass is marked in a relation as NOT_AUDITED.
|
||||
*/
|
||||
boolean isEntityNameNotAudited(String entityName);
|
||||
|
||||
/**
|
||||
*
|
||||
* @param entity
|
||||
* that was obtained previously from the same AuditReader.
|
||||
*
|
||||
* @return the entityName for the given entity, null in case the entity is
|
||||
* not associated with this AuditReader instance.
|
||||
*/
|
||||
String getEntityName(Object primaryKey, Number revision, Object entity)
|
||||
throws EnversException;
|
||||
|
||||
}
|
||||
|
|
|
@ -42,16 +42,18 @@ import org.hibernate.Session;
|
|||
import org.hibernate.engine.SessionImplementor;
|
||||
import org.hibernate.envers.configuration.AuditConfiguration;
|
||||
import org.hibernate.envers.exception.AuditException;
|
||||
import org.hibernate.envers.exception.EnversException;
|
||||
import org.hibernate.envers.exception.NotAuditedException;
|
||||
import org.hibernate.envers.exception.RevisionDoesNotExistException;
|
||||
import org.hibernate.envers.query.AuditEntity;
|
||||
import org.hibernate.envers.query.AuditQueryCreator;
|
||||
import org.hibernate.envers.synchronization.AuditProcess;
|
||||
import org.hibernate.event.EventSource;
|
||||
import org.hibernate.proxy.HibernateProxy;
|
||||
|
||||
/**
|
||||
* @author Adam Warski (adam at warski dot org)
|
||||
* @author Hern<EFBFBD>n Chanfreau
|
||||
* @author Hernán Chanfreau
|
||||
*/
|
||||
public class AuditReaderImpl implements AuditReaderImplementor {
|
||||
private final AuditConfiguration verCfg;
|
||||
|
@ -256,10 +258,44 @@ public class AuditReaderImpl implements AuditReaderImplementor {
|
|||
}
|
||||
|
||||
public boolean isEntityClassAudited(Class<?> entityClass) {
|
||||
checkNotNull(entityClass, "Entity class");
|
||||
checkSession();
|
||||
return this.isEntityNameAudited(entityClass.getName());
|
||||
}
|
||||
|
||||
String entityName = entityClass.getName();
|
||||
|
||||
public boolean isEntityNameAudited(String entityName) {
|
||||
checkNotNull(entityName, "Entity name");
|
||||
checkSession();
|
||||
return (verCfg.getEntCfg().isVersioned(entityName));
|
||||
}
|
||||
|
||||
public boolean isEntityClassNotAudited(Class<?> entityClass) {
|
||||
return this.isEntityNameNotAudited(entityClass.getName());
|
||||
}
|
||||
|
||||
public boolean isEntityNameNotAudited(String entityName) {
|
||||
checkNotNull(entityName, "Entity name");
|
||||
checkSession();
|
||||
return (verCfg.getEntCfg().isNotAudited(entityName));
|
||||
}
|
||||
|
||||
|
||||
public String getEntityName(Object primaryKey, Number revision ,Object entity) throws EnversException{
|
||||
checkNotNull(primaryKey, "Primary key");
|
||||
checkNotNull(revision, "Entity revision");
|
||||
checkPositive(revision, "Entity revision");
|
||||
checkNotNull(entity, "Entity");
|
||||
checkSession();
|
||||
|
||||
// Unwrap if necessary
|
||||
if(entity instanceof HibernateProxy) {
|
||||
entity = ((HibernateProxy)entity).getHibernateLazyInitializer().getImplementation();
|
||||
}
|
||||
if(firstLevelCache.containsEntityName(primaryKey, revision, entity)) {
|
||||
// it´s on envers FLC!
|
||||
return firstLevelCache.getFromEntityNameCache(primaryKey, revision, entity);
|
||||
} else {
|
||||
throw new EnversException(
|
||||
"Can´t resolve entityName for historic entity. The id, revision and entity is not on envers first level cache.");
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue