Class AuditReaderImpl
- java.lang.Object
-
- org.hibernate.envers.internal.reader.AuditReaderImpl
-
- All Implemented Interfaces:
AuditReader
,AuditReaderImplementor
public class AuditReaderImpl extends Object implements AuditReaderImplementor
-
-
Constructor Summary
Constructors Constructor Description AuditReaderImpl(EnversService enversService, Session session, SessionImplementor sessionImplementor)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description AuditQueryCreator
createQuery()
Creates an audit query<T> T
find(Class<T> cls, Object primaryKey, Number revision)
Find an entity by primary key at the given revision.<T> T
find(Class<T> cls, Object primaryKey, LocalDateTime datetime)
Find an entity by primary key on the given datetime.<T> T
find(Class<T> cls, Object primaryKey, Date date)
Find an entity by primary key on the given date.<T> T
find(Class<T> cls, String entityName, Object primaryKey, Number revision)
Find an entity by primary key at the given revision with the specified entityName.<T> T
find(Class<T> cls, String entityName, Object primaryKey, Number revision, boolean includeDeletions)
Find an entity by primary key at the given revision with the specified entityName, possibly including deleted entities in the search.<T> T
findRevision(Class<T> revisionEntityClass, Number revision)
A helper method; should be used only if a custom revision entity is used.<T> Map<Number,T>
findRevisions(Class<T> revisionEntityClass, Set<Number> revisions)
Find a map of revisions using the revision numbers specified.CrossTypeRevisionChangesReader
getCrossTypeRevisionChangesReader()
<T> T
getCurrentRevision(Class<T> revisionEntityClass, boolean persist)
Gets an instance of the current revision entity, to which any entries in the audit tables will be bound.String
getEntityName(Object primaryKey, Number revision, Object entity)
Get the entity name of an instance of an entity returned by this AuditReader.FirstLevelCache
getFirstLevelCache()
Date
getRevisionDate(Number revision)
Get the date, at which a revision was created.Number
getRevisionNumberForDate(Instant date)
Gets the revision number, that corresponds to the given date.Number
getRevisionNumberForDate(LocalDateTime date)
Gets the revision number, that corresponds to the given date.Number
getRevisionNumberForDate(Date date)
Gets the revision number, that corresponds to the given date.List<Number>
getRevisions(Class<?> cls, Object primaryKey)
Get a list of revision numbers, at which an entity was modified.List<Number>
getRevisions(Class<?> cls, String entityName, Object primaryKey)
Get a list of revision numbers, at which an entity was modified, looking by entityName.Session
getSession()
SessionImplementor
getSessionImplementor()
boolean
isEntityClassAudited(Class<?> entityClass)
Checks if the entityClass was configured to be audited.boolean
isEntityNameAudited(String entityName)
Checks if the entityName was configured to be audited.
-
-
-
Constructor Detail
-
AuditReaderImpl
public AuditReaderImpl(EnversService enversService, Session session, SessionImplementor sessionImplementor)
-
-
Method Detail
-
getSessionImplementor
public SessionImplementor getSessionImplementor()
- Specified by:
getSessionImplementor
in interfaceAuditReaderImplementor
-
getSession
public Session getSession()
- Specified by:
getSession
in interfaceAuditReaderImplementor
-
getFirstLevelCache
public FirstLevelCache getFirstLevelCache()
- Specified by:
getFirstLevelCache
in interfaceAuditReaderImplementor
-
find
public <T> T find(Class<T> cls, Object primaryKey, Number revision) throws IllegalArgumentException, NotAuditedException, IllegalStateException
Description copied from interface:AuditReader
Find an entity by primary key at the given revision.- Specified by:
find
in interfaceAuditReader
- Type Parameters:
T
- The type of the entity to find- Parameters:
cls
- Class of the entity.primaryKey
- Primary key of the entity.revision
- Revision in which to get the entity.- Returns:
- The found entity instance at the given revision (its properties may be partially filled if not all properties are audited) or null, if an entity with that id didn't exist at that revision.
- Throws:
IllegalArgumentException
- If cls or primaryKey is null or revision is less or equal to 0.NotAuditedException
- When entities of the given class are not audited.IllegalStateException
- If the associated entity manager is closed.
-
find
public <T> T find(Class<T> cls, String entityName, Object primaryKey, Number revision) throws IllegalArgumentException, NotAuditedException, IllegalStateException
Description copied from interface:AuditReader
Find an entity by primary key at the given revision with the specified entityName.- Specified by:
find
in interfaceAuditReader
- Type Parameters:
T
- The type of the entity to find- Parameters:
cls
- Class of the entity.entityName
- Name of the entity (if it can't be guessed basing on thecls
).primaryKey
- Primary key of the entity.revision
- Revision in which to get the entity.- Returns:
- The found entity instance at the given revision (its properties may be partially filled if not all properties are audited) or null, if an entity with that id didn't exist at that revision.
- Throws:
IllegalArgumentException
- If cls or primaryKey is null or revision is less or equal to 0.NotAuditedException
- When entities of the given class are not audited.IllegalStateException
- If the associated entity manager is closed.
-
find
public <T> T find(Class<T> cls, String entityName, Object primaryKey, Number revision, boolean includeDeletions) throws IllegalArgumentException, NotAuditedException, IllegalStateException
Description copied from interface:AuditReader
Find an entity by primary key at the given revision with the specified entityName, possibly including deleted entities in the search.- Specified by:
find
in interfaceAuditReader
- Type Parameters:
T
- The type of the entity to find- Parameters:
cls
- Class of the entity.entityName
- Name of the entity (if it can't be guessed basing on thecls
).primaryKey
- Primary key of the entity.revision
- Revision in which to get the entity.includeDeletions
- Whether to include deleted entities in the search.- Returns:
- The found entity instance at the given revision (its properties may be partially filled if not all properties are audited) or null, if an entity with that id didn't exist at that revision.
- Throws:
IllegalArgumentException
- If cls or primaryKey is null or revision is less or equal to 0.NotAuditedException
- When entities of the given class are not audited.IllegalStateException
- If the associated entity manager is closed.
-
getRevisions
public List<Number> getRevisions(Class<?> cls, Object primaryKey) throws IllegalArgumentException, NotAuditedException, IllegalStateException
Description copied from interface:AuditReader
Get a list of revision numbers, at which an entity was modified.- Specified by:
getRevisions
in interfaceAuditReader
- Parameters:
cls
- Class of the entity.primaryKey
- Primary key of the entity.- Returns:
- A list of revision numbers, at which the entity was modified, sorted in ascending order (so older revisions come first).
- Throws:
IllegalArgumentException
- If cls or primaryKey is null.NotAuditedException
- When entities of the given class are not audited.IllegalStateException
- If the associated entity manager is closed.
-
find
public <T> T find(Class<T> cls, Object primaryKey, Date date) throws IllegalArgumentException, NotAuditedException, RevisionDoesNotExistException, IllegalStateException
Description copied from interface:AuditReader
Find an entity by primary key on the given date. The date specifies restricting the result to any entity created on or before the date with the highest revision number.- Specified by:
find
in interfaceAuditReader
- Parameters:
cls
- Class of the entity.primaryKey
- Primary key of the entity.date
- Date for which to get entity revision.- Returns:
- The found entity instance at created on or before the specified date with the highest revision number or null, if an entity with the id had not been created on or before the specified date.
- Throws:
IllegalArgumentException
- if cls, primaryKey, or date is null.NotAuditedException
- When entities of the given class are not audited.RevisionDoesNotExistException
- If the given date is before the first revision.IllegalStateException
- If the associated entity manager is closed.
-
find
public <T> T find(Class<T> cls, Object primaryKey, LocalDateTime datetime) throws IllegalArgumentException, NotAuditedException, RevisionDoesNotExistException, IllegalStateException
Description copied from interface:AuditReader
Find an entity by primary key on the given datetime. The datetime specifies restricting the result to any entity created on or before the date with the highest revision number.- Specified by:
find
in interfaceAuditReader
- Parameters:
cls
- Class of the entity.primaryKey
- Primary key of the entity.datetime
- Datetime for which to get entity revision.- Returns:
- The found entity instance at created on or before the specified date with the highest revision number or null, if an entity with the id had not been created on or before the specified date.
- Throws:
IllegalArgumentException
- if cls, primaryKey, or date is null.NotAuditedException
- When entities of the given class are not audited.RevisionDoesNotExistException
- If the given date is before the first revision.IllegalStateException
- If the associated entity manager is closed.
-
getRevisions
public List<Number> getRevisions(Class<?> cls, String entityName, Object primaryKey) throws IllegalArgumentException, NotAuditedException, IllegalStateException
Description copied from interface:AuditReader
Get a list of revision numbers, at which an entity was modified, looking by entityName.- Specified by:
getRevisions
in interfaceAuditReader
- Parameters:
cls
- Class of the entity.entityName
- Name of the entity (if it can't be guessed basing on thecls
).primaryKey
- Primary key of the entity.- Returns:
- A list of revision numbers, at which the entity was modified, sorted in ascending order (so older revisions come first).
- Throws:
IllegalArgumentException
- If cls or primaryKey is null.NotAuditedException
- When entities of the given class are not audited.IllegalStateException
- If the associated entity manager is closed.
-
getRevisionDate
public Date getRevisionDate(Number revision) throws IllegalArgumentException, RevisionDoesNotExistException, IllegalStateException
Description copied from interface:AuditReader
Get the date, at which a revision was created.- Specified by:
getRevisionDate
in interfaceAuditReader
- Parameters:
revision
- Number of the revision for which to get the date.- Returns:
- Date of commiting the given revision.
- Throws:
IllegalArgumentException
- If revision is less or equal to 0.RevisionDoesNotExistException
- If the revision does not exist.IllegalStateException
- If the associated entity manager is closed.
-
getRevisionNumberForDate
public Number getRevisionNumberForDate(Date date)
Description copied from interface:AuditReader
Gets the revision number, that corresponds to the given date. More precisely, returns the number of the highest revision, which was created on or before the given date. So:getRevisionDate(getRevisionNumberForDate(date)) <= date
andgetRevisionDate(getRevisionNumberForDate(date)+1) > date
.- Specified by:
getRevisionNumberForDate
in interfaceAuditReader
- Parameters:
date
- Date for which to get the revision.- Returns:
- Revision number corresponding to the given date.
-
getRevisionNumberForDate
public Number getRevisionNumberForDate(LocalDateTime date)
Description copied from interface:AuditReader
Gets the revision number, that corresponds to the given date. More precisely, returns the number of the highest revision, which was created on or before the given date. So:getRevisionDate(getRevisionNumberForDate(date)) <= date
andgetRevisionDate(getRevisionNumberForDate(date)+1) > date
.- Specified by:
getRevisionNumberForDate
in interfaceAuditReader
- Parameters:
date
- Date for which to get the revision.- Returns:
- Revision number corresponding to the given date.
-
getRevisionNumberForDate
public Number getRevisionNumberForDate(Instant date)
Description copied from interface:AuditReader
Gets the revision number, that corresponds to the given date. More precisely, returns the number of the highest revision, which was created on or before the given date. So:getRevisionDate(getRevisionNumberForDate(date)) <= date
andgetRevisionDate(getRevisionNumberForDate(date)+1) > date
.- Specified by:
getRevisionNumberForDate
in interfaceAuditReader
- Parameters:
date
- Date for which to get the revision.- Returns:
- Revision number corresponding to the given date.
-
findRevision
public <T> T findRevision(Class<T> revisionEntityClass, Number revision) throws IllegalArgumentException, RevisionDoesNotExistException, IllegalStateException
Description copied from interface:AuditReader
A helper method; should be used only if a custom revision entity is used. See alsoRevisionEntity
.- Specified by:
findRevision
in interfaceAuditReader
- Type Parameters:
T
- The type of the revision entity to find- Parameters:
revisionEntityClass
- Class of the revision entity. Should be annotated withRevisionEntity
.revision
- Number of the revision for which to get the data.- Returns:
- Entity containing data for the given revision.
- Throws:
IllegalArgumentException
- If revision is less or equal to 0 or if the class of the revision entity is invalid.RevisionDoesNotExistException
- If the revision does not exist.IllegalStateException
- If the associated entity manager is closed.
-
findRevisions
public <T> Map<Number,T> findRevisions(Class<T> revisionEntityClass, Set<Number> revisions) throws IllegalArgumentException, IllegalStateException
Description copied from interface:AuditReader
Find a map of revisions using the revision numbers specified.- Specified by:
findRevisions
in interfaceAuditReader
- Type Parameters:
T
- The type of the revision entity to find- Parameters:
revisionEntityClass
- Class of the revision entity. Should be annotated withRevisionEntity
.revisions
- Revision numbers of the revision for which to get the data.- Returns:
- A map of revision number and the given revision entity.
- Throws:
IllegalArgumentException
- If a revision number is less or equal to 0 or if the class of the revision entity is invalid.IllegalStateException
- If the associated entity manager is closed.
-
getCrossTypeRevisionChangesReader
public CrossTypeRevisionChangesReader getCrossTypeRevisionChangesReader() throws AuditException
- Specified by:
getCrossTypeRevisionChangesReader
in interfaceAuditReader
- Returns:
- Basic implementation of
CrossTypeRevisionChangesReader
interface. Raises an exception if the default mechanism of tracking entity names modified during revisions has not been enabled. - Throws:
AuditException
- If none of the following conditions is satisfied:org.hibernate.envers.track_entities_changed_in_revision
parameter is set totrue
.- Custom revision entity (annotated with
RevisionEntity
) extendsDefaultTrackingModifiedEntitiesRevisionEntity
base class. - Custom revision entity (annotated with
RevisionEntity
) encapsulates a field marked withModifiedEntityNames
interface.
-
getCurrentRevision
public <T> T getCurrentRevision(Class<T> revisionEntityClass, boolean persist)
Description copied from interface:AuditReader
Gets an instance of the current revision entity, to which any entries in the audit tables will be bound. Please note the ifpersist
isfalse
, and no audited entities are modified in this session, then the obtained revision entity instance won't be persisted. Ifpersist
istrue
, the revision entity instance will always be persisted, regardless of whether audited entities are changed or not.- Specified by:
getCurrentRevision
in interfaceAuditReader
- Type Parameters:
T
- The type of the revision entity to find- Parameters:
revisionEntityClass
- Class of the revision entity. Should be annotated withRevisionEntity
.persist
- If the revision entity is not yet persisted, should it become persisted. This way, the primary identifier (id) will be filled (if it's assigned by the DB) and available, but the revision entity will be persisted even if there are no changes to audited entities. Otherwise, the revision number (id) can benull
.- Returns:
- The current revision entity, to which any entries in the audit tables will be bound.
-
createQuery
public AuditQueryCreator createQuery()
Description copied from interface:AuditReader
Creates an audit query- Specified by:
createQuery
in interfaceAuditReader
- Returns:
- A query creator, associated with this AuditReader instance, with which queries can be created and later executed. Shouldn't be used after the associated Session or EntityManager is closed.
-
isEntityClassAudited
public boolean isEntityClassAudited(Class<?> entityClass)
Description copied from interface:AuditReader
Checks if the entityClass was configured to be audited. Calling isEntityNameAudited() with the string of the class name will return the same value.- Specified by:
isEntityClassAudited
in interfaceAuditReader
- Parameters:
entityClass
- Class of the entity asking for audit support- Returns:
- true if the entityClass is audited.
-
isEntityNameAudited
public boolean isEntityNameAudited(String entityName)
Description copied from interface:AuditReader
Checks if the entityName was configured to be audited.- Specified by:
isEntityNameAudited
in interfaceAuditReader
- Parameters:
entityName
- EntityName of the entity asking for audit support.- Returns:
- true if the entityName is audited.
-
getEntityName
public String getEntityName(Object primaryKey, Number revision, Object entity) throws HibernateException
Description copied from interface:AuditReader
Get the entity name of an instance of an entity returned by this AuditReader.Each AuditReader maintains its own internal cache of objects which it has provided the caller, much like the
Session
maintains a first-level cache. It is this specific cache which this call uses to find and return the entity-name. This means if the supplied three values do not correlate to an existing entry in the AuditReader's first-level cache, this method will result in throwing aHibernateException
since that entity has not yet been loaded.- Specified by:
getEntityName
in interfaceAuditReader
- Parameters:
primaryKey
- the primary key of the associated entity instance.revision
- the revision of the associated entity of interest.entity
- the entity instance that was obtained previously from the same AuditReader.- Returns:
- the entityName for the given entity.
- Throws:
HibernateException
- if one of the following conditions are satisfied:- The supplied entity has yet to be returned by this AuditReader instance, e.g. it isn't in the reader's cache.
- The supplied entity, primary key, and revision triplet is not a valid combination.
-
-