hibernate-enversnhibernate-envers

Add OR condition to query


I am wondering how it is possible to add an OR condition to the Envers criteria api:

    public IEnumerable<Guid> GetHistory(object id, params string[] props)
    {
        var auditQuery = AuditReaderFactory.Get(Session).CreateQuery()
            .ForRevisionsOfEntity(typeof(T), false, true);

        foreach (var prop in props)
        {
            auditQuery.Add(AuditEntity.RelatedId(prop).Eq(id)); // <-- adds AND, while OR is required!
        }

        return auditQuery
            .GetResultList<object[]>()
            .Select(i => ((T)i[0]).ID)
            .Distinct();
    }

Solution

  • Use AuditEntity.Disjunction().

    In your example, something like...

    [..]
    var disjunction = AuditEntity.Disjunction();
    foreach (var prop in props)
    {
      disjunction.Add(AuditEntity.RelatedId(prop).Eq(id)); 
    }
    auditQuery.Add(disjunction);
    [..]