Search in sources :

Example 1 with AuditException

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

the class ComponentPropertyMapper method mapToEntityFromMap.

@Override
public void mapToEntityFromMap(EnversService enversService, Object obj, Map data, Object primaryKey, AuditReaderImplementor versionsReader, Number revision) {
    if (data == null || obj == null) {
        return;
    }
    if (propertyData.getBeanName() == null) {
        // If properties are not encapsulated in a component but placed directly in a class
        // (e.g. by applying <properties> tag).
        delegate.mapToEntityFromMap(enversService, obj, data, primaryKey, versionsReader, revision);
        return;
    }
    final Setter setter = ReflectionTools.getSetter(obj.getClass(), propertyData, enversService.getServiceRegistry());
    // If all properties are null and single, then the component has to be null also.
    boolean allNullAndSingle = true;
    for (Map.Entry<PropertyData, PropertyMapper> property : delegate.getProperties().entrySet()) {
        if (data.get(property.getKey().getName()) != null || !(property.getValue() instanceof SinglePropertyMapper)) {
            allNullAndSingle = false;
            break;
        }
    }
    if (allNullAndSingle) {
        // single property, but default value need not be null, so we'll set it to null anyway
        setter.set(obj, null, null);
    } else {
        // set the component
        try {
            final Object subObj = ReflectHelper.getDefaultConstructor(componentClass).newInstance();
            setter.set(obj, subObj, null);
            delegate.mapToEntityFromMap(enversService, subObj, data, primaryKey, versionsReader, revision);
        } catch (Exception e) {
            throw new AuditException(e);
        }
    }
}
Also used : PropertyData(org.hibernate.envers.internal.entities.PropertyData) Setter(org.hibernate.property.access.spi.Setter) AuditException(org.hibernate.envers.exception.AuditException) HashMap(java.util.HashMap) Map(java.util.Map) AuditException(org.hibernate.envers.exception.AuditException)

Example 2 with AuditException

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

the class EmbeddedIdMapper method mapToEntityFromMap.

@Override
public boolean mapToEntityFromMap(Object obj, Map data) {
    if (data == null || obj == null) {
        return false;
    }
    final Getter getter = ReflectionTools.getGetter(obj.getClass(), idPropertyData, getServiceRegistry());
    final Setter setter = ReflectionTools.getSetter(obj.getClass(), idPropertyData, getServiceRegistry());
    try {
        final Object subObj = ReflectHelper.getDefaultConstructor(getter.getReturnType()).newInstance();
        boolean ret = true;
        for (IdMapper idMapper : ids.values()) {
            ret &= idMapper.mapToEntityFromMap(subObj, data);
        }
        if (ret) {
            setter.set(obj, subObj, null);
        }
        return ret;
    } catch (Exception e) {
        throw new AuditException(e);
    }
}
Also used : Getter(org.hibernate.property.access.spi.Getter) Setter(org.hibernate.property.access.spi.Setter) AuditException(org.hibernate.envers.exception.AuditException) AuditException(org.hibernate.envers.exception.AuditException)

Example 3 with AuditException

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

the class AbstractOneToOneMapper method nullSafeMapToEntityFromMap.

@Override
public void nullSafeMapToEntityFromMap(EnversService enversService, Object obj, Map data, Object primaryKey, AuditReaderImplementor versionsReader, Number revision) {
    final EntityInfo referencedEntity = getEntityInfo(enversService, referencedEntityName);
    Object value;
    try {
        value = queryForReferencedEntity(versionsReader, referencedEntity, (Serializable) primaryKey, revision);
    } catch (NoResultException e) {
        value = null;
    } catch (NonUniqueResultException e) {
        throw new AuditException("Many versions results for one-to-one relationship " + entityName + "." + getPropertyData().getBeanName() + ".", e);
    }
    setPropertyValue(obj, value);
}
Also used : NonUniqueResultException(org.hibernate.NonUniqueResultException) Serializable(java.io.Serializable) AuditException(org.hibernate.envers.exception.AuditException) NoResultException(javax.persistence.NoResultException)

Example 4 with AuditException

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

the class MiddleEmbeddableComponentMapper method mapToObjectFromFullMap.

@Override
public Object mapToObjectFromFullMap(EntityInstantiator entityInstantiator, Map<String, Object> data, Object dataObject, Number revision) {
    try {
        final Object componentInstance = dataObject != null ? dataObject : ReflectHelper.getDefaultConstructor(componentClass).newInstance();
        delegate.mapToEntityFromMap(entityInstantiator.getEnversService(), componentInstance, data, null, entityInstantiator.getAuditReaderImplementor(), revision);
        return componentInstance;
    } catch (Exception e) {
        throw new AuditException(e);
    }
}
Also used : AuditException(org.hibernate.envers.exception.AuditException) AuditException(org.hibernate.envers.exception.AuditException)

Example 5 with AuditException

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

the class AuditReaderImpl method getRevisionDate.

@Override
public Date getRevisionDate(Number revision) throws IllegalArgumentException, RevisionDoesNotExistException, IllegalStateException {
    checkNotNull(revision, "Entity revision");
    checkPositive(revision, "Entity revision");
    checkSession();
    final Query<?> query = enversService.getRevisionInfoQueryCreator().getRevisionDateQuery(session, revision);
    try {
        final Object timestampObject = query.uniqueResult();
        if (timestampObject == null) {
            throw new RevisionDoesNotExistException(revision);
        }
        // The timestamp object is either a date or a long
        return timestampObject instanceof Date ? (Date) timestampObject : new Date((Long) timestampObject);
    } 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) Date(java.util.Date)

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