Let's say i have Employees and Departments. An employee is always assigned to one department and a department has a set of employees. If i want to know the name of the current department of an employee whose id i know, i can simply say:
Employee emp = entityManager.find(Employee.class, empId);
String depName = emp.getDepartment().getName();
Alternatively, i can also query Department directly using JPQL, which is easier if i only have empId:
TypedQuery<Department> q = entityManager
.createQuery("select d from Department d join d.employees e where e.id=:empId", Department.class)
.setParameter("empId", empId);
String depName = q.getSingleResult().getName();
If i want to know the name of emp's department at a certain point in the past, i can use envers:
AuditReader reader = AuditReaderFactory.get(entityManager);
AuditQuery query = reader.createQuery()
.forEntitiesAtRevision(Employee.class, atThisRevisionId)
.add(AuditEntity.id().eq(empId));
Employee emp = query.getSingleResult();
String depName = emp.getDepartment().getName();
where envers makes sure the emp.getDepartment() resolves to the department at the time of atThisRevisionId. But just like in the first snippet above, i have to ask for an employee first, and then for its department. Is there a way to formulate the query on Departments directly? Something along these lines:
AuditReader reader = AuditReaderFactory.get(entityManager);
AuditQuery query = reader.createQuery()
.forEntitiesAtRevision(Department.class, atThisRevisionId)
.add(AuditEntity.property("employees").id().contains(empId));
Department dep = query.getSingleResult();
String depName = dep.getName();
Hmm, I'm afraid that's not yet implementd. Support for queries is still at the basic level, but I hope to expand it some day using the ValidityAuditStrategy. For the time being you could only query for employees with a given department number.
Adam