Author: adamw
Date: 2008-08-22 08:03:52 -0400 (Fri, 22 Aug 2008)
New Revision: 121
Added:
trunk/src/main/org/jboss/envers/Unversioned.java
trunk/src/test/org/jboss/envers/test/entities/UnversionedEntity.java
trunk/src/test/org/jboss/envers/test/integration/basic/UnversionedProperty.java
Removed:
trunk/src/main/org/jboss/envers/synchronization/work/BidirectionalCollectionChangeWorkUnit.java
Modified:
trunk/src/main/org/jboss/envers/configuration/metadata/AnnotationsMetadataReader.java
trunk/src/main/org/jboss/envers/configuration/metadata/PersistentClassVersioningData.java
trunk/src/main/org/jboss/envers/configuration/metadata/VersionsMetadataGenerator.java
Log:
ENVERS-39: unversioned properties, test
Added: trunk/src/main/org/jboss/envers/Unversioned.java
===================================================================
--- trunk/src/main/org/jboss/envers/Unversioned.java (rev 0)
+++ trunk/src/main/org/jboss/envers/Unversioned.java 2008-08-22 12:03:52 UTC (rev 121)
@@ -0,0 +1,37 @@
+/*
+ * Envers.
http://www.jboss.org/envers
+ *
+ * Copyright 2008 Red Hat Middleware, LLC. All rights reserved.
+ *
+ * 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, v. 2.1.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT A 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, v.2.1 along with this distribution; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ *
+ * Red Hat Author(s): Adam Warski
+ */
+package org.jboss.envers;
+
+import java.lang.annotation.Target;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+
+/**
+ * When applied to a field, indicates that this field should not be versioned.
+ * @author Sebastian Komander
+ */
+(a)Retention(RetentionPolicy.RUNTIME)
+(a)Target({ElementType.METHOD, ElementType.FIELD})
+public @interface Unversioned {
+
+}
Modified:
trunk/src/main/org/jboss/envers/configuration/metadata/AnnotationsMetadataReader.java
===================================================================
---
trunk/src/main/org/jboss/envers/configuration/metadata/AnnotationsMetadataReader.java 2008-08-21
14:45:59 UTC (rev 120)
+++
trunk/src/main/org/jboss/envers/configuration/metadata/AnnotationsMetadataReader.java 2008-08-22
12:03:52 UTC (rev 121)
@@ -24,40 +24,42 @@
import org.jboss.envers.tools.reflection.YClass;
import org.jboss.envers.tools.reflection.YProperty;
import org.jboss.envers.tools.reflection.YReflectionManager;
-import org.jboss.envers.configuration.metadata.PersistentClassVersioningData;
-import org.jboss.envers.Versioned;
-import org.jboss.envers.VersionsTable;
-import org.jboss.envers.SecondaryVersionsTable;
-import org.jboss.envers.SecondaryVersionsTables;
+import org.jboss.envers.*;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.MappingException;
/**
* A helper class to read versioning meta-data from annotations on persistent classes.
* @author Adam Warski (adam at warski dot org)
+ * @author Sebastian Komander
*/
public class AnnotationsMetadataReader {
- private void addPropertiesFromClass(YClass clazz, PersistentClassVersioningData
versioningData) {
- YClass superclazz = clazz.getSuperclass();
- if (!"java.lang.Object".equals(superclazz.getName())) {
- addPropertiesFromClass(superclazz, versioningData);
- }
-
- for (YProperty property : clazz.getDeclaredProperties("field")) {
+ private void addFromProperties(Iterable<YProperty> properties,
PersistentClassVersioningData versioningData) {
+ for (YProperty property : properties) {
Versioned ver = property.getAnnotation(Versioned.class);
if (ver != null) {
versioningData.propertyStoreInfo.propertyStores.put(property.getName(),
ver.modStore());
}
- }
- for (YProperty property : clazz.getDeclaredProperties("property")) {
- Versioned ver = property.getAnnotation(Versioned.class);
- if (ver != null) {
- versioningData.propertyStoreInfo.propertyStores.put(property.getName(),
ver.modStore());
+ // check if a property is declared as unversioned to exclude it
+ // useful if a class is versioned but some properties should be excluded
+ Unversioned unVer = property.getAnnotation(Unversioned.class);
+ if (unVer != null) {
+ versioningData.unversionedProperties.add(property.getName());
}
}
}
+ private void addPropertiesFromClass(YClass clazz, PersistentClassVersioningData
versioningData) {
+ YClass superclazz = clazz.getSuperclass();
+ if (!"java.lang.Object".equals(superclazz.getName())) {
+ addPropertiesFromClass(superclazz, versioningData);
+ }
+
+ addFromProperties(clazz.getDeclaredProperties("field"),
versioningData);
+ addFromProperties(clazz.getDeclaredProperties("property"),
versioningData);
+ }
+
public PersistentClassVersioningData getVersioningData(PersistentClass pc,
YReflectionManager reflectionManager) {
PersistentClassVersioningData versioningData = new
PersistentClassVersioningData();
Modified:
trunk/src/main/org/jboss/envers/configuration/metadata/PersistentClassVersioningData.java
===================================================================
---
trunk/src/main/org/jboss/envers/configuration/metadata/PersistentClassVersioningData.java 2008-08-21
14:45:59 UTC (rev 120)
+++
trunk/src/main/org/jboss/envers/configuration/metadata/PersistentClassVersioningData.java 2008-08-22
12:03:52 UTC (rev 121)
@@ -25,14 +25,18 @@
import java.util.HashMap;
import java.util.Map;
+import java.util.List;
+import java.util.ArrayList;
/**
* @author Adam Warski (adam at warski dot org)
+ * @author Sebastian Komander
*/
public class PersistentClassVersioningData {
public PersistentClassVersioningData() {
propertyStoreInfo = new PropertyStoreInfo(new HashMap<String,
ModificationStore>());
secondaryTableDictionary = new HashMap<String, String>();
+ unversionedProperties = new ArrayList<String>();
}
public PropertyStoreInfo propertyStoreInfo;
@@ -40,6 +44,7 @@
public String schema;
public String catalog;
public Map<String, String> secondaryTableDictionary;
+ public List<String> unversionedProperties;
public boolean isVersioned() {
if (propertyStoreInfo.propertyStores.size() > 0) { return true; }
Modified:
trunk/src/main/org/jboss/envers/configuration/metadata/VersionsMetadataGenerator.java
===================================================================
---
trunk/src/main/org/jboss/envers/configuration/metadata/VersionsMetadataGenerator.java 2008-08-21
14:45:59 UTC (rev 120)
+++
trunk/src/main/org/jboss/envers/configuration/metadata/VersionsMetadataGenerator.java 2008-08-22
12:03:52 UTC (rev 121)
@@ -53,6 +53,7 @@
/**
* @author Adam Warski (adam at warski dot org)
+ * @author Sebastian Komander
*/
public class VersionsMetadataGenerator {
private final static Map<String, ModificationStore> EMPTY_STORE =
Collections.emptyMap();
@@ -139,7 +140,8 @@
@SuppressWarnings({"unchecked"})
private void addComponent(Element parent, Property property, CompositeMapperBuilder
mapper, ModificationStore store,
- String entityName, EntityMappingData mappingData, boolean
firstPass) {
+ String entityName, EntityMappingData mappingData,
List<String> unversionedProperties,
+ boolean firstPass) {
Element component_mapping = null;
Component prop_component = (Component) property.getValue();
@@ -166,7 +168,7 @@
addProperties(component_mapping, (Iterator<Property>)
prop_component.getPropertyIterator(),
mapper.addComposite(property.getName()), new PropertyStoreInfo(store,
EMPTY_STORE), entityName,
- mappingData, firstPass);
+ mappingData, unversionedProperties, firstPass);
}
@SuppressWarnings({"unchecked"})
@@ -417,18 +419,20 @@
@SuppressWarnings({"unchecked"})
private void addProperties(Element parent, Iterator<Property> properties,
CompositeMapperBuilder currentMapper,
PropertyStoreInfo propertyStoreInfo, String entityName,
EntityMappingData mappingData,
- boolean firstPass) {
+ List<String> unversionedProperties, boolean
firstPass) {
while (properties.hasNext()) {
Property property = properties.next();
Type propertyType = property.getType();
if (!"_identifierMapper".equals(property.getName())) {
ModificationStore store = getStoreForProperty(property,
propertyStoreInfo);
- if (store != null) {
+ // check if the store isn't empty and the property isn't
unversioned
+ if (store != null && !unversionedProperties.contains(property.getName())) {
if (propertyType instanceof ComponentType) {
// only first pass
if (firstPass) {
- addComponent(parent, property, currentMapper, store,
entityName, mappingData, firstPass);
+ addComponent(parent, property, currentMapper, store,
entityName, mappingData,
+ unversionedProperties, firstPass);
}
} else if (propertyType instanceof ImmutableType || propertyType
instanceof MutableType) {
// only first pass
@@ -529,7 +533,8 @@
@SuppressWarnings({"unchecked"})
private void addJoins(PersistentClass pc, CompositeMapperBuilder currentMapper,
PropertyStoreInfo propertyStoreInfo,
- String entityName, EntityMappingData mappingData, boolean
firstPass) {
+ String entityName, EntityMappingData mappingData,
List<String> unversionedProperties,
+ boolean firstPass) {
Iterator<Join> joins = pc.getJoinIterator();
while (joins.hasNext()) {
@@ -537,7 +542,7 @@
Element joinElement = entitiesJoins.get(entityName).get(join);
addProperties(joinElement, join.getPropertyIterator(), currentMapper,
propertyStoreInfo, entityName,
- mappingData, firstPass);
+ mappingData, unversionedProperties, firstPass);
}
}
@@ -668,11 +673,13 @@
// Mapping unjoined properties
addProperties(class_mapping, (Iterator<Property>)
pc.getUnjoinedPropertyIterator(), propertyMapper,
- versioningData.propertyStoreInfo, pc.getEntityName(), mappingData,
true);
+ versioningData.propertyStoreInfo, pc.getEntityName(), mappingData,
versioningData.unversionedProperties,
+ true);
// Creating and mapping joins (first pass)
createJoins(pc, class_mapping, versioningData);
- addJoins(pc, propertyMapper, versioningData.propertyStoreInfo,
pc.getEntityName(), mappingData, true);
+ addJoins(pc, propertyMapper, versioningData.propertyStoreInfo,
pc.getEntityName(), mappingData,
+ versioningData.unversionedProperties, true);
// Storing the generated configuration
EntityConfiguration entityCfg = new EntityConfiguration(entityName,
versionsEntityName, idMapper,
@@ -694,10 +701,12 @@
}
addProperties(parent, (Iterator<Property>)
pc.getUnjoinedPropertyIterator(),
- propertyMapper, versioningData.propertyStoreInfo, entityName,
mappingData, false);
+ propertyMapper, versioningData.propertyStoreInfo, entityName,
mappingData,
+ versioningData.unversionedProperties, false);
// Mapping joins (second pass)
- addJoins(pc, propertyMapper, versioningData.propertyStoreInfo, entityName,
mappingData, false);
+ addJoins(pc, propertyMapper, versioningData.propertyStoreInfo, entityName,
mappingData,
+ versioningData.unversionedProperties, false);
}
public Map<String, EntityConfiguration> getEntitiesConfigurations() {
Deleted:
trunk/src/main/org/jboss/envers/synchronization/work/BidirectionalCollectionChangeWorkUnit.java
===================================================================
---
trunk/src/main/org/jboss/envers/synchronization/work/BidirectionalCollectionChangeWorkUnit.java 2008-08-21
14:45:59 UTC (rev 120)
+++
trunk/src/main/org/jboss/envers/synchronization/work/BidirectionalCollectionChangeWorkUnit.java 2008-08-22
12:03:52 UTC (rev 121)
@@ -1,78 +0,0 @@
-/*
- * Envers.
http://www.jboss.org/envers
- *
- * Copyright 2008 Red Hat Middleware, LLC. All rights reserved.
- *
- * 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, v. 2.1.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT A 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, v.2.1 along with this distribution; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
- * 02110-1301, USA.
- *
- * Red Hat Author(s): Adam Warski
- */
-package org.jboss.envers.synchronization.work;
-
-import org.hibernate.Session;
-import org.jboss.envers.configuration.VersionsConfiguration;
-import org.jboss.envers.RevisionType;
-
-import java.io.Serializable;
-import java.util.Map;
-import java.util.HashMap;
-
-/**
- * @author Adam Warski (adam at warski dot org)
- */
-public class BidirectionalCollectionChangeWorkUnit extends AbstractVersionsWorkUnit
implements VersionsWorkUnit {
- private final Object entity;
-
- public BidirectionalCollectionChangeWorkUnit(String entityName, VersionsConfiguration
verCfg, Serializable id, Object entity) {
- super(entityName, verCfg, id);
-
- this.entity = entity;
- }
-
- public boolean containsWork() {
- return true;
- }
-
- public void perform(Session session, Number revision) {
- Map<String, Object> data = new HashMap<String, Object>();
- fillDataWithId(data, revision, RevisionType.MOD);
-
-
verCfg.getEntCfg().get(getEntityName()).getPropertyMapper().mapToMapFromEntity(data,
entity, null);
-
- session.save(verCfg.getVerEntCfg().getVersionsEntityName(getEntityName()),
data);
-
- setPerformed(data);
- }
-
- public KeepCheckResult check(AddWorkUnit second) {
- return KeepCheckResult.SECOND;
- }
-
- public KeepCheckResult check(ModWorkUnit second) {
- return KeepCheckResult.SECOND;
- }
-
- public KeepCheckResult check(DelWorkUnit second) {
- return KeepCheckResult.SECOND;
- }
-
- public KeepCheckResult check(BidirectionalCollectionChangeWorkUnit second) {
- return KeepCheckResult.FIRST;
- }
-
- public KeepCheckResult dispatch(KeepCheckVisitor first) {
- return first.check(this);
- }
-}
Added: trunk/src/test/org/jboss/envers/test/entities/UnversionedEntity.java
===================================================================
--- trunk/src/test/org/jboss/envers/test/entities/UnversionedEntity.java
(rev 0)
+++ trunk/src/test/org/jboss/envers/test/entities/UnversionedEntity.java 2008-08-22
12:03:52 UTC (rev 121)
@@ -0,0 +1,86 @@
+package org.jboss.envers.test.entities;
+
+import org.jboss.envers.Versioned;
+import org.jboss.envers.Unversioned;
+
+import javax.persistence.Id;
+import javax.persistence.Basic;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+
+/**
+ * @author Adam Warski (adam at warski dot org)
+ */
+@Entity
+@Versioned
+public class UnversionedEntity {
+ @Id
+ @GeneratedValue
+ private Integer id;
+
+ @Basic
+ private String data1;
+
+ @Basic
+ @Unversioned
+ private String data2;
+
+ public UnversionedEntity() {
+ }
+
+ public UnversionedEntity(String data1, String data2) {
+ this.data1 = data1;
+ this.data2 = data2;
+ }
+
+ public UnversionedEntity(Integer id, String data1, String data2) {
+ this.id = id;
+ this.data1 = data1;
+ this.data2 = data2;
+ }
+
+ public Integer getId() {
+ return id;
+ }
+
+ public void setId(Integer id) {
+ this.id = id;
+ }
+
+ public String getData1() {
+ return data1;
+ }
+
+ public void setData1(String data1) {
+ this.data1 = data1;
+ }
+
+ public String getData2() {
+ return data2;
+ }
+
+ public void setData2(String data2) {
+ this.data2 = data2;
+ }
+
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (!(o instanceof UnversionedEntity)) return false;
+
+ UnversionedEntity that = (UnversionedEntity) o;
+
+ if (data1 != null ? !data1.equals(that.data1) : that.data1 != null) return
false;
+ if (data2 != null ? !data2.equals(that.data2) : that.data2 != 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 + (data1 != null ? data1.hashCode() : 0);
+ result = 31 * result + (data2 != null ? data2.hashCode() : 0);
+ return result;
+ }
+}
Added: trunk/src/test/org/jboss/envers/test/integration/basic/UnversionedProperty.java
===================================================================
--- trunk/src/test/org/jboss/envers/test/integration/basic/UnversionedProperty.java
(rev 0)
+++
trunk/src/test/org/jboss/envers/test/integration/basic/UnversionedProperty.java 2008-08-22
12:03:52 UTC (rev 121)
@@ -0,0 +1,54 @@
+package org.jboss.envers.test.integration.basic;
+
+import org.hibernate.ejb.Ejb3Configuration;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+import org.jboss.envers.test.entities.UnversionedEntity;
+import org.jboss.envers.test.integration.AbstractEntityTest;
+
+import javax.persistence.EntityManager;
+import java.util.Arrays;
+
+/**
+ * @author Adam Warski (adam at warski dot org)
+ */
+public class UnversionedProperty extends AbstractEntityTest {
+ private Integer id1;
+
+ public void configure(Ejb3Configuration cfg) {
+ cfg.addAnnotatedClass(UnversionedEntity.class);
+ }
+
+ @BeforeClass(dependsOnMethods = "init")
+ public void initData() {
+ EntityManager em = getEntityManager();
+
+ // Rev 1
+ em.getTransaction().begin();
+ UnversionedEntity ue1 = new UnversionedEntity("a1", "b1");
+ em.persist(ue1);
+ id1 = ue1.getId();
+ em.getTransaction().commit();
+
+ // Rev 2
+ em.getTransaction().begin();
+ ue1 = em.find(UnversionedEntity.class, id1);
+ ue1.setData1("a2");
+ ue1.setData2("b2");
+ em.getTransaction().commit();
+ }
+
+ @Test
+ public void testRevisionsCounts() {
+ assert Arrays.asList(1,
2).equals(getVersionsReader().getRevisions(UnversionedEntity.class, id1));
+ }
+
+ @Test
+ public void testHistoryOfId1() {
+ UnversionedEntity rev1 = new UnversionedEntity(id1, "a1", null);
+ UnversionedEntity rev2 = new UnversionedEntity(id1, "a2", null);
+
+ assert getVersionsReader().find(UnversionedEntity.class, id1, 1).equals(rev1);
+ assert getVersionsReader().find(UnversionedEntity.class, id1, 2).equals(rev2);
+ }
+}