4 Replies Latest reply: Apr 18, 2011 11:15 AM by Stevo Slavic RSS

Null instead of RevisionDoesNotExistException

Stevo Slavic Newbie

Couldn't AuditQuery's getResultList and getSingleResult return null instead of throwing RevisionDoesNotExistException?

  • 1. Null instead of RevisionDoesNotExistException
    Adam Warski Master

    Example query?

     

    Adam

  • 2. Null instead of RevisionDoesNotExistException
    Stevo Slavic Newbie

    Did I mention that I'm total newbie to Envers? Well, I am. You have been warned

     

    Here's a method I have in a generic DAO:

     

    public T findOneAtRevisionDate(ID id, Date revisionDate) {

            AuditReader auditReader = AuditReaderFactory.get(getEntityManager());

            Number revision = auditReader.getRevisionNumberForDate(revisionDate);

            return auditReader.find(getDomainClass(), id, revision);

    }

     

    where T is @Entity, and ID is Serializable PK of T. I'm not sure if this is correct usage of Envers API.

     

    Throwing and handling RevisionDoesNotExistException IMO is too expensive for a normal non-exceptional situation. When finding not audited entity in db by id no exception is thrown when instance/record doesn't exist, a null is returned; when querying for collection and there are no hits, empty collection is returned, and no exception is thrown since it's a normal non-exceptional situation. Same IMO should be the case when querying history data.

  • 3. Null instead of RevisionDoesNotExistException
    Hernán Chanfreau Master

    Hello!

     

    I don't think that these are normal situations.

    The only situation should be trying to retrieve a revision with a date referring to a moment before the object was initially created...

     

    Am I missing something?

     

    Regards. Hernán.

  • 4. Null instead of RevisionDoesNotExistException
    Stevo Slavic Newbie

    IMO AuditReader's both getRevisionNumberForDate and find method throw unnecessarily exception when they can just return null. Same goes for AuditQuery's getSingleResult and getResultList. Returning null, and empty list is natural for non-audited entities (see getResultList and getSingleResult in javax.persistence.TypedQuery), I don't see why it shouldn't be natural for audited entities.

     

    @Hernán Why do you think it's not normal/ordinary situation to query and find nothing? What's exceptional there?

     

    I quote Joshua Bloch, Effective Java: Use exceptions only for exceptional conditions.

     

    Regards,

    Stevo.