Skip to content

Instantly share code, notes, and snippets.

@thjanssen
Last active March 16, 2017 16:27
Show Gist options
  • Save thjanssen/88db6a0df440a03172ee568811e2cc84 to your computer and use it in GitHub Desktop.
Save thjanssen/88db6a0df440a03172ee568811e2cc84 to your computer and use it in GitHub Desktop.
@Entity
@Audited
public class Author implements Serializable { … }
CREATE TABLE author_aud
(
id bigint NOT NULL,
rev integer NOT NULL,
revtype smallint,
firstname character varying(255),
lastname character varying(255),
CONSTRAINT author_aud_pkey PRIMARY KEY (id, rev),
CONSTRAINT author_aud_revinfo FOREIGN KEY (rev)
REFERENCES revinfo (rev) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)
AuditReader auditReader = AuditReaderFactory.get(em);
List revisionNumbers = auditReader.getRevisions(Book.class, b.getId());
for (Number rev : revisionNumbers) {
Book auditedBook = auditReader.find(Book.class, b.getId(), rev);
log.info(“Book [“+auditedBook+”] at revision [“+rev+”].”);
}
AuditReader auditReader = AuditReaderFactory.get(em);
Book auditedBook = auditReader.find(Book.class, b.getId(), created);
log.info(“Book [“+auditedBook+”] at [“+created+”].”);
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
Author a = new Author();
a.setFirstName(“Thorben”);
a.setLastName(“Janssen”);
em.persist(a);
Book b = new Book();
b.setTitle(“Hibernate Tips”);
b.getAuthors().add(a);
a.getBooks().add(b);
em.persist(b);
em.getTransaction().commit();
em.close();
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-envers</artifactId>
<version>5.2.5.Final</version>
</dependency>
CREATE TABLE revinfo
(
rev integer NOT NULL,
revtstmp bigint,
CONSTRAINT revinfo_pkey PRIMARY KEY (rev)
)
Book b = em.find(Book.class, b.getId());
b.setTitle(“Hibernate Tips – 64 Tips for your day to day work”)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment