title: Retrieve Revisions of Mapping Tables - Hibernate Envers
body: <p>I'm using Hibernate with Envers, 4.3.6 FINAL. I have set up auditing using the annotations, and all of my audit tables have been created and are being populated correctly when I save information in my application. One of my audited entities has a mapping table to additional entities, with a @ManyToMany configuration.</p>
  
  <p>Employer -> Designations</p>
  
  <p>The employer can have 0 to n Designation entities associated with it. The mapping table is standard, just having two columns, a foreign key to the Employers table and a foreign key to the Designations table. Envers created an auditing table for this mapping table and records are appropriately recorded, with inserts or deletes entered with the same REV as the Employer when the Employer is edited.</p>
  
  <p>What I'm trying to do now is retrieve the audit information from the database so I can present it for reporting purposes. Retrieving the list of revisions for my Employer is working and the state of the Employer for a given revision is accurate. What I haven't been able to figure out is how to retrieve the audits for the mapping table. I have no actual entity for these pairings. They are put together using the javax.persistence.JoinTable annotation. As such, there's no record for an entity in my REVCHANGES table (I'm setting org.hibernate.envers.track_entities_changed_in_revision to true). Looking at the database, there are records for my EmployerDesignations mapping table and the corresponding EmployerDesignations_AUD audit table, but I don't know how to retrieve these audit records nor how to even know when they have been changed for a given revision.</p>
  
  <p>How can the audit history for a mapping table be retrieved?</p>
  
  <p>Employer class:</p>
  
  <pre><code>@Entity
  @DynamicInsert
  @DynamicUpdate
  @SelectBeforeUpdate
  @Table(name="EMPLOYERS")
  @Audited
  public class Employer implements Serializable {
  
      //All other fields omitted for brevity
      ...
  
      private List&lt;Designations&gt; designations;
      @ManyToMany
      @JoinTable(name = "EMPLOYERDESIGNATIONS", 
          joinColumns =           { @JoinColumn(name = "FK_EMPLOYER", nullable = false) },
          inverseJoinColumns =    { @JoinColumn(name = "FK_DESIGNATION", nullable = false) })
      public List&lt;Designations&gt; getDesignations() {
          return designations;
      }
      public void setDesignations(List&lt;Designations&gt; designations) {
          this.designations= designations;
      }
  }
  </code></pre>
  
  <p>Designation class:</p>
  
  <pre><code>@Entity
  @DynamicInsert
  @DynamicUpdate
  @SelectBeforeUpdate
  @Table(name="DESIGNATIONS")
  @Audited
  public class Designation implements Serializable {
  
      //All other fields omitted for brevity
      ...
  
      private List&lt;Employer&gt; employers;
      @ManyToMany
      @JoinTable(name = "EMPLOYERDESIGNATIONS", 
          joinColumns =           { @JoinColumn(name = "FK_DESIGNATION", nullable = false) },
          inverseJoinColumns =    { @JoinColumn(name = "FK_EMPLOYER", nullable = false) })
      public List&lt;Employer&gt; getEmployers() {
          return employers;
      }
      public void setEmployers(List&lt;Employer&gt; employers) {
          this.employers= employers;
      }
  }
  </code></pre>
  
  <p>Test Code for audit data retrieval:</p>
  
  <pre><code>List&lt;Number&gt; revisions = reader.getRevisions(Employer.class, employerId);
  System.out.println(revisions);
  for (Number revisionNum : revisions) {
      Employer employer = reader.find(Employer.class, employerId, revisionNum);
      System.out.println(employer);
  }
  </code></pre>