Spork
Spork

Reputation: 1651

How should I visualize the data collected by Envers?

I'm a true beginner at the Hibernate (4.2) + Spring combo tasked to work on a sub-application that uses these to do facilitate some CRUD management in an administrator portal, so I am probably getting lost at a very basic step.

I have loaded Hibernate-Envers into the project to do audit-logging on a specific @Entity field. After adding @Audited to it Envers works as expected, adding data into the _aud table.

My goal is to show a list of all revisions to this entity to the administrators. I am not sure how I should go about it.

What I've tried so far is..

  1. Create a new Repository class that I imagined would use the AuditReader to supply a list of all revisions through some new Service. However, I couldn't make this work without adding a new Entity for the History object itself - and I felt this was probably not the way to go.
  2. Attempt to use an AuditReader in the existing controller for the the Entity. For the creation of an AuditReader I needed a sessionFactory that I tried to autowire into the controller, but this turned out not to be the right thing, as autowiring failed.

What is the preferred structure for gathering data from the envers *_aud tables to show them to the end-users on a webpage?

Upvotes: 0

Views: 354

Answers (1)

Naros
Naros

Reputation: 21123

Create a new Repository class that I imagined would use the AuditReader to supply a list of all revisions through some new Service. However, I couldn't make this work without adding a new Entity for the History object itself - and I felt this was probably not the way to go.

Let us assume we have some really silly basic entity like this:

@Entity
@Audited
public class BasicEntity {
  @Id
  @GeneratedValue
  private Integer id;
  private String name;
  ...
}

You should be able to construct two repository/dao components that both use BasicEntity as the entity-type here, but both internally operate very differently with those instances.

Your business repository component would have the Session or EntityManager injected and you use that to manipulate these instances calling #persist, #merge, etc.

Your audit repository component would also have the Session or EntityManager injected but would use that to construct an AuditReader and query the audit tables.

@Component
public interface BasicEntityAuditRepository implements AuditRepository<BasicEntity> {

  @PersistenceContext
  private EntityManager entityManager;

  @Override
  public List<BasicEntity> findByRevision(Long id, Number revision) {
    final AuditReader auditReader = AuditReaderFactory.get( entityManager );
    // build whatever query and return the results.
  }

}

public interface AuditRepository<T> {
  List<T> findByRevision(Long id, Number revision);
}

Upvotes: 1

Related Questions