Search in sources :

Example 6 with NonUniqueResultException

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

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