Merge branch 'master' of github.com:hibernate/hibernate-core

This commit is contained in:
Strong Liu 2010-11-01 12:21:56 +08:00
commit 54da3d5282
21 changed files with 1285 additions and 170 deletions

View File

@ -59,6 +59,7 @@
import org.hibernate.engine.SessionImplementor;
import org.hibernate.event.EventListeners;
import org.hibernate.stat.Statistics;
import org.hibernate.testing.junit.FailureExpected;
import org.hibernate.util.ConfigHelper;
/**
@ -72,7 +73,7 @@
*/
@SuppressWarnings("unchecked")
public class PackagedEntityManagerTest extends PackagingTestCase {
@FailureExpected(jiraKey = "HHH-5693")
public void testDefaultPar() throws Exception {
File testPackage = buildDefaultPar();
addPackageToClasspath( testPackage );
@ -107,6 +108,7 @@ public void testDefaultPar() throws Exception {
emf.close();
}
@FailureExpected(jiraKey = "HHH-5693")
public void testDefaultParForPersistence_1_0() throws Exception {
File testPackage = buildDefaultPar_1_0();
addPackageToClasspath( testPackage );
@ -179,6 +181,7 @@ public void testListenersDefaultPar() throws Exception {
emf.close();
}
@FailureExpected(jiraKey = "HHH-5693")
public void testExplodedPar() throws Exception {
File testPackage = buildExplodedPar();
addPackageToClasspath( testPackage );
@ -334,7 +337,7 @@ public void testExtendedEntityManager() throws Exception {
assertTrue( em.contains( item ) );
em.getTransaction().begin();
Item item1 = ( Item ) em.createQuery( "select i from Item i where descr like 'M%'" ).getSingleResult();
Item item1 = (Item) em.createQuery( "select i from Item i where descr like 'M%'" ).getSingleResult();
assertNotNull( item1 );
assertSame( item, item1 );
item.setDescr( "Micro$oft wireless mouse" );
@ -353,7 +356,7 @@ public void testExtendedEntityManager() throws Exception {
assertSame( item, item1 );
assertTrue( em.contains( item ) );
item1 = ( Item ) em.createQuery( "select i from Item i where descr like 'M%'" ).getSingleResult();
item1 = (Item) em.createQuery( "select i from Item i where descr like 'M%'" ).getSingleResult();
assertNotNull( item1 );
assertSame( item, item1 );
assertTrue( em.contains( item ) );
@ -378,7 +381,7 @@ public void testConfiguration() throws Exception {
res.setName( "Bruce" );
item.setDistributors( new HashSet<Distributor>() );
item.getDistributors().add( res );
Statistics stats = ( ( HibernateEntityManagerFactory ) emf ).getSessionFactory().getStatistics();
Statistics stats = ( (HibernateEntityManagerFactory) emf ).getSessionFactory().getStatistics();
stats.clear();
stats.setStatisticsEnabled( true );

View File

@ -25,7 +25,6 @@
import java.util.Arrays;
import java.util.List;
import junit.framework.TestCase;
import org.jboss.shrinkwrap.api.ArchivePath;
import org.jboss.shrinkwrap.api.ArchivePaths;
import org.jboss.shrinkwrap.api.Archives;
@ -59,6 +58,7 @@
import org.hibernate.ejb.test.pack.spacepar.Bug;
import org.hibernate.ejb.test.pack.various.Airplane;
import org.hibernate.ejb.test.pack.various.Seat;
import org.hibernate.test.annotations.TestCase;
/**
* @author Hardy Ferentschik
@ -77,10 +77,10 @@ public abstract class PackagingTestCase extends TestCase {
// this is assuming that there is a target directory
int index = myUrl.getFile().lastIndexOf( "target" );
if ( index == -1 ) {
fail("Unable to setup packaging test");
fail( "Unable to setup packaging test" );
}
String baseDirPath = myUrl.getFile().substring( 0, index);
String baseDirPath = myUrl.getFile().substring( 0, index );
File baseDir = new File( baseDirPath );
File testPackagesDir = new File( baseDir, "target/bundles" );
@ -366,6 +366,11 @@ protected File buildNestedEarDir(File includeFile) {
archive.as( ExplodedExporter.class ).exportExploded( packageTargetDir );
return testPackage;
}
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[0];
}
}

View File

@ -3,6 +3,7 @@
/**
* Implementations hold other audited properties.
* @author Adam Warski (adam at warski dot org)
* @author Hern&aacut;n Chanfreau
*/
public interface AuditedPropertiesHolder {
/**
@ -17,4 +18,16 @@ public interface AuditedPropertiesHolder {
* @return Auditing data for the property.
*/
PropertyAuditingData getPropertyAuditingData(String propertyName);
/**
* @return true if the holder contains any audited property
*/
boolean isEmpty();
/**
* @return true if the holder contains the given audited property
*/
boolean contains(String propertyName);
}

View File

@ -30,9 +30,10 @@
* filling all the auditing data.
* @author Adam Warski (adam at warski dot org)
* @author Erik-Berndt Scheper
* @author Hern&aacut;n Chanfreau
*/
public class AuditedPropertiesReader {
private final ModificationStore defaultStore;
protected final ModificationStore defaultStore;
private final PersistentPropertiesSource persistentPropertiesSource;
private final AuditedPropertiesHolder auditedPropertiesHolder;
private final GlobalConfiguration globalCfg;
@ -68,7 +69,7 @@ public void read() {
}
private void readPersistentPropertiesAccess() {
Iterator propertyIter = persistentPropertiesSource.getPropertyIterator();
Iterator<Property> propertyIter = persistentPropertiesSource.getPropertyIterator();
while (propertyIter.hasNext()) {
Property property = (Property) propertyIter.next();
if ("field".equals(property.getPropertyAccessorName())) {
@ -80,49 +81,71 @@ private void readPersistentPropertiesAccess() {
}
private void addPropertiesFromClass(XClass clazz) {
XClass superclazz = clazz.getSuperclass();
if (!clazz.isInterface() && !"java.lang.Object".equals(superclazz.getName())) {
addPropertiesFromClass(superclazz);
}
addFromProperties(clazz.getDeclaredProperties("field"), "field", fieldAccessedPersistentProperties);
addFromProperties(clazz.getDeclaredProperties("property"), "property", propertyAccessedPersistentProperties);
}
private void addFromProperties(Iterable<XProperty> properties, String accessType, Set<String> persistentProperties) {
for (XProperty property : properties) {
// If this is not a persistent property, with the same access type as currently checked,
// it's not audited as well.
if (persistentProperties.contains(property.getName())) {
Value propertyValue = persistentPropertiesSource.getProperty(property.getName()).getValue();
PropertyAuditingData propertyData;
boolean isAudited;
if (propertyValue instanceof Component) {
ComponentAuditingData componentData = new ComponentAuditingData();
isAudited = fillPropertyData(property, componentData, accessType);
PersistentPropertiesSource componentPropertiesSource = new ComponentPropertiesSource(
(Component) propertyValue);
new AuditedPropertiesReader(ModificationStore.FULL, componentPropertiesSource, componentData,
globalCfg, reflectionManager,
propertyNamePrefix + MappingTools.createComponentPrefix(property.getName()))
.read();
propertyData = componentData;
} else {
propertyData = new PropertyAuditingData();
isAudited = fillPropertyData(property, propertyData, accessType);
}
if (isAudited) {
// Now we know that the property is audited
auditedPropertiesHolder.addPropertyAuditingData(property.getName(), propertyData);
}
Audited allClassAudited = clazz.getAnnotation(Audited.class);
//look in the class
addFromProperties(clazz.getDeclaredProperties("field"), "field", fieldAccessedPersistentProperties, allClassAudited);
addFromProperties(clazz.getDeclaredProperties("property"), "property", propertyAccessedPersistentProperties, allClassAudited);
if(allClassAudited != null || !auditedPropertiesHolder.isEmpty()) {
XClass superclazz = clazz.getSuperclass();
if (!clazz.isInterface() && !"java.lang.Object".equals(superclazz.getName())) {
addPropertiesFromClass(superclazz);
}
}
}
private void addFromProperties(Iterable<XProperty> properties, String accessType, Set<String> persistentProperties, Audited allClassAudited) {
for (XProperty property : properties) {
// If this is not a persistent property, with the same access type as currently checked,
// it's not audited as well.
// If the property was already defined by the subclass, is ignored by superclasses
if ((persistentProperties.contains(property.getName()) && (!auditedPropertiesHolder
.contains(property.getName())))) {
Value propertyValue = persistentPropertiesSource.getProperty(property.getName()).getValue();
if (propertyValue instanceof Component) {
this.addFromComponentProperty(property, accessType, (Component)propertyValue, allClassAudited);
} else {
this.addFromNotComponentProperty(property, accessType, allClassAudited);
}
}
}
}
private void addFromComponentProperty(XProperty property,
String accessType, Component propertyValue, Audited allClassAudited) {
ComponentAuditingData componentData = new ComponentAuditingData();
boolean isAudited = fillPropertyData(property, componentData, accessType,
allClassAudited);
PersistentPropertiesSource componentPropertiesSource = new ComponentPropertiesSource(
(Component) propertyValue);
ComponentAuditedPropertiesReader audPropReader = new ComponentAuditedPropertiesReader(
ModificationStore.FULL, componentPropertiesSource,
componentData, globalCfg, reflectionManager, propertyNamePrefix
+ MappingTools
.createComponentPrefix(property.getName()));
audPropReader.read();
if (isAudited) {
// Now we know that the property is audited
auditedPropertiesHolder.addPropertyAuditingData(property.getName(),
componentData);
}
}
private void addFromNotComponentProperty(XProperty property, String accessType, Audited allClassAudited){
PropertyAuditingData propertyData = new PropertyAuditingData();
boolean isAudited = fillPropertyData(property, propertyData, accessType, allClassAudited);
if (isAudited) {
// Now we know that the property is audited
auditedPropertiesHolder.addPropertyAuditingData(property.getName(), propertyData);
}
}
/**
* Checks if a property is audited and if yes, fills all of its data.
* @param property Property to check.
@ -131,7 +154,7 @@ private void addFromProperties(Iterable<XProperty> properties, String accessType
* @return False if this property is not audited.
*/
private boolean fillPropertyData(XProperty property, PropertyAuditingData propertyData,
String accessType) {
String accessType, Audited allClassAudited) {
// check if a property is declared as not audited to exclude it
// useful if a class is audited but some properties should be excluded
@ -149,18 +172,11 @@ private boolean fillPropertyData(XProperty property, PropertyAuditingData proper
}
}
// Checking if this property is explicitly audited or if all properties are.
Audited aud = property.getAnnotation(Audited.class);
if (aud != null) {
propertyData.setStore(aud.modStore());
propertyData.setRelationTargetAuditMode(aud.targetAuditMode());
} else {
if (defaultStore != null) {
propertyData.setStore(defaultStore);
} else {
return false;
}
if(!this.checkAudited(property, propertyData, allClassAudited)){
return false;
}
propertyData.setName(propertyNamePrefix + property.getName());
propertyData.setBeanName(property.getName());
@ -177,6 +193,21 @@ private boolean fillPropertyData(XProperty property, PropertyAuditingData proper
return true;
}
protected boolean checkAudited(XProperty property,
PropertyAuditingData propertyData, Audited allClassAudited) {
// Checking if this property is explicitly audited or if all properties are.
Audited aud = (property.isAnnotationPresent(Audited.class)) ? (property.getAnnotation(Audited.class)) : allClassAudited;
//Audited aud = property.getAnnotation(Audited.class);
if (aud != null) {
propertyData.setStore(aud.modStore());
propertyData.setRelationTargetAuditMode(aud.targetAuditMode());
return true;
} else {
return false;
}
}
private void setPropertyAuditMappedBy(XProperty property, PropertyAuditingData propertyData) {
AuditMappedBy auditMappedBy = property.getAnnotation(AuditMappedBy.class);
if (auditMappedBy != null) {

View File

@ -31,6 +31,7 @@
/**
* @author Adam Warski (adam at warski dot org)
* @author Sebastian Komander
* @author Hern&aacut;n Chanfreau
*/
public class ClassAuditingData implements AuditedPropertiesHolder {
private final Map<String, PropertyAuditingData> properties;
@ -49,6 +50,10 @@ public ClassAuditingData() {
secondaryTableDictionary = newHashMap();
}
public boolean isEmpty() {
return properties.isEmpty();
}
public void addPropertyAuditingData(String propertyName, PropertyAuditingData auditingData) {
properties.put(propertyName, auditingData);
}
@ -80,4 +85,8 @@ public void setDefaultAudited(boolean defaultAudited) {
public boolean isAudited() {
return defaultAudited || properties.size() > 0;
}
public boolean contains(String propertyName) {
return properties.containsKey(propertyName);
}
}

View File

@ -0,0 +1,40 @@
package org.hibernate.envers.configuration.metadata.reader;
import org.hibernate.annotations.common.reflection.ReflectionManager;
import org.hibernate.annotations.common.reflection.XProperty;
import org.hibernate.envers.Audited;
import org.hibernate.envers.ModificationStore;
import org.hibernate.envers.configuration.GlobalConfiguration;
/**
* Reads the audited properties for components.
*
* @author Hern&aacut;n Chanfreau
*
*/
public class ComponentAuditedPropertiesReader extends AuditedPropertiesReader {
public ComponentAuditedPropertiesReader(ModificationStore defaultStore,
PersistentPropertiesSource persistentPropertiesSource,
AuditedPropertiesHolder auditedPropertiesHolder,
GlobalConfiguration globalCfg, ReflectionManager reflectionManager,
String propertyNamePrefix) {
super(defaultStore, persistentPropertiesSource, auditedPropertiesHolder,
globalCfg, reflectionManager, propertyNamePrefix);
}
@Override
protected boolean checkAudited(XProperty property,
PropertyAuditingData propertyData, Audited allClassAudited) {
// Checking if this property is explicitly audited or if all properties are.
Audited aud = property.getAnnotation(Audited.class);
if (aud != null) {
propertyData.setStore(aud.modStore());
propertyData.setRelationTargetAuditMode(aud.targetAuditMode());
} else {
propertyData.setStore(ModificationStore.FULL);
}
return true;
}
}

View File

@ -31,6 +31,7 @@
/**
* Audit mapping meta-data for component.
* @author Adam Warski (adam at warski dot org)
* @author Hern&aacut;n Chanfreau
*/
public class ComponentAuditingData extends PropertyAuditingData implements AuditedPropertiesHolder {
private final Map<String, PropertyAuditingData> properties;
@ -39,6 +40,10 @@ public ComponentAuditingData() {
this.properties = newHashMap();
}
public boolean isEmpty() {
return properties.isEmpty();
}
public void addPropertyAuditingData(String propertyName, PropertyAuditingData auditingData) {
properties.put(propertyName, auditingData);
}
@ -46,4 +51,8 @@ public void addPropertyAuditingData(String propertyName, PropertyAuditingData au
public PropertyAuditingData getPropertyAuditingData(String propertyName) {
return properties.get(propertyName);
}
public boolean contains(String propertyName) {
return properties.containsKey(propertyName);
}
}

View File

@ -1,75 +0,0 @@
/*
* 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.superclass;
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 MappedSubclassing extends AbstractEntityTest {
private Integer id1;
public void configure(Ejb3Configuration cfg) {
cfg.addAnnotatedClass(SubclassEntity.class);
}
@BeforeClass(dependsOnMethods = "init")
public void initData() {
// Revision 1
EntityManager em = getEntityManager();
em.getTransaction().begin();
SubclassEntity se1 = new SubclassEntity("x");
em.persist(se1);
id1 = se1.getId();
em.getTransaction().commit();
// Revision 2
em.getTransaction().begin();
se1 = em.find(SubclassEntity.class, id1);
se1.setStr("y");
em.getTransaction().commit();
}
@Test
public void testRevisionsCounts() {
assert Arrays.asList(1, 2).equals(getAuditReader().getRevisions(SubclassEntity.class, id1));
}
@Test
public void testHistoryOfId1() {
SubclassEntity ver1 = new SubclassEntity(id1, "x");
SubclassEntity ver2 = new SubclassEntity(id1, "y");
assert getAuditReader().find(SubclassEntity.class, id1, 1).equals(ver1);
assert getAuditReader().find(SubclassEntity.class, id1, 2).equals(ver2);
}
}

View File

@ -0,0 +1,84 @@
/*
* 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.superclass.auditAtMethodSuperclassLevel;
import javax.persistence.MappedSuperclass;
import org.hibernate.envers.Audited;
/**
* @author Adam Warski (adam at warski dot org)
*
* @author Hern&aacut;n Chanfreau
*/
@MappedSuperclass
public class AuditedMethodMappedSuperclass {
@Audited
private String str;
private String otherStr;
public AuditedMethodMappedSuperclass() {
}
public AuditedMethodMappedSuperclass(String str, String otherStr) {
this.str = str;
this.otherStr = otherStr;
}
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
public String getOtherStr() {
return otherStr;
}
public void setOtherStr(String otherStr) {
this.otherStr = otherStr;
}
public boolean equals(Object o) {
if (this == o)
return true;
if (!(o instanceof AuditedMethodMappedSuperclass))
return false;
AuditedMethodMappedSuperclass that = (AuditedMethodMappedSuperclass) o;
if (str != null ? !str.equals(that.str) : that.str != null)
return false;
return true;
}
public int hashCode() {
return (str != null ? str.hashCode() : 0);
}
}

View File

@ -0,0 +1,96 @@
/*
* 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.superclass.auditAtMethodSuperclassLevel;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
/**
* @author Adam Warski (adam at warski dot org)
*
* @author Hern&aacute;n Chanfreau
*
*/
@Entity
public class NotAuditedSubclassEntity extends AuditedMethodMappedSuperclass {
@Id
@GeneratedValue
private Integer id;
private String notAuditedStr;
public NotAuditedSubclassEntity() {
}
public NotAuditedSubclassEntity(Integer id, String str, String otherStr, String notAuditedStr) {
super(str, otherStr);
this.notAuditedStr = notAuditedStr;
this.id = id;
}
public NotAuditedSubclassEntity(String str, String otherStr, String notAuditedStr) {
super(str, otherStr);
this.notAuditedStr = notAuditedStr;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getNotAuditedStr() {
return notAuditedStr;
}
public void setNotAuditedStr(String notAuditedStr) {
this.notAuditedStr = notAuditedStr;
}
public boolean equals(Object o) {
if (this == o)
return true;
if (!(o instanceof NotAuditedSubclassEntity))
return false;
if (!super.equals(o))
return false;
NotAuditedSubclassEntity that = (NotAuditedSubclassEntity) o;
if (id != null ? !id.equals(that.id) : that.id != null)
return false;
return true;
}
public int hashCode() {
int result = super.hashCode();
result = 31 * result + (id != null ? id.hashCode() : 0);
return result;
}
}

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.superclass.auditAtMethodSuperclassLevel.auditAllSubclass;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import org.hibernate.envers.Audited;
import org.hibernate.envers.test.integration.superclass.auditAtMethodSuperclassLevel.AuditedMethodMappedSuperclass;
/**
* @author Adam Warski (adam at warski dot org)
* @author Hern&aacut;n Chanfreau
*/
@Entity
@Audited
public class AuditedAllSubclassEntity extends AuditedMethodMappedSuperclass {
@Id
@GeneratedValue
private Integer id;
private String subAuditedStr;
public AuditedAllSubclassEntity() {
}
public AuditedAllSubclassEntity(Integer id, String str, String otherString, String subAuditedStr) {
super(str, otherString);
this.subAuditedStr = subAuditedStr;
this.id = id;
}
public AuditedAllSubclassEntity(String str, String otherString, String subAuditedStr ) {
super(str, otherString);
this.subAuditedStr = subAuditedStr;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getSubAuditedStr() {
return subAuditedStr;
}
public void setSubAuditedStr(String subAuditedStr) {
this.subAuditedStr = subAuditedStr;
}
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof AuditedAllSubclassEntity)) return false;
if (!super.equals(o)) return false;
AuditedAllSubclassEntity that = (AuditedAllSubclassEntity) o;
if (id != null ? !id.equals(that.id) : that.id != null) return false;
return true;
}
public int hashCode() {
int result = super.hashCode();
result = 31 * result + (id != null ? id.hashCode() : 0);
return result;
}
}

View File

@ -0,0 +1,121 @@
/*
* 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.superclass.auditAtMethodSuperclassLevel.auditAllSubclass;
import java.util.Arrays;
import javax.persistence.EntityManager;
import org.hibernate.ejb.Ejb3Configuration;
import org.hibernate.envers.exception.NotAuditedException;
import org.hibernate.envers.test.AbstractEntityTest;
import org.hibernate.envers.test.integration.superclass.auditAtMethodSuperclassLevel.AuditedMethodMappedSuperclass;
import org.hibernate.envers.test.integration.superclass.auditAtMethodSuperclassLevel.NotAuditedSubclassEntity;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
/**
* @author Adam Warski (adam at warski dot org)
*
* @author Hern&aacut;n Chanfreau
*
*/
public class MappedSubclassingAllAuditedTest extends AbstractEntityTest {
private Integer id2_1;
private Integer id1_1;
public void configure(Ejb3Configuration cfg) {
cfg.addAnnotatedClass(AuditedMethodMappedSuperclass.class);
cfg.addAnnotatedClass(AuditedAllSubclassEntity.class);
cfg.addAnnotatedClass(NotAuditedSubclassEntity.class);
}
@BeforeClass(dependsOnMethods = "init")
public void initData() {
// Revision 1
EntityManager em = getEntityManager();
em.getTransaction().begin();
NotAuditedSubclassEntity nas = new NotAuditedSubclassEntity("nae","super str","not audited str");
em.persist(nas);
AuditedAllSubclassEntity ae = new AuditedAllSubclassEntity("ae", "super str", "audited str");
em.persist(ae);
id1_1 = ae.getId();
id2_1 = nas.getId();
em.getTransaction().commit();
// Revision 2
em.getTransaction().begin();
ae = em.find(AuditedAllSubclassEntity.class, id1_1);
ae.setStr("ae new");
ae.setSubAuditedStr("audited str new");
nas = em.find(NotAuditedSubclassEntity.class, id2_1);
nas.setStr("nae new");
nas.setNotAuditedStr("not aud str new");
em.getTransaction().commit();
}
@Test
public void testRevisionsCountsForAudited() {
assert Arrays.asList(1, 2).equals(
getAuditReader().getRevisions(AuditedAllSubclassEntity.class, id1_1));
}
@Test(expectedExceptions={ NotAuditedException.class })
public void testRevisionsCountsForNotAudited() {
try {
getAuditReader().getRevisions(NotAuditedSubclassEntity.class, id2_1);
assert(false);
} catch (NotAuditedException nae) {
throw nae;
}
}
@Test
public void testHistoryOfAudited() {
AuditedAllSubclassEntity ver1 = new AuditedAllSubclassEntity(id1_1, "ae", "super str", "audited str");
AuditedAllSubclassEntity ver2 = new AuditedAllSubclassEntity(id1_1, "ae new", "super str", "audited str new");
AuditedAllSubclassEntity rev1 = getAuditReader().find(AuditedAllSubclassEntity.class, id1_1, 1);
AuditedAllSubclassEntity rev2 = getAuditReader().find(AuditedAllSubclassEntity.class, id1_1, 2);
//this property is not audited on superclass
assert(rev1.getOtherStr() == null);
assert(rev2.getOtherStr() == null);
assert rev1.equals(ver1);
assert rev2.equals(ver2);
}
@Test(expectedExceptions={ NotAuditedException.class })
public void testHistoryOfNotAudited() {
try {
getAuditReader().find(NotAuditedSubclassEntity.class, id2_1, 1);
assert(false);
} catch (NotAuditedException nae) {
throw nae;
}
}
}

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.superclass.auditAtMethodSuperclassLevel.auditMethodSubclass;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import org.hibernate.envers.Audited;
import org.hibernate.envers.test.integration.superclass.auditAtMethodSuperclassLevel.AuditedMethodMappedSuperclass;
/**
* @author Adam Warski (adam at warski dot org)
* @author Hern&aacut;n Chanfreau
*/
@Entity
public class AuditedMethodSubclassEntity extends AuditedMethodMappedSuperclass {
@Id
@GeneratedValue
private Integer id;
@Audited
private String subAuditedStr;
public AuditedMethodSubclassEntity() {
}
public AuditedMethodSubclassEntity(Integer id, String str, String otherString, String subAuditedStr) {
super(str, otherString);
this.subAuditedStr = subAuditedStr;
this.id = id;
}
public AuditedMethodSubclassEntity(String str, String otherString, String subAuditedStr ) {
super(str, otherString);
this.subAuditedStr = subAuditedStr;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getSubAuditedStr() {
return subAuditedStr;
}
public void setSubAuditedStr(String subAuditedStr) {
this.subAuditedStr = subAuditedStr;
}
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof AuditedMethodSubclassEntity)) return false;
if (!super.equals(o)) return false;
AuditedMethodSubclassEntity that = (AuditedMethodSubclassEntity) o;
if (id != null ? !id.equals(that.id) : that.id != null) return false;
return true;
}
public int hashCode() {
int result = super.hashCode();
result = 31 * result + (id != null ? id.hashCode() : 0);
return result;
}
}

View File

@ -0,0 +1,121 @@
/*
* 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.superclass.auditAtMethodSuperclassLevel.auditMethodSubclass;
import java.util.Arrays;
import javax.persistence.EntityManager;
import org.hibernate.ejb.Ejb3Configuration;
import org.hibernate.envers.exception.NotAuditedException;
import org.hibernate.envers.test.AbstractEntityTest;
import org.hibernate.envers.test.integration.superclass.auditAtMethodSuperclassLevel.AuditedMethodMappedSuperclass;
import org.hibernate.envers.test.integration.superclass.auditAtMethodSuperclassLevel.NotAuditedSubclassEntity;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
/**
* @author Adam Warski (adam at warski dot org)
*
* @author Hern&aacut;n Chanfreau
*
*/
public class MappedSubclassingMethodAuditedTest extends AbstractEntityTest {
private Integer id2_1;
private Integer id1_1;
public void configure(Ejb3Configuration cfg) {
cfg.addAnnotatedClass(AuditedMethodMappedSuperclass.class);
cfg.addAnnotatedClass(AuditedMethodSubclassEntity.class);
cfg.addAnnotatedClass(NotAuditedSubclassEntity.class);
}
@BeforeClass(dependsOnMethods = "init")
public void initData() {
// Revision 1
EntityManager em = getEntityManager();
em.getTransaction().begin();
NotAuditedSubclassEntity nas = new NotAuditedSubclassEntity("nae","super str","not audited str");
em.persist(nas);
AuditedMethodSubclassEntity ae = new AuditedMethodSubclassEntity("ae", "super str", "audited str");
em.persist(ae);
id1_1 = ae.getId();
id2_1 = nas.getId();
em.getTransaction().commit();
// Revision 2
em.getTransaction().begin();
ae = em.find(AuditedMethodSubclassEntity.class, id1_1);
ae.setStr("ae new");
ae.setSubAuditedStr("audited str new");
nas = em.find(NotAuditedSubclassEntity.class, id2_1);
nas.setStr("nae new");
nas.setNotAuditedStr("not aud str new");
em.getTransaction().commit();
}
@Test
public void testRevisionsCountsForAudited() {
assert Arrays.asList(1, 2).equals(
getAuditReader().getRevisions(AuditedMethodSubclassEntity.class, id1_1));
}
@Test(expectedExceptions={ NotAuditedException.class })
public void testRevisionsCountsForNotAudited() {
try {
getAuditReader().getRevisions(NotAuditedSubclassEntity.class, id2_1);
assert(false);
} catch (NotAuditedException nae) {
throw nae;
}
}
@Test
public void testHistoryOfAudited() {
AuditedMethodSubclassEntity ver1 = new AuditedMethodSubclassEntity(id1_1, "ae", "super str", "audited str");
AuditedMethodSubclassEntity ver2 = new AuditedMethodSubclassEntity(id1_1, "ae new", "super str", "audited str new");
AuditedMethodSubclassEntity rev1 = getAuditReader().find(AuditedMethodSubclassEntity.class, id1_1, 1);
AuditedMethodSubclassEntity rev2 = getAuditReader().find(AuditedMethodSubclassEntity.class, id1_1, 2);
//this property is not audited on superclass
assert(rev1.getOtherStr() == null);
assert(rev2.getOtherStr() == null);
assert rev1.equals(ver1);
assert rev2.equals(ver2);
}
@Test(expectedExceptions={ NotAuditedException.class })
public void testHistoryOfNotAudited() {
try {
getAuditReader().find(NotAuditedSubclassEntity.class, id2_1, 1);
assert(false);
} catch (NotAuditedException nae) {
throw nae;
}
}
}

View File

@ -21,7 +21,7 @@
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.envers.test.integration.superclass;
package org.hibernate.envers.test.integration.superclass.auditedAtSuperclassLevel;
import javax.persistence.MappedSuperclass;
@ -29,39 +29,56 @@
/**
* @author Adam Warski (adam at warski dot org)
*
* @author Hern&aacut;n Chanfreau
*/
@MappedSuperclass
public class SuperclassOfEntity {
@Audited
private String str;
@Audited
public class AuditedAllMappedSuperclass {
public SuperclassOfEntity() {
}
private String str;
private String otherStr;
public SuperclassOfEntity(String str) {
this.str = str;
}
public AuditedAllMappedSuperclass() {
}
public String getStr() {
return str;
}
public AuditedAllMappedSuperclass(String str, String otherStr) {
this.str = str;
this.otherStr = otherStr;
}
public void setStr(String str) {
this.str = str;
}
public String getStr() {
return str;
}
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof SuperclassOfEntity)) return false;
public void setStr(String str) {
this.str = str;
}
SuperclassOfEntity that = (SuperclassOfEntity) o;
public String getOtherStr() {
return otherStr;
}
if (str != null ? !str.equals(that.str) : that.str != null) return false;
public void setOtherStr(String otherStr) {
this.otherStr = otherStr;
}
return true;
}
public boolean equals(Object o) {
if (this == o)
return true;
if (!(o instanceof AuditedAllMappedSuperclass))
return false;
public int hashCode() {
return (str != null ? str.hashCode() : 0);
}
AuditedAllMappedSuperclass that = (AuditedAllMappedSuperclass) o;
if (str != null ? !str.equals(that.str) : that.str != null)
return false;
return true;
}
public int hashCode() {
return (str != null ? str.hashCode() : 0);
}
}

View File

@ -0,0 +1,96 @@
/*
* 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.superclass.auditedAtSuperclassLevel;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
/**
* @author Adam Warski (adam at warski dot org)
*
* @author Hern&aacute;n Chanfreau
*
*/
@Entity
public class NotAuditedSubclassEntity extends AuditedAllMappedSuperclass {
@Id
@GeneratedValue
private Integer id;
private String notAuditedStr;
public NotAuditedSubclassEntity() {
}
public NotAuditedSubclassEntity(Integer id, String str, String otherStr, String notAuditedStr) {
super(str, otherStr);
this.notAuditedStr = notAuditedStr;
this.id = id;
}
public NotAuditedSubclassEntity(String str, String otherStr, String notAuditedStr) {
super(str, otherStr);
this.notAuditedStr = notAuditedStr;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getNotAuditedStr() {
return notAuditedStr;
}
public void setNotAuditedStr(String notAuditedStr) {
this.notAuditedStr = notAuditedStr;
}
public boolean equals(Object o) {
if (this == o)
return true;
if (!(o instanceof NotAuditedSubclassEntity))
return false;
if (!super.equals(o))
return false;
NotAuditedSubclassEntity that = (NotAuditedSubclassEntity) o;
if (id != null ? !id.equals(that.id) : that.id != null)
return false;
return true;
}
public int hashCode() {
int result = super.hashCode();
result = 31 * result + (id != null ? id.hashCode() : 0);
return result;
}
}

View File

@ -21,31 +21,41 @@
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.envers.test.integration.superclass;
package org.hibernate.envers.test.integration.superclass.auditedAtSuperclassLevel.auditAllSubclass;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import org.hibernate.envers.Audited;
import org.hibernate.envers.test.integration.superclass.auditedAtSuperclassLevel.AuditedAllMappedSuperclass;
/**
* @author Adam Warski (adam at warski dot org)
* @author Hern&aacut;n Chanfreau
*/
@Entity
public class SubclassEntity extends SuperclassOfEntity {
@Audited
public class AuditedAllSubclassEntity extends AuditedAllMappedSuperclass {
@Id
@GeneratedValue
private Integer id;
private String subAuditedStr;
public SubclassEntity() {
public AuditedAllSubclassEntity() {
}
public SubclassEntity(Integer id, String str) {
super(str);
public AuditedAllSubclassEntity(Integer id, String str, String otherString, String subAuditedStr) {
super(str, otherString);
this.subAuditedStr = subAuditedStr;
this.id = id;
}
public SubclassEntity(String str) {
super(str);
public AuditedAllSubclassEntity(String str, String otherString, String subAuditedStr ) {
super(str, otherString);
this.subAuditedStr = subAuditedStr;
}
public Integer getId() {
@ -56,12 +66,20 @@ public void setId(Integer id) {
this.id = id;
}
public String getSubAuditedStr() {
return subAuditedStr;
}
public void setSubAuditedStr(String subAuditedStr) {
this.subAuditedStr = subAuditedStr;
}
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof SubclassEntity)) return false;
if (!(o instanceof AuditedAllSubclassEntity)) return false;
if (!super.equals(o)) return false;
SubclassEntity that = (SubclassEntity) o;
AuditedAllSubclassEntity that = (AuditedAllSubclassEntity) o;
if (id != null ? !id.equals(that.id) : that.id != null) return false;

View File

@ -0,0 +1,120 @@
/*
* 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.superclass.auditedAtSuperclassLevel.auditAllSubclass;
import java.util.Arrays;
import javax.persistence.EntityManager;
import org.hibernate.ejb.Ejb3Configuration;
import org.hibernate.envers.exception.NotAuditedException;
import org.hibernate.envers.test.AbstractEntityTest;
import org.hibernate.envers.test.integration.superclass.auditedAtSuperclassLevel.AuditedAllMappedSuperclass;
import org.hibernate.envers.test.integration.superclass.auditedAtSuperclassLevel.NotAuditedSubclassEntity;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
/**
* @author Adam Warski (adam at warski dot org)
*
* @author Hern&aacut;n Chanfreau
*
*/
public class MappedSubclassingAllAuditedTest extends AbstractEntityTest {
private Integer id2_1;
private Integer id1_1;
public void configure(Ejb3Configuration cfg) {
cfg.addAnnotatedClass(AuditedAllMappedSuperclass.class);
cfg.addAnnotatedClass(AuditedAllSubclassEntity.class);
cfg.addAnnotatedClass(NotAuditedSubclassEntity.class);
}
@BeforeClass(dependsOnMethods = "init")
public void initData() {
// Revision 1
EntityManager em = getEntityManager();
em.getTransaction().begin();
NotAuditedSubclassEntity nas = new NotAuditedSubclassEntity("nae","super str","not audited str");
em.persist(nas);
AuditedAllSubclassEntity ae = new AuditedAllSubclassEntity("ae", "super str", "audited str");
em.persist(ae);
id1_1 = ae.getId();
id2_1 = nas.getId();
em.getTransaction().commit();
// Revision 2
em.getTransaction().begin();
ae = em.find(AuditedAllSubclassEntity.class, id1_1);
ae.setStr("ae new");
ae.setSubAuditedStr("audited str new");
nas = em.find(NotAuditedSubclassEntity.class, id2_1);
nas.setStr("nae new");
nas.setNotAuditedStr("not aud str new");
em.getTransaction().commit();
}
@Test
public void testRevisionsCountsForAudited() {
assert Arrays.asList(1, 2).equals(
getAuditReader().getRevisions(AuditedAllSubclassEntity.class, id1_1));
}
@Test(expectedExceptions={ NotAuditedException.class })
public void testRevisionsCountsForNotAudited() {
try {
getAuditReader().getRevisions(NotAuditedSubclassEntity.class, id2_1);
assert(false);
} catch (NotAuditedException nae) {
throw nae;
}
}
@Test
public void testHistoryOfAudited() {
AuditedAllSubclassEntity ver1 = new AuditedAllSubclassEntity(id1_1, "ae", "super str", "audited str");
AuditedAllSubclassEntity ver2 = new AuditedAllSubclassEntity(id1_1, "ae new", "super str", "audited str new");
AuditedAllSubclassEntity rev1 = getAuditReader().find(AuditedAllSubclassEntity.class, id1_1, 1);
AuditedAllSubclassEntity rev2 = getAuditReader().find(AuditedAllSubclassEntity.class, id1_1, 2);
assert(rev1.getOtherStr() != null);
assert(rev2.getOtherStr() != null);
assert rev1.equals(ver1);
assert rev2.equals(ver2);
}
@Test(expectedExceptions={ NotAuditedException.class })
public void testHistoryOfNotAudited() {
try {
getAuditReader().find(NotAuditedSubclassEntity.class, id2_1, 1);
assert(false);
} catch (NotAuditedException nae) {
throw nae;
}
}
}

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.superclass.auditedAtSuperclassLevel.auditMethodSubclass;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import org.hibernate.envers.Audited;
import org.hibernate.envers.test.integration.superclass.auditedAtSuperclassLevel.AuditedAllMappedSuperclass;
/**
* @author Adam Warski (adam at warski dot org)
* @author Hern&aacut;n Chanfreau
*/
@Entity
public class AuditedMethodSubclassEntity extends AuditedAllMappedSuperclass {
@Id
@GeneratedValue
private Integer id;
@Audited
private String subAuditedStr;
public AuditedMethodSubclassEntity() {
}
public AuditedMethodSubclassEntity(Integer id, String str, String otherString, String subAuditedStr) {
super(str, otherString);
this.subAuditedStr = subAuditedStr;
this.id = id;
}
public AuditedMethodSubclassEntity(String str, String otherString, String subAuditedStr ) {
super(str, otherString);
this.subAuditedStr = subAuditedStr;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getSubAuditedStr() {
return subAuditedStr;
}
public void setSubAuditedStr(String subAuditedStr) {
this.subAuditedStr = subAuditedStr;
}
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof AuditedMethodSubclassEntity)) return false;
if (!super.equals(o)) return false;
AuditedMethodSubclassEntity that = (AuditedMethodSubclassEntity) o;
if (id != null ? !id.equals(that.id) : that.id != null) return false;
return true;
}
public int hashCode() {
int result = super.hashCode();
result = 31 * result + (id != null ? id.hashCode() : 0);
return result;
}
}

View File

@ -0,0 +1,120 @@
/*
* 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.superclass.auditedAtSuperclassLevel.auditMethodSubclass;
import java.util.Arrays;
import javax.persistence.EntityManager;
import org.hibernate.ejb.Ejb3Configuration;
import org.hibernate.envers.exception.NotAuditedException;
import org.hibernate.envers.test.AbstractEntityTest;
import org.hibernate.envers.test.integration.superclass.auditedAtSuperclassLevel.AuditedAllMappedSuperclass;
import org.hibernate.envers.test.integration.superclass.auditedAtSuperclassLevel.NotAuditedSubclassEntity;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
/**
* @author Adam Warski (adam at warski dot org)
*
* @author Hern&aacut;n Chanfreau
*
*/
public class MappedSubclassingMethodAuditedTest extends AbstractEntityTest {
private Integer id2_1;
private Integer id1_1;
public void configure(Ejb3Configuration cfg) {
cfg.addAnnotatedClass(AuditedAllMappedSuperclass.class);
cfg.addAnnotatedClass(AuditedMethodSubclassEntity.class);
cfg.addAnnotatedClass(NotAuditedSubclassEntity.class);
}
@BeforeClass(dependsOnMethods = "init")
public void initData() {
// Revision 1
EntityManager em = getEntityManager();
em.getTransaction().begin();
NotAuditedSubclassEntity nas = new NotAuditedSubclassEntity("nae","super str","not audited str");
em.persist(nas);
AuditedMethodSubclassEntity ae = new AuditedMethodSubclassEntity("ae", "super str", "audited str");
em.persist(ae);
id1_1 = ae.getId();
id2_1 = nas.getId();
em.getTransaction().commit();
// Revision 2
em.getTransaction().begin();
ae = em.find(AuditedMethodSubclassEntity.class, id1_1);
ae.setStr("ae new");
ae.setSubAuditedStr("audited str new");
nas = em.find(NotAuditedSubclassEntity.class, id2_1);
nas.setStr("nae new");
nas.setNotAuditedStr("not aud str new");
em.getTransaction().commit();
}
@Test
public void testRevisionsCountsForAudited() {
assert Arrays.asList(1, 2).equals(
getAuditReader().getRevisions(AuditedMethodSubclassEntity.class, id1_1));
}
@Test(expectedExceptions={ NotAuditedException.class })
public void testRevisionsCountsForNotAudited() {
try {
getAuditReader().getRevisions(NotAuditedSubclassEntity.class, id2_1);
assert(false);
} catch (NotAuditedException nae) {
throw nae;
}
}
@Test
public void testHistoryOfAudited() {
AuditedMethodSubclassEntity ver1 = new AuditedMethodSubclassEntity(id1_1, "ae", "super str", "audited str");
AuditedMethodSubclassEntity ver2 = new AuditedMethodSubclassEntity(id1_1, "ae new", "super str", "audited str new");
AuditedMethodSubclassEntity rev1 = getAuditReader().find(AuditedMethodSubclassEntity.class, id1_1, 1);
AuditedMethodSubclassEntity rev2 = getAuditReader().find(AuditedMethodSubclassEntity.class, id1_1, 2);
assert(rev1.getOtherStr() != null);
assert(rev2.getOtherStr() != null);
assert rev1.equals(ver1);
assert rev2.equals(ver2);
}
@Test(expectedExceptions={ NotAuditedException.class })
public void testHistoryOfNotAudited() {
try {
getAuditReader().find(NotAuditedSubclassEntity.class, id2_1, 1);
assert(false);
} catch (NotAuditedException nae) {
throw nae;
}
}
}

View File

@ -69,7 +69,12 @@
<package name="org.hibernate.envers.test.integration.secondary.ids" />
<package name="org.hibernate.envers.test.integration.serialization" />
<package name="org.hibernate.envers.test.integration.strategy" />
<package name="org.hibernate.envers.test.integration.superclass" />
<package name="org.hibernate.envers.test.integration.superclass.auditAtMethodSuperclassLevel" />
<package name="org.hibernate.envers.test.integration.superclass.auditAtMethodSuperclassLevel.auditAllSubclass" />
<package name="org.hibernate.envers.test.integration.superclass.auditedAtSuperclassLevel.auditAllSubclass" />
<package name="org.hibernate.envers.test.integration.superclass.auditedAtSuperclassLevel.auditMethodSubclass" />
<package name="org.hibernate.envers.test.integration.entityNames.auditedEntity" />
<package name="org.hibernate.envers.test.integration.entityNames.manyToManyAudited" />
<package name="org.hibernate.envers.test.integration.entityNames.oneToManyAudited" />