Search in sources :

Example 11 with AuditException

use of org.hibernate.envers.exception.AuditException in project hibernate-orm by hibernate.

the class AuditReaderImpl method find.

@Override
@SuppressWarnings({ "unchecked" })
public <T> T find(Class<T> cls, String entityName, Object primaryKey, Number revision, boolean includeDeletions) throws IllegalArgumentException, NotAuditedException, IllegalStateException {
    cls = getTargetClassIfProxied(cls);
    checkNotNull(cls, "Entity class");
    checkNotNull(entityName, "Entity name");
    checkNotNull(primaryKey, "Primary key");
    checkNotNull(revision, "Entity revision");
    checkPositive(revision, "Entity revision");
    checkSession();
    if (firstLevelCache.contains(entityName, revision, primaryKey)) {
        return (T) firstLevelCache.get(entityName, revision, primaryKey);
    }
    Object result;
    try {
        // The result is put into the cache by the entity instantiator called from the query
        result = createQuery().forEntitiesAtRevision(cls, entityName, revision, includeDeletions).add(AuditEntity.id().eq(primaryKey)).getSingleResult();
    } catch (NoResultException e) {
        result = null;
    } catch (NonUniqueResultException e) {
        throw new AuditException(e);
    }
    return (T) result;
}
Also used : NonUniqueResultException(org.hibernate.NonUniqueResultException) AuditException(org.hibernate.envers.exception.AuditException) NoResultException(javax.persistence.NoResultException)

Example 12 with AuditException

use of org.hibernate.envers.exception.AuditException in project hibernate-orm by hibernate.

the class AuditReaderImpl method findRevisions.

@Override
@SuppressWarnings({ "unchecked" })
public <T> Map<Number, T> findRevisions(Class<T> revisionEntityClass, Set<Number> revisions) throws IllegalArgumentException, IllegalStateException {
    revisionEntityClass = getTargetClassIfProxied(revisionEntityClass);
    final Map<Number, T> result = new HashMap<>(revisions.size());
    for (Number revision : revisions) {
        checkNotNull(revision, "Entity revision");
        checkPositive(revision, "Entity revision");
    }
    checkSession();
    final Query<?> query = enversService.getRevisionInfoQueryCreator().getRevisionsQuery(session, revisions);
    try {
        final List<?> revisionList = query.getResultList();
        for (Object revision : revisionList) {
            final Number revNo = enversService.getRevisionInfoNumberReader().getRevisionNumber(revision);
            result.put(revNo, (T) revision);
        }
        return result;
    } catch (HibernateException e) {
        throw new AuditException(e);
    }
}
Also used : HashMap(java.util.HashMap) HibernateException(org.hibernate.HibernateException) AuditException(org.hibernate.envers.exception.AuditException)

Example 13 with AuditException

use of org.hibernate.envers.exception.AuditException in project hibernate-orm by hibernate.

the class RelatedAuditEqualityExpression method addToQuery.

@Override
protected void addToQuery(EnversService enversService, AuditReaderImplementor versionsReader, String entityName, String alias, QueryBuilder qb, Parameters parameters) {
    String propertyName = CriteriaTools.determinePropertyName(enversService, versionsReader, entityName, propertyNameGetter);
    RelationDescription relatedEntity = CriteriaTools.getRelatedEntity(enversService, entityName, propertyName);
    if (relatedEntity == null) {
        throw new AuditException("This criterion can only be used on a property that is a relation to another property.");
    }
    relatedEntity.getIdMapper().addIdEqualsToQuery(parameters, id, alias, null, equals);
}
Also used : RelationDescription(org.hibernate.envers.internal.entities.RelationDescription) AuditException(org.hibernate.envers.exception.AuditException)

Aggregations

AuditException (org.hibernate.envers.exception.AuditException)13 NonUniqueResultException (org.hibernate.NonUniqueResultException)5 RevisionDoesNotExistException (org.hibernate.envers.exception.RevisionDoesNotExistException)3 RelationDescription (org.hibernate.envers.internal.entities.RelationDescription)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 NoResultException (javax.persistence.NoResultException)2 Setter (org.hibernate.property.access.spi.Setter)2 Serializable (java.io.Serializable)1 Date (java.util.Date)1 HashSet (java.util.HashSet)1 HibernateException (org.hibernate.HibernateException)1 SessionImplementor (org.hibernate.engine.spi.SessionImplementor)1 PropertyData (org.hibernate.envers.internal.entities.PropertyData)1 IdMapper (org.hibernate.envers.internal.entities.mapper.id.IdMapper)1 QueryParameterData (org.hibernate.envers.internal.entities.mapper.id.QueryParameterData)1 Getter (org.hibernate.property.access.spi.Getter)1 ComponentType (org.hibernate.type.ComponentType)1 Type (org.hibernate.type.Type)1