1 Reply Latest reply on Sep 27, 2011 2:01 PM by adamw

    contains clause in forEntitiesAtRevision query?

    wallenborn

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