2014-10-30 83 views
1

我想知道它是如何可能一個OR條件添加到Envers標準API:添加OR條件查詢

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(); 
    } 

回答

3

使用AuditEntity.Disjunction()

在你的榜樣,像...

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

我@Roger沒有像這樣Java如上所述。 (以防萬一需要)

public List<Employee> getAuditHistory(Session session, int id, String property) { 
    AuditReader auditReader = AuditReaderFactory.get(session); 
    List<Employee> employeeHistory = new ArrayList<>(); 
    if (auditReader != null) { 
     AuditQuery auditQuery = auditReader.createQuery().forRevisionsOfEntity(Employee.class, true, false) 
       .add(AuditEntity.property(ResultsConstants.Employee_ID).eq(id)); 
     AuditDisjunction auditDisjunction = null; 
     if (property.equalsIgnoreCase("FULL_NAME")) { 
      auditDisjunction = AuditEntity.disjunction().add(AuditEntity.property("FIRST_NAME".toUpperCase()).hasChanged()) 
        .add(AuditEntity.property("LAST_NAME".toUpperCase()).hasChanged()); 
     } else { 
      auditQuery = auditQuery.add(AuditEntity.property(property.toUpperCase()).hasChanged()); 
     } 
     auditQuery = auditQuery.addOrder(AuditEntity.property("MODIFIED_DATE").desc()); 
     if(null != auditDisjunction){ 
      auditQuery = auditQuery.add(auditDisjunction); 
     } 
     if (auditQuery != null) { 
      if (auditQuery.getResultList().isEmpty()) { 
       // Log here or throw it back to caller 
      } 
      employeeHistory.addAll(auditQuery.getResultList()); 
     } 
    } 
    return employeeHistory; 
}