Search code examples
javahibernatehibernate-envers

Hibernate envers - fetch latest data for non-audited and historical data for audited fields


I would like to know if there is a easy/standard way to query oneToMany relationship where owning entity/parent is not audited but the child is audited

Person -< Address

public class Person {
    @Id
    int id;

    @Column(name = "name")
    String name;

    @Audited
    @OneToMany(mappedBy = "person", orphanRemoval = true, cascade = { CascadeType.ALL })
    private List<Address> addresses = new ArrayList<>();
}

@Audited
public class Address {
    @Id
    int id;

    @Column(name = "country")
    String country;

    @Column(name = "city")
    String city;

    @ManyToOne
    @JoinColumn(name = "person_id")
    private Person person;
}

So how can I query the list of address and person at a revision with all fields populated (audited & non-audited)

E.g. On the above data model, if I do a

auditReader.createQuery().
     forEntitiesAtRevision(Person.class, 2).getResultList().get(0);

I am expecting the below result

Person 1 -> 5, "Bob", [{1, "USA", "Seattle"},{2,"USA","Austin"}]

But instead its returning

Person 1 -> 5, null , [{1, "USA", "Seattle"},{2,"USA","Austin"}]

Solution

  • If the fields are not audited, they are not going to be populated. How could they be if the data is not stored? :)