HHH-3633: classes can be audited also if they don't contain any properties (w/ test)

Some corrections in the manual

Performance tests

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@15722 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Adam Warski 2008-12-22 19:16:28 +00:00
parent 4e5381eb9f
commit 76c4685117
12 changed files with 264 additions and 12 deletions

View File

@ -51,7 +51,7 @@
<para>
Also, the query interface has changed slightly, mainly in the part for specifying restrictions,
projections and order. Please refer to the API for further details.
projections and order. Please refer to the Javadoc for further details.
</para>
</sect1>
@ -91,7 +91,7 @@
&lt;!-- Envers listeners --&gt;
&lt;property name="org.hibernate.envers.versionsTableSuffix" value="_versions" /&gt;
&lt;property name="org.hibernate.envers.auditTableSuffix" value="_versions" /&gt;
&lt;property name="org.hibernate.envers.revisionFieldName" value="_revision" /&gt;
&lt;property name="org.hibernate.envers.revisionTypeFieldName" value="_rev_type" /&gt;
&lt;!-- other envers properties --&gt;
@ -150,8 +150,6 @@ public class ExampleRevEntity {
@Column(name="revision_timestamp")
private long timestamp;
private String username;
// Getters, setters, equals, hashCode ...
}]]></programlisting>

View File

@ -186,10 +186,12 @@ public final class AnnotationsMetadataReader {
if (defaultAudited != null) {
defaultStore = defaultAudited.modStore();
auditData.setDefaultAudited(true);
} else {
Versioned defaultVersioned = clazz.getAnnotation(Versioned.class);
if (defaultVersioned != null) {
defaultStore = translateModStore(defaultVersioned.modStore());
auditData.setDefaultAudited(true);
}
}
}

View File

@ -41,6 +41,11 @@ public class PersistentClassAuditingData {
private Map<String, PersistentPropertyAuditingData> properties;
private AuditTable auditTable;
private Map<String, String> secondaryTableDictionary;
/**
* True if the class is audited globally (this helps to cover the cases when there are no fields in the class,
* but it's still audited).
*/
private boolean defaultAudited;
public Map<String, PersistentPropertyAuditingData> getProperties() {
return properties;
@ -62,8 +67,12 @@ public class PersistentClassAuditingData {
this.auditTable = auditTable;
}
public boolean isAudited() {
if (properties.size() > 0) {
public void setDefaultAudited(boolean defaultAudited) {
this.defaultAudited = defaultAudited;
}
public boolean isAudited() {
if (defaultAudited || properties.size() > 0) {
return true;
} else {
return false;

View File

@ -49,7 +49,7 @@ public abstract class AbstractEntityTest {
public abstract void configure(Ejb3Configuration cfg);
protected void initListeners() {
private void initListeners() {
AuditEventListener listener = new AuditEventListener();
cfg.getEventListeners().setPostInsertEventListeners(new PostInsertEventListener[] { listener });
cfg.getEventListeners().setPostUpdateEventListeners(new PostUpdateEventListener[] { listener });

View File

@ -0,0 +1,93 @@
/*
* 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.inheritance.joined.emptychild;
import java.util.Arrays;
import javax.persistence.EntityManager;
import org.hibernate.envers.test.AbstractEntityTest;
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 EmptyChildAuditing extends AbstractEntityTest {
private Integer id1;
public void configure(Ejb3Configuration cfg) {
cfg.addAnnotatedClass(EmptyChildEntity.class);
cfg.addAnnotatedClass(ParentEntity.class);
}
@BeforeClass(dependsOnMethods = "init")
public void initData() {
EntityManager em = getEntityManager();
id1 = 1;
// Rev 1
em.getTransaction().begin();
EmptyChildEntity pe = new EmptyChildEntity(id1, "x");
em.persist(pe);
em.getTransaction().commit();
// Rev 2
em.getTransaction().begin();
pe = em.find(EmptyChildEntity.class, id1);
pe.setData("y");
em.getTransaction().commit();
}
@Test
public void testRevisionsCounts() {
assert Arrays.asList(1, 2).equals(getAuditReader().getRevisions(EmptyChildEntity.class, id1));
}
@Test
public void testHistoryOfChildId1() {
EmptyChildEntity ver1 = new EmptyChildEntity(id1, "x");
EmptyChildEntity ver2 = new EmptyChildEntity(id1, "y");
assert getAuditReader().find(EmptyChildEntity.class, id1, 1).equals(ver1);
assert getAuditReader().find(EmptyChildEntity.class, id1, 2).equals(ver2);
assert getAuditReader().find(ParentEntity.class, id1, 1).equals(ver1);
assert getAuditReader().find(ParentEntity.class, id1, 2).equals(ver2);
}
@Test
public void testPolymorphicQuery() {
EmptyChildEntity childVer1 = new EmptyChildEntity(id1, "x");
assert getAuditReader().createQuery().forEntitiesAtRevision(EmptyChildEntity.class, 1).getSingleResult()
.equals(childVer1);
assert getAuditReader().createQuery().forEntitiesAtRevision(ParentEntity.class, 1).getSingleResult()
.equals(childVer1);
}
}

View File

@ -0,0 +1,43 @@
/*
* 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.inheritance.joined.emptychild;
import javax.persistence.Entity;
import org.hibernate.envers.Audited;
/**
* @author Adam Warski (adam at warski dot org)
*/
@Entity
@Audited
public class EmptyChildEntity extends ParentEntity {
public EmptyChildEntity() {
}
public EmptyChildEntity(Integer id, String data) {
super(id, data);
}
}

View File

@ -0,0 +1,94 @@
/*
* 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.inheritance.joined.emptychild;
import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import org.hibernate.envers.Audited;
/**
* @author Adam Warski (adam at warski dot org)
*/
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@Audited
public abstract class ParentEntity {
@Id
private Integer id;
@Basic
private String data;
public ParentEntity() {
}
public ParentEntity(Integer id, String data) {
this.id = id;
this.data = data;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof ParentEntity)) return false;
ParentEntity that = (ParentEntity) o;
if (data != null ? !data.equals(that.data) : that.data != null) return false;
if (id != null ? !id.equals(that.id) : that.id != null) return false;
return true;
}
public int hashCode() {
int result;
result = (id != null ? id.hashCode() : 0);
result = 31 * result + (data != null ? data.hashCode() : 0);
return result;
}
public String toString() {
return "ParentEntity(id = " + getId() + ", data = " + getData() + ")";
}
}

View File

@ -0,0 +1,12 @@
package org.hibernate.envers.test.performance;
import java.io.IOException;
public class AllPerformance {
public static void main(String[] args) throws IOException {
new InsertsPerformance().test(10);
new ComplexInsertPerformance().test(10);
new UpdatesPerformance().test(10);
new InsertsOneTransactionPerformance().test(10);
}
}

View File

@ -45,7 +45,7 @@ public class ComplexInsertPerformance extends AbstractPerformanceTest {
cfg.addAnnotatedClass(ChildEntity2.class);
}
private final static int NUMBER_INSERTS = 100;
private final static int NUMBER_INSERTS = 1000;
private long idCounter = 0;

View File

@ -38,7 +38,7 @@ public class InsertsPerformance extends AbstractPerformanceTest {
cfg.addAnnotatedClass(StrTestEntity.class);
}
private final static int NUMBER_INSERTS = 500;
private final static int NUMBER_INSERTS = 5000;
protected void doTest() {
for (int i=0; i<NUMBER_INSERTS; i++) {

View File

@ -40,7 +40,7 @@ public class UpdatesPerformance extends AbstractPerformanceTest {
cfg.addAnnotatedClass(StrTestEntity.class);
}
private final static int NUMBER_UPDATES = 500;
private final static int NUMBER_UPDATES = 5000;
private final static int NUMBER_ENTITIES = 10;
private Random random = new Random();

View File

@ -15,6 +15,7 @@
<package name="org.hibernate.envers.test.integration.ids" />
<package name="org.hibernate.envers.test.integration.inheritance.joined" />
<package name="org.hibernate.envers.test.integration.inheritance.joined.childrelation" />
<package name="org.hibernate.envers.test.integration.inheritance.joined.emptychild" />
<package name="org.hibernate.envers.test.integration.inheritance.joined.notownedrelation" />
<package name="org.hibernate.envers.test.integration.inheritance.joined.relation" />
<package name="org.hibernate.envers.test.integration.inheritance.single" />