Search in sources :

Example 1 with NonUniqueResultException

use of org.hibernate.NonUniqueResultException 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 2 with NonUniqueResultException

use of org.hibernate.NonUniqueResultException 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)

Example 3 with NonUniqueResultException

use of org.hibernate.NonUniqueResultException 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 4 with NonUniqueResultException

use of org.hibernate.NonUniqueResultException 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 5 with NonUniqueResultException

use of org.hibernate.NonUniqueResultException in project hibernate-orm by hibernate.

the class AbstractProducedQuery method uniqueElement.

public static <R> R uniqueElement(List<R> list) throws NonUniqueResultException {
    int size = list.size();
    if (size == 0) {
        return null;
    }
    R first = list.get(0);
    for (int i = 1; i < size; i++) {
        if (list.get(i) != first) {
            throw new NonUniqueResultException(list.size());
        }
    }
    return first;
}
Also used : NonUniqueResultException(org.hibernate.NonUniqueResultException) WAIT_FOREVER(org.hibernate.LockOptions.WAIT_FOREVER) EntityGraphQueryHint(org.hibernate.engine.query.spi.EntityGraphQueryHint)

Aggregations

NonUniqueResultException (org.hibernate.NonUniqueResultException)6 AuditException (org.hibernate.envers.exception.AuditException)5 RevisionDoesNotExistException (org.hibernate.envers.exception.RevisionDoesNotExistException)3 NoResultException (javax.persistence.NoResultException)2 Serializable (java.io.Serializable)1 Date (java.util.Date)1 HashSet (java.util.HashSet)1 WAIT_FOREVER (org.hibernate.LockOptions.WAIT_FOREVER)1 EntityGraphQueryHint (org.hibernate.engine.query.spi.EntityGraphQueryHint)1