Interface AuditReader

  • All Known Subinterfaces:
    AuditReaderImplementor
    All Known Implementing Classes:
    AuditReaderImpl

    public interface AuditReader
    Provides access to past versions of audited entities based on a global revision. A new revision is created every time any audited entity is created, updated, or deleted.

    By default, each revision is defined by:

    • a strictly-increasing global revision number, and
    • the Unix epoch at which the revision was made.
    The method getRevisionNumberForDate(LocalDateTime) obtains the revision number which was current at the given instant.

    Given a revision number, we may obtain historical versions of any entity. For example, find(Class, Object, Number) retrieves the version of an entity with the given id which was current in the given revision. Much more complex queries are possible via createQuery().

    • Method Detail

      • find

        <T> T find​(Class<T> cls,
                   Object primaryKey,
                   Number revision)
            throws IllegalArgumentException,
                   NotAuditedException,
                   IllegalStateException
        Find an entity by primary key at the given revision.
        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

        <T> T find​(Class<T> cls,
                   String entityName,
                   Object primaryKey,
                   Number revision)
            throws IllegalArgumentException,
                   NotAuditedException,
                   IllegalStateException
        Find an entity by primary key at the given revision with the specified entityName.
        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 the cls).
        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

        <T> T find​(Class<T> cls,
                   String entityName,
                   Object primaryKey,
                   Number revision,
                   boolean includeDeletions)
            throws IllegalArgumentException,
                   NotAuditedException,
                   IllegalStateException
        Find an entity by primary key at the given revision with the specified entityName, possibly including deleted entities in the search.
        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 the cls).
        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.
      • findRevisions

        <T> Map<Number,​T> findRevisions​(Class<T> revisionEntityClass,
                                              Set<Number> revisions)
                                       throws IllegalArgumentException,
                                              IllegalStateException
        Find a map of revisions using the revision numbers specified.
        Type Parameters:
        T - The type of the revision entity to find
        Parameters:
        revisionEntityClass - Class of the revision entity. Should be annotated with RevisionEntity.
        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.
      • getCurrentRevision

        @Deprecated(since="5.2")
        <T> T getCurrentRevision​(Class<T> revisionEntityClass,
                                 boolean persist)
        Deprecated.
        use RevisionListener instead. While this method is being deprecated, expect a new API for this in 6.0.
        Gets an instance of the current revision entity, to which any entries in the audit tables will be bound. Please note the if persist is false, and no audited entities are modified in this session, then the obtained revision entity instance won't be persisted. If persist is true, the revision entity instance will always be persisted, regardless of whether audited entities are changed or not.
        Type Parameters:
        T - The type of the revision entity to find
        Parameters:
        revisionEntityClass - Class of the revision entity. Should be annotated with RevisionEntity.
        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 be null.
        Returns:
        The current revision entity, to which any entries in the audit tables will be bound.
      • createQuery

        AuditQueryCreator createQuery()
        Creates an audit query
        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

        boolean isEntityClassAudited​(Class<?> entityClass)
        Checks if the entityClass was configured to be audited. Calling isEntityNameAudited() with the string of the class name will return the same value.
        Parameters:
        entityClass - Class of the entity asking for audit support
        Returns:
        true if the entityClass is audited.
      • isEntityNameAudited

        boolean isEntityNameAudited​(String entityName)
        Checks if the entityName was configured to be audited.
        Parameters:
        entityName - EntityName of the entity asking for audit support.
        Returns:
        true if the entityName is audited.
      • getEntityName

        String getEntityName​(Object primaryKey,
                             Number revision,
                             Object entity)
                      throws HibernateException
        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 a HibernateException since that entity has not yet been loaded.

        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.