From 04802d5c268fb87a74fa4480ac7ff14ef49baf07 Mon Sep 17 00:00:00 2001 From: Adam Warski Date: Thu, 30 Apr 2009 07:36:14 +0000 Subject: [PATCH] HHH-3823: - adding a method to obtain the current revision entity + test git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@16485 1b8cb986-b30d-0410-93ca-fae66ebed9b2 --- .../org/hibernate/envers/AuditReader.java | 14 ++ .../envers/reader/AuditReaderImpl.java | 17 ++- .../DefaultRevisionInfoGenerator.java | 16 +- .../revisioninfo/RevisionInfoGenerator.java | 3 +- .../envers/synchronization/AuditSync.java | 19 ++- .../reventity/CustomDataRevEntity.java | 95 ++++++++++++ .../reventity/CustomNoListener.java | 137 ++++++++++++++++++ 7 files changed, 287 insertions(+), 14 deletions(-) create mode 100644 envers/src/test/java/org/hibernate/envers/test/entities/reventity/CustomDataRevEntity.java create mode 100644 envers/src/test/java/org/hibernate/envers/test/integration/reventity/CustomNoListener.java diff --git a/envers/src/main/java/org/hibernate/envers/AuditReader.java b/envers/src/main/java/org/hibernate/envers/AuditReader.java index dfdadc2ded..5b353c610b 100644 --- a/envers/src/main/java/org/hibernate/envers/AuditReader.java +++ b/envers/src/main/java/org/hibernate/envers/AuditReader.java @@ -100,6 +100,20 @@ public interface AuditReader { T findRevision(Class revisionEntityClass, Number revision) throws IllegalArgumentException, RevisionDoesNotExistException, IllegalStateException; + /** + * Gets an instance of the current revision entity, to which any entries in the audit tables will be bound. + * Please note the if {@code persist} is {@code false}, and no audited entities are modified in this session, + * then the obtained revision entity instance won't be persisted. If {@code persist} is {@code true}, the revision + * entity instance will always be persisted, regardless of whether audited entities are changed or not. + * @param revisionEntityClass Class of the revision entity. Should be annotated with {@link RevisionEntity}. + * @param persist If the revision entity is not yet persisted, should it become persisted. This way, the primary + * identifier (id) will be filled (if it's assigned by the DB) and available, but the revision entity will be + * persisted even if there are no changes to audited entities. Otherwise, the revision number (id) can be + * {@code null}. + * @return The current revision entity, to which any entries in the audit tables will be bound. + */ + T getCurrentRevision(Class revisionEntityClass, boolean persist); + /** * * @return A query creator, associated with this AuditReader instance, with which queries can be diff --git a/envers/src/main/java/org/hibernate/envers/reader/AuditReaderImpl.java b/envers/src/main/java/org/hibernate/envers/reader/AuditReaderImpl.java index 19fb1cf9e4..7f376b9821 100644 --- a/envers/src/main/java/org/hibernate/envers/reader/AuditReaderImpl.java +++ b/envers/src/main/java/org/hibernate/envers/reader/AuditReaderImpl.java @@ -34,10 +34,12 @@ import org.hibernate.envers.exception.AuditException; import org.hibernate.envers.query.AuditEntity; import static org.hibernate.envers.tools.ArgumentsTools.checkNotNull; import static org.hibernate.envers.tools.ArgumentsTools.checkPositive; +import org.hibernate.envers.synchronization.AuditSync; import org.hibernate.NonUniqueResultException; import org.hibernate.Query; import org.hibernate.Session; +import org.hibernate.event.EventSource; import org.hibernate.engine.SessionImplementor; import org.jboss.envers.query.VersionsQueryCreator; @@ -190,7 +192,20 @@ public class AuditReaderImpl implements AuditReaderImplementor { } } - public VersionsQueryCreator createQuery() { + @SuppressWarnings({"unchecked"}) + public T getCurrentRevision(Class revisionEntityClass, boolean persist) { + if (!(session instanceof EventSource)) { + throw new IllegalArgumentException("The provided session is not an EventSource!"); + } + + // Obtaining the current audit sync + AuditSync auditSync = verCfg.getSyncManager().get((EventSource) session); + + // And getting the current revision data + return (T) auditSync.getCurrentRevisionData(session, persist); + } + + public VersionsQueryCreator createQuery() { return new VersionsQueryCreator(verCfg, this); } } diff --git a/envers/src/main/java/org/hibernate/envers/revisioninfo/DefaultRevisionInfoGenerator.java b/envers/src/main/java/org/hibernate/envers/revisioninfo/DefaultRevisionInfoGenerator.java index aeab30b7de..b33be8018b 100644 --- a/envers/src/main/java/org/hibernate/envers/revisioninfo/DefaultRevisionInfoGenerator.java +++ b/envers/src/main/java/org/hibernate/envers/revisioninfo/DefaultRevisionInfoGenerator.java @@ -63,14 +63,18 @@ public class DefaultRevisionInfoGenerator implements RevisionInfoGenerator { } } - private Object newRevision() { - Object revisionInfo; + public void saveRevisionData(Session session, Object revisionData) { + session.save(revisionInfoEntityName, revisionData); + } + + public Object generate() { + Object revisionInfo; try { revisionInfo = revisionInfoClass.newInstance(); } catch (Exception e) { throw new RuntimeException(e); } - + revisionTimestampSetter.set(revisionInfo, System.currentTimeMillis(), null); if (listener != null) { @@ -79,10 +83,4 @@ public class DefaultRevisionInfoGenerator implements RevisionInfoGenerator { return revisionInfo; } - - public Object generate(Session session) { - Object revisionData = newRevision(); - session.save(revisionInfoEntityName, revisionData); - return revisionData; - } } diff --git a/envers/src/main/java/org/hibernate/envers/revisioninfo/RevisionInfoGenerator.java b/envers/src/main/java/org/hibernate/envers/revisioninfo/RevisionInfoGenerator.java index c99c164ed5..6e5c9b6568 100644 --- a/envers/src/main/java/org/hibernate/envers/revisioninfo/RevisionInfoGenerator.java +++ b/envers/src/main/java/org/hibernate/envers/revisioninfo/RevisionInfoGenerator.java @@ -29,5 +29,6 @@ import org.hibernate.Session; * @author Adam Warski (adam at warski dot org) */ public interface RevisionInfoGenerator { - Object generate(Session session); + void saveRevisionData(Session session, Object revisionData); + Object generate(); } diff --git a/envers/src/main/java/org/hibernate/envers/synchronization/AuditSync.java b/envers/src/main/java/org/hibernate/envers/synchronization/AuditSync.java index 8a12f15b55..8a0d815d38 100644 --- a/envers/src/main/java/org/hibernate/envers/synchronization/AuditSync.java +++ b/envers/src/main/java/org/hibernate/envers/synchronization/AuditSync.java @@ -111,9 +111,8 @@ public class AuditSync implements Synchronization { } private void executeInSession(Session session) { - if (revisionData == null) { - revisionData = revisionInfoGenerator.generate(session); - } + // Making sure the revision data is persisted. + getCurrentRevisionData(session, true); AuditWorkUnit vwu; @@ -127,6 +126,20 @@ public class AuditSync implements Synchronization { } } + public Object getCurrentRevisionData(Session session, boolean persist) { + // Generating the revision data if not yet generated + if (revisionData == null) { + revisionData = revisionInfoGenerator.generate(); + } + + // Saving the revision data, if not yet saved and persist is true + if (!session.contains(revisionData) && persist) { + revisionInfoGenerator.saveRevisionData(session, revisionData); + } + + return revisionData; + } + public void beforeCompletion() { if (workUnits.size() == 0 && undoQueue.size() == 0) { return; diff --git a/envers/src/test/java/org/hibernate/envers/test/entities/reventity/CustomDataRevEntity.java b/envers/src/test/java/org/hibernate/envers/test/entities/reventity/CustomDataRevEntity.java new file mode 100644 index 0000000000..906432cd7b --- /dev/null +++ b/envers/src/test/java/org/hibernate/envers/test/entities/reventity/CustomDataRevEntity.java @@ -0,0 +1,95 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Middleware LLC. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.envers.test.entities.reventity; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; + +import org.hibernate.envers.RevisionEntity; +import org.hibernate.envers.RevisionNumber; +import org.hibernate.envers.RevisionTimestamp; + +/** + * @author Adam Warski (adam at warski dot org) + */ +@Entity +@RevisionEntity +public class CustomDataRevEntity { + @Id + @GeneratedValue + @RevisionNumber + private int customId; + + @RevisionTimestamp + private long customTimestamp; + + private String data; + + public int getCustomId() { + return customId; + } + + public void setCustomId(int customId) { + this.customId = customId; + } + + public long getCustomTimestamp() { + return customTimestamp; + } + + public void setCustomTimestamp(long customTimestamp) { + this.customTimestamp = customTimestamp; + } + + public String getData() { + return data; + } + + public void setData(String data) { + this.data = data; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof CustomDataRevEntity)) return false; + + CustomDataRevEntity that = (CustomDataRevEntity) o; + + if (customId != that.customId) return false; + if (customTimestamp != that.customTimestamp) return false; + if (data != null ? !data.equals(that.data) : that.data != null) return false; + + return true; + } + + @Override + public int hashCode() { + int result = customId; + result = 31 * result + (int) (customTimestamp ^ (customTimestamp >>> 32)); + result = 31 * result + (data != null ? data.hashCode() : 0); + return result; + } +} \ No newline at end of file diff --git a/envers/src/test/java/org/hibernate/envers/test/integration/reventity/CustomNoListener.java b/envers/src/test/java/org/hibernate/envers/test/integration/reventity/CustomNoListener.java new file mode 100644 index 0000000000..d83ba26de1 --- /dev/null +++ b/envers/src/test/java/org/hibernate/envers/test/integration/reventity/CustomNoListener.java @@ -0,0 +1,137 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Middleware LLC. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.envers.test.integration.reventity; + +import java.util.Arrays; +import java.util.Date; +import javax.persistence.EntityManager; + +import org.hibernate.envers.AuditReader; +import org.hibernate.envers.exception.RevisionDoesNotExistException; +import org.hibernate.envers.test.AbstractEntityTest; +import org.hibernate.envers.test.entities.StrTestEntity; +import org.hibernate.envers.test.entities.reventity.CustomDataRevEntity; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +import org.hibernate.ejb.Ejb3Configuration; + +/** + * @author Adam Warski (adam at warski dot org) + */ +public class CustomNoListener extends AbstractEntityTest { + private Integer id; + + public void configure(Ejb3Configuration cfg) { + cfg.addAnnotatedClass(StrTestEntity.class); + cfg.addAnnotatedClass(CustomDataRevEntity.class); + } + + @BeforeClass(dependsOnMethods = "init") + public void initData() throws InterruptedException { + EntityManager em = getEntityManager(); + + // Revision 1 + em.getTransaction().begin(); + StrTestEntity te = new StrTestEntity("x"); + em.persist(te); + id = te.getId(); + + // Setting the data on the revision entity + CustomDataRevEntity custom = getAuditReader().getCurrentRevision(CustomDataRevEntity.class, false); + custom.setData("data1"); + + em.getTransaction().commit(); + + // Revision 2 + em.getTransaction().begin(); + te = em.find(StrTestEntity.class, id); + te.setStr("y"); + + // Setting the data on the revision entity + custom = getAuditReader().getCurrentRevision(CustomDataRevEntity.class, false); + custom.setData("data2"); + + em.getTransaction().commit(); + + // Revision 3 - no changes, but rev entity should be persisted + em.getTransaction().begin(); + + // Setting the data on the revision entity + custom = getAuditReader().getCurrentRevision(CustomDataRevEntity.class, true); + custom.setData("data3"); + + em.getTransaction().commit(); + + // No changes, rev entity won't be persisted + em.getTransaction().begin(); + + // Setting the data on the revision entity + custom = getAuditReader().getCurrentRevision(CustomDataRevEntity.class, false); + custom.setData("data4"); + + em.getTransaction().commit(); + + // Revision 4 + em.getTransaction().begin(); + te = em.find(StrTestEntity.class, id); + te.setStr("z"); + + // Setting the data on the revision entity + custom = getAuditReader().getCurrentRevision(CustomDataRevEntity.class, false); + custom.setData("data5"); + + custom = getAuditReader().getCurrentRevision(CustomDataRevEntity.class, false); + custom.setData("data5bis"); + + em.getTransaction().commit(); + } + + @Test + public void testFindRevision() { + AuditReader vr = getAuditReader(); + + assert "data1".equals(vr.findRevision(CustomDataRevEntity.class, 1).getData()); + assert "data2".equals(vr.findRevision(CustomDataRevEntity.class, 2).getData()); + assert "data3".equals(vr.findRevision(CustomDataRevEntity.class, 3).getData()); + assert "data5bis".equals(vr.findRevision(CustomDataRevEntity.class, 4).getData()); + } + + @Test + public void testRevisionsCounts() { + assert Arrays.asList(1, 2, 4).equals(getAuditReader().getRevisions(StrTestEntity.class, id)); + } + + @Test + public void testHistoryOfId1() { + StrTestEntity ver1 = new StrTestEntity("x", id); + StrTestEntity ver2 = new StrTestEntity("y", id); + StrTestEntity ver3 = new StrTestEntity("z", id); + + assert getAuditReader().find(StrTestEntity.class, id, 1).equals(ver1); + assert getAuditReader().find(StrTestEntity.class, id, 2).equals(ver2); + assert getAuditReader().find(StrTestEntity.class, id, 3).equals(ver2); + assert getAuditReader().find(StrTestEntity.class, id, 4).equals(ver3); + } +} \ No newline at end of file