Search code examples
javahibernatespring-mvchibernate-envers

Envers - Showing audit data with relationships


  • I added @Audited in my entities;
  • I created my listener to add the user ID to revinfo;
  • I can filter audited data with user id, entity class, min and max date, using:

    public <T extends BaseModel> List<Object[]> buscar(Class<T> clazz, Usuario usuario, java.util.Date inicio, java.util.Date fim){
    GregorianCalendar novo = new GregorianCalendar();
    novo.setTime(fim);
    novo.add(Calendar.DAY_OF_MONTH, 1);
    
    AuditReader reader = AuditReaderFactory.get(getEm());
    return reader.createQuery()
        .forRevisionsOfEntity(clazz, false, true)
        .add(AuditEntity.revisionProperty("usuario")
            .eq(usuario))
        .add(AuditEntity.revisionProperty("revtstmp")
            .between(inicio.getTime(), novo.getTime().getTime()))
        .addOrder(AuditEntity.property("id")
            .asc())
        .getResultList();
    
    }
    

But all relationships are lazy, including the @ManyToOne.

I found many post about problems with @OneToMany, but this is not the case

What can I do to access these properties?

PS: I tried, but could not highlight the code.


Solution

  • All relations in the objects returned by Envers are lazy, regardless if it's a one-to-many or many-to-one.

    In an object, to access the related object's properties just call the getter :)

    In a query, it's not possible. Joins are not supported, also regardless of the relation type. You can only constraint the id of the related entity, but not its properties.