Search in sources :

Example 6 with AuditException

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

the class AuditReaderImpl method findRevision.

@Override
@SuppressWarnings({ "unchecked" })
public <T> T findRevision(Class<T> revisionEntityClass, Number revision) throws IllegalArgumentException, RevisionDoesNotExistException, IllegalStateException {
    revisionEntityClass = getTargetClassIfProxied(revisionEntityClass);
    checkNotNull(revision, "Entity revision");
    checkPositive(revision, "Entity revision");
    checkSession();
    final Set<Number> revisions = new HashSet<>(1);
    revisions.add(revision);
    final Query<?> query = enversService.getRevisionInfoQueryCreator().getRevisionsQuery(session, revisions);
    try {
        final T revisionData = (T) query.uniqueResult();
        if (revisionData == null) {
            throw new RevisionDoesNotExistException(revision);
        }
        return revisionData;
    } catch (NonUniqueResultException e) {
        throw new AuditException(e);
    }
}
Also used : NonUniqueResultException(org.hibernate.NonUniqueResultException) AuditException(org.hibernate.envers.exception.AuditException) RevisionDoesNotExistException(org.hibernate.envers.exception.RevisionDoesNotExistException) HashSet(java.util.HashSet)

Example 7 with AuditException

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

the class AuditReaderImpl method getRevisionNumberForDate.

@Override
public Number getRevisionNumberForDate(Date date) {
    checkNotNull(date, "Date of revision");
    checkSession();
    final Query<?> query = enversService.getRevisionInfoQueryCreator().getRevisionNumberForDateQuery(session, date);
    try {
        final Number res = (Number) query.uniqueResult();
        if (res == null) {
            throw new RevisionDoesNotExistException(date);
        }
        return res;
    } catch (NonUniqueResultException e) {
        throw new AuditException(e);
    }
}
Also used : NonUniqueResultException(org.hibernate.NonUniqueResultException) AuditException(org.hibernate.envers.exception.AuditException) RevisionDoesNotExistException(org.hibernate.envers.exception.RevisionDoesNotExistException)

Example 8 with AuditException

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

the class RelatedAuditInExpression 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("The criterion can only be used on a property that is a relation to another property.");
    }
    // todo: should this throw an error if qpdList is null?  is it possible?
    List<QueryParameterData> qpdList = relatedEntity.getIdMapper().mapToQueryParametersFromId(propertyName);
    if (qpdList != null) {
        QueryParameterData qpd = qpdList.iterator().next();
        parameters.addWhereWithParams(alias, qpd.getQueryParameterName(), "in (", ids, ")");
    }
}
Also used : QueryParameterData(org.hibernate.envers.internal.entities.mapper.id.QueryParameterData) RelationDescription(org.hibernate.envers.internal.entities.RelationDescription) AuditException(org.hibernate.envers.exception.AuditException)

Example 9 with AuditException

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

the class SimpleAuditExpression 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) {
        // HHH-9178 - Add support to component type equality.
        // This basically will allow = and <> operators to perform component-based equality checks.
        // Any other operator for a component type will not be supported.
        // Non-component types will continue to behave normally.
        final SessionImplementor session = versionsReader.getSessionImplementor();
        final Type type = getPropertyType(session, entityName, propertyName);
        if (type != null && type.isComponentType()) {
            if (!"=".equals(op) && !"<>".equals(op)) {
                throw new AuditException("Component-based criterion is not supported for op: " + op);
            }
            final ComponentType componentType = (ComponentType) type;
            for (int i = 0; i < componentType.getPropertyNames().length; i++) {
                final Object componentValue = componentType.getPropertyValue(value, i, session);
                parameters.addWhereWithParam(alias, propertyName + "_" + componentType.getPropertyNames()[i], op, componentValue);
            }
        } else {
            parameters.addWhereWithParam(alias, propertyName, op, value);
        }
    } else {
        if (!"=".equals(op) && !"<>".equals(op)) {
            throw new AuditException("This type of operation: " + op + " (" + entityName + "." + propertyName + ") isn't supported and can't be used in queries.");
        }
        Object id = relatedEntity.getIdMapper().mapToIdFromEntity(value);
        relatedEntity.getIdMapper().addIdEqualsToQuery(parameters, id, alias, null, "=".equals(op));
    }
}
Also used : ComponentType(org.hibernate.type.ComponentType) Type(org.hibernate.type.Type) ComponentType(org.hibernate.type.ComponentType) RelationDescription(org.hibernate.envers.internal.entities.RelationDescription) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) AuditException(org.hibernate.envers.exception.AuditException)

Example 10 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