Author: adamw
Date: 2008-09-24 06:42:17 -0400 (Wed, 24 Sep 2008)
New Revision: 166
Added:
trunk/src/main/org/jboss/envers/entities/mapper/relation/query/OneVersionsEntityQueryGenerator.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/query/RelationQueryGenerator.java
trunk/src/test/org/jboss/envers/test/integration/onetomany/BidirectionalMapKey.java
trunk/src/test/org/jboss/envers/test/integration/onetomany/RefEdMapKeyEntity.java
trunk/src/test/org/jboss/envers/test/integration/onetomany/RefIngMapKeyEntity.java
Removed:
trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyAttachedMapper.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleMapKeyComponentMapper.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/OneToManyAttachedInitializor.java
trunk/src/test/org/jboss/envers/test/integration/components/Component1.java
trunk/src/test/org/jboss/envers/test/integration/components/Component2.java
trunk/src/test/org/jboss/envers/test/integration/components/ComponentTestEntity.java
Modified:
trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java
trunk/src/main/org/jboss/envers/configuration/metadata/QueryGeneratorBuilder.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/CommonCollectionMapperData.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/MiddleIdData.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/AbstractCollectionInitializor.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/ArrayCollectionInitializor.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/BasicCollectionInitializor.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/ListCollectionInitializor.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/MapCollectionInitializor.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/query/OneEntityQueryGenerator.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/query/ThreeEntityQueryGenerator.java
trunk/src/main/org/jboss/envers/entities/mapper/relation/query/TwoEntityQueryGenerator.java
trunk/src/test/org/jboss/envers/test/integration/collection/mapkey/IdMapKey.java
trunk/src/test/org/jboss/envers/test/integration/onetomany/BasicCollection.java
Log:
ENVERS-25: adding @MapKey support for one-to-many attached relations. Using the new
collection generator for generating the mapping of one-to-many attached relations.
Modified:
trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java
===================================================================
---
trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java 2008-09-23
17:05:36 UTC (rev 165)
+++
trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java 2008-09-24
10:42:17 UTC (rev 166)
@@ -7,9 +7,9 @@
import org.jboss.envers.entities.mapper.CompositeMapperBuilder;
import org.jboss.envers.entities.mapper.relation.*;
import org.jboss.envers.entities.mapper.relation.lazy.proxy.*;
-import org.jboss.envers.entities.mapper.relation.query.MiddleTableQueryGenerator;
+import org.jboss.envers.entities.mapper.relation.query.RelationQueryGenerator;
+import org.jboss.envers.entities.mapper.relation.query.OneVersionsEntityQueryGenerator;
import org.jboss.envers.entities.mapper.relation.component.*;
-import org.jboss.envers.entities.mapper.id.IdMapper;
import org.jboss.envers.entities.EntityConfiguration;
import org.jboss.envers.entities.IdMappingData;
import org.jboss.envers.tools.Tools;
@@ -92,33 +92,59 @@
void addCollection() {
Type type = propertyValue.getType();
- if ((type instanceof BagType || type instanceof SetType) &&
+ if ((type instanceof BagType || type instanceof SetType || type instanceof
MapType) &&
(propertyValue.getElement() instanceof OneToMany) &&
(propertyValue.isInverse())) {
// A one-to-many relation mapped using @ManyToOne and
@OneToMany(mappedBy="...")
addOneToManyAttached();
} else {
- // All other kinds of relations require a middle (join) table).
+ // All other kinds of relations require a middle (join) table.
addWithMiddleTable();
}
}
@SuppressWarnings({"unchecked"})
private void addOneToManyAttached() {
- String owningReferencePropertyName = getMappedBy(propertyValue);
+ String mappedBy = getMappedBy(propertyValue);
+ EntityConfiguration referencedEntityConfiguration =
mainGenerator.getEntitiesConfigurations()
+ .get(referencedEntityName);
+
+ IdMappingData referencedIdMapping =
referencedEntityConfiguration.getIdMappingData();
IdMappingData referencingIdMapping =
referencingEntityConfiguration.getIdMappingData();
- String lastPropertyPrefix = owningReferencePropertyName + "_";
- // Generating the id mapper for the relation
- IdMapper ownedIdMapper =
referencingIdMapping.getIdMapper().prefixMappedProperties(lastPropertyPrefix);
+ // Generating the id mappers data for the referencing side of the relation.
+ MiddleIdData referencingIdData = new MiddleIdData(mainGenerator.getVerEntCfg(),
referencingIdMapping,
+ mappedBy + "_", referencingEntityName);
- // Storing information about this relation
- referencingEntityConfiguration.addToManyNotOwningRelation(propertyName,
owningReferencePropertyName,
- referencedEntityName, ownedIdMapper);
+ // And for the referenced side. The prefixed mapper won't be used (as this
collection isn't persisted
+ // in a join table, so the prefix value is arbitrary).
+ MiddleIdData referencedIdData = new MiddleIdData(mainGenerator.getVerEntCfg(),
referencedIdMapping,
+ null, referencedEntityName);
- // Adding mapper for the id
- currentMapper.addComposite(propertyName, new
OneToManyAttachedMapper(referencedEntityName, propertyName,
- owningReferencePropertyName));
+ // Generating the element mapping.
+ MiddleComponentData elementComponentData = new MiddleComponentData(
+ new MiddleRelatedComponentMapper(referencedIdData), 0);
+
+ // Generating the index mapping, if an index exists. It can only exists in case a
javax.persistence.MapKey
+ // annotation is present on the entity. So the middleEntityXml will be not be
used. The queryGeneratorBuilder
+ // will only be checked for nullnes.
+ MiddleComponentData indexComponentData = addIndex(null, null);
+
+ // Generating the query generator - it should read directly from the related
entity.
+ RelationQueryGenerator queryGenerator = new
OneVersionsEntityQueryGenerator(mainGenerator.getVerEntCfg(),
+ referencingIdData, referencedEntityName,
referencedIdMapping.getIdMapper());
+
+ // Creating common mapper data.
+ CommonCollectionMapperData commonCollectionMapperData = new
CommonCollectionMapperData(
+ mainGenerator.getVerEntCfg(), referencedEntityName, propertyName,
+ referencingIdData, queryGenerator);
+
+ // Checking the type of the collection and adding an appropriate mapper.
+ addMapper(commonCollectionMapperData, elementComponentData, indexComponentData);
+
+ // Storing information about this relation.
+ referencingEntityConfiguration.addToManyNotOwningRelation(propertyName,
mappedBy,
+ referencedEntityName, referencingIdData.getPrefixedMapper());
}
/**
@@ -201,11 +227,8 @@
}
// Storing the id data of the referencing entity: original mapper, prefixed
mapper and entity name.
- MiddleIdData referencingIdData = new MiddleIdData(
- referencingIdMapping.getIdMapper(),
-
referencingIdMapping.getIdMapper().prefixMappedProperties(referencingPrefixRelated),
- referencingEntityName,
-
mainGenerator.getVerEntCfg().getVersionsEntityName(referencingEntityName));
+ MiddleIdData referencingIdData = new MiddleIdData(mainGenerator.getVerEntCfg(),
referencingIdMapping,
+ referencingPrefixRelated, referencingEntityName);
// Creating a query generator builder, to which additional id data will be added,
in case this collection
// references some entities (either from the element or index). At the end, this
will be used to build
@@ -236,7 +259,7 @@
// Generating the property mapper.
// ******
// Building the query generator.
- MiddleTableQueryGenerator queryGenerator =
queryGeneratorBuilder.build(elementComponentData, indexComponentData);
+ RelationQueryGenerator queryGenerator =
queryGeneratorBuilder.build(elementComponentData, indexComponentData);
// Creating common data
CommonCollectionMapperData commonCollectionMapperData = new
CommonCollectionMapperData(
@@ -261,21 +284,20 @@
} else {
IdMappingData referencedIdMapping =
mainGenerator.getEntitiesConfigurations()
.get(referencedEntityName).getIdMappingData();
+ int currentIndex = queryGeneratorBuilder == null ? 0 :
queryGeneratorBuilder.getCurrentIndex();
if ("".equals(mapKey)) {
// The key of the map is the id of the entity.
return new MiddleComponentData(new
MiddleMapKeyIdComponentMapper(mainGenerator.getVerEntCfg(),
- referencedIdMapping.getIdMapper()),
queryGeneratorBuilder.getCurrentIndex());
+ referencedIdMapping.getIdMapper()), currentIndex);
} else {
// The key of the map is a property of the entity.
- return new MiddleComponentData(new
MiddleMapKeyPropertyComponentMapper(mapKey),
- queryGeneratorBuilder.getCurrentIndex());
+ return new MiddleComponentData(new
MiddleMapKeyPropertyComponentMapper(mapKey), currentIndex);
}
}
// TODO: @MapKeyManyToMany
} else {
// No index - creating a dummy mapper.
- return new MiddleComponentData(new MiddleDummyComponentMapper(),
- queryGeneratorBuilder.getCurrentIndex());
+ return new MiddleComponentData(new MiddleDummyComponentMapper(), 0);
}
}
@@ -313,12 +335,8 @@
}
// Storing the id data of the referenced entity: original mapper, prefixed
mapper and entity name.
- IdMapper referencedPrefixedIdMapper =
referencedIdMapping.getIdMapper().prefixMappedProperties(prefixRelated);
- MiddleIdData referencedIdData = new MiddleIdData(
- referencedIdMapping.getIdMapper(),
- referencedPrefixedIdMapper,
- referencedEntityName,
-
mainGenerator.getVerEntCfg().getVersionsEntityName(referencedEntityName));
+ MiddleIdData referencedIdData = new
MiddleIdData(mainGenerator.getVerEntCfg(), referencedIdMapping,
+ prefixRelated, referencedEntityName);
// And adding it to the generator builder.
queryGeneratorBuilder.addRelation(referencedIdData);
Modified:
trunk/src/main/org/jboss/envers/configuration/metadata/QueryGeneratorBuilder.java
===================================================================
---
trunk/src/main/org/jboss/envers/configuration/metadata/QueryGeneratorBuilder.java 2008-09-23
17:05:36 UTC (rev 165)
+++
trunk/src/main/org/jboss/envers/configuration/metadata/QueryGeneratorBuilder.java 2008-09-24
10:42:17 UTC (rev 166)
@@ -2,7 +2,7 @@
import org.jboss.envers.entities.mapper.relation.MiddleIdData;
import org.jboss.envers.entities.mapper.relation.MiddleComponentData;
-import org.jboss.envers.entities.mapper.relation.query.MiddleTableQueryGenerator;
+import org.jboss.envers.entities.mapper.relation.query.RelationQueryGenerator;
import org.jboss.envers.entities.mapper.relation.query.OneEntityQueryGenerator;
import org.jboss.envers.entities.mapper.relation.query.TwoEntityQueryGenerator;
import org.jboss.envers.entities.mapper.relation.query.ThreeEntityQueryGenerator;
@@ -35,7 +35,7 @@
idDatas.add(idData);
}
- MiddleTableQueryGenerator build(MiddleComponentData... componentDatas) {
+ RelationQueryGenerator build(MiddleComponentData... componentDatas) {
if (idDatas.size() == 0) {
return new OneEntityQueryGenerator(verEntCfg, versionsMiddleEntityName,
referencingIdData,
componentDatas);
Modified:
trunk/src/main/org/jboss/envers/entities/mapper/relation/CommonCollectionMapperData.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/CommonCollectionMapperData.java 2008-09-23
17:05:36 UTC (rev 165)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/CommonCollectionMapperData.java 2008-09-24
10:42:17 UTC (rev 166)
@@ -1,7 +1,7 @@
package org.jboss.envers.entities.mapper.relation;
import org.jboss.envers.configuration.VersionsEntitiesConfiguration;
-import org.jboss.envers.entities.mapper.relation.query.MiddleTableQueryGenerator;
+import org.jboss.envers.entities.mapper.relation.query.RelationQueryGenerator;
/**
* Data that is used by all collection mappers, regardless of the type.
@@ -12,11 +12,11 @@
private final String versionsMiddleEntityName;
private final String collectionReferencingPropertyName;
private final MiddleIdData referencingIdData;
- private final MiddleTableQueryGenerator queryGenerator;
+ private final RelationQueryGenerator queryGenerator;
public CommonCollectionMapperData(VersionsEntitiesConfiguration verEntCfg, String
versionsMiddleEntityName,
String collectionReferencingPropertyName,
MiddleIdData referencingIdData,
- MiddleTableQueryGenerator queryGenerator) {
+ RelationQueryGenerator queryGenerator) {
this.verEntCfg = verEntCfg;
this.versionsMiddleEntityName = versionsMiddleEntityName;
this.collectionReferencingPropertyName = collectionReferencingPropertyName;
@@ -40,7 +40,7 @@
return referencingIdData;
}
- public MiddleTableQueryGenerator getQueryGenerator() {
+ public RelationQueryGenerator getQueryGenerator() {
return queryGenerator;
}
}
Modified: trunk/src/main/org/jboss/envers/entities/mapper/relation/MiddleIdData.java
===================================================================
--- trunk/src/main/org/jboss/envers/entities/mapper/relation/MiddleIdData.java 2008-09-23
17:05:36 UTC (rev 165)
+++ trunk/src/main/org/jboss/envers/entities/mapper/relation/MiddleIdData.java 2008-09-24
10:42:17 UTC (rev 166)
@@ -1,6 +1,8 @@
package org.jboss.envers.entities.mapper.relation;
import org.jboss.envers.entities.mapper.id.IdMapper;
+import org.jboss.envers.entities.IdMappingData;
+import org.jboss.envers.configuration.VersionsEntitiesConfiguration;
/**
* A class holding information about ids, which form a virtual "relation" from
a middle-table. Middle-tables are used
@@ -25,11 +27,12 @@
*/
private final String versionsEntityName;
- public MiddleIdData(IdMapper originalMapper, IdMapper prefixedMapper, String
entityName, String versionsEntityName) {
- this.originalMapper = originalMapper;
- this.prefixedMapper = prefixedMapper;
+ public MiddleIdData(VersionsEntitiesConfiguration verEntCfg, IdMappingData
mappingData, String prefix,
+ String entityName) {
+ this.originalMapper = mappingData.getIdMapper();
+ this.prefixedMapper = mappingData.getIdMapper().prefixMappedProperties(prefix);
this.entityName = entityName;
- this.versionsEntityName = versionsEntityName;
+ this.versionsEntityName = verEntCfg.getVersionsEntityName(entityName);
}
public IdMapper getOriginalMapper() {
Deleted:
trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyAttachedMapper.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyAttachedMapper.java 2008-09-23
17:05:36 UTC (rev 165)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyAttachedMapper.java 2008-09-24
10:42:17 UTC (rev 166)
@@ -1,101 +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.entities.mapper.relation;
-
-import org.jboss.envers.entities.mapper.PropertyMapper;
-import org.jboss.envers.entities.mapper.PersistentCollectionChangeData;
-import org.jboss.envers.reader.VersionsReaderImplementor;
-import org.jboss.envers.entities.mapper.relation.lazy.OneToManyAttachedInitializor;
-import org.jboss.envers.entities.mapper.relation.lazy.proxy.ListProxy;
-import org.jboss.envers.entities.mapper.relation.lazy.proxy.SetProxy;
-import org.jboss.envers.entities.mapper.relation.lazy.proxy.MapProxy;
-import org.jboss.envers.entities.mapper.relation.lazy.initializor.Initializor;
-import org.jboss.envers.configuration.VersionsConfiguration;
-import org.jboss.envers.tools.reflection.ReflectionTools;
-import org.jboss.envers.exception.VersionsException;
-import org.hibernate.collection.PersistentCollection;
-import org.hibernate.property.Getter;
-import org.hibernate.property.Setter;
-
-import java.util.*;
-import java.io.Serializable;
-
-/**
- * @author Adam Warski (adam at warski dot org)
- */
-public class OneToManyAttachedMapper implements PropertyMapper {
- private final String owningEntityName;
- private final String propertyName;
- private final String owningReferencePropertyName;
-
- public OneToManyAttachedMapper(String owningEntityName, String propertyName, String
owningReferencePropertyName) {
- this.owningEntityName = owningEntityName;
- this.propertyName = propertyName;
- this.owningReferencePropertyName = owningReferencePropertyName;
- }
-
- @SuppressWarnings({"unchecked"})
- public void mapToEntityFromMap(VersionsConfiguration verCfg, Object obj, Map data,
Object primaryKey,
- VersionsReaderImplementor versionsReader, Number
revision) {
- if (obj == null) {
- return;
- }
-
- Class<?> entityClass = ReflectionTools.loadClass(owningEntityName);
-
- Getter getter = ReflectionTools.getGetter(obj.getClass(), propertyName);
- Class collectionClass = getter.getReturnType();
-
- // todo: investigate generics
- // todo: add support for @MapKey, sorted collections
- Object value;
- if (List.class.isAssignableFrom(collectionClass)) {
- value = new ListProxy(getInitializator(versionsReader, entityClass,
primaryKey, revision, ArrayList.class));
- } else if (Set.class.isAssignableFrom(collectionClass) ||
Collection.class.isAssignableFrom(collectionClass)) {
- value = new SetProxy(getInitializator(versionsReader, entityClass,
primaryKey, revision, HashSet.class));
- } else if (Map.class.isAssignableFrom(collectionClass)) {
- value = new MapProxy(getInitializator(versionsReader, entityClass,
primaryKey, revision, HashMap.class));
- } else {
- throw new VersionsException("Unsupported versioned collection type:
" + collectionClass.getName());
- }
-
- Setter setter = ReflectionTools.getSetter(obj.getClass(), propertyName);
- setter.set(obj, value, null);
- }
-
- public boolean mapToMapFromEntity(Map<String, Object> data, Object newObj,
Object oldObj) {
- return false;
- }
-
- protected <T> Initializor<T> getInitializator(VersionsReaderImplementor
versionsReader,
- Class<?> entityClass, Object
primaryKey,
- Number revision,
- Class<T> collectionClass) {
- return new OneToManyAttachedInitializor<T>(versionsReader, entityClass,
owningReferencePropertyName, primaryKey,
- revision, collectionClass);
- }
-
- public List<PersistentCollectionChangeData> mapCollectionChanges(String
referencingPropertyName, PersistentCollection newColl,
- Serializable
oldColl, Serializable id) {
- return null;
- }
-}
\ No newline at end of file
Deleted:
trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleMapKeyComponentMapper.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleMapKeyComponentMapper.java 2008-09-23
17:05:36 UTC (rev 165)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleMapKeyComponentMapper.java 2008-09-24
10:42:17 UTC (rev 166)
@@ -1,34 +0,0 @@
-package org.jboss.envers.entities.mapper.relation.component;
-
-import org.jboss.envers.entities.EntityInstantiator;
-import org.jboss.envers.entities.mapper.relation.MiddleIdData;
-import org.jboss.envers.tools.query.Parameters;
-
-import java.util.Map;
-
-/**
- * A component mapper for the @MapKey mapping: the value of the map's key is the id
of the entity. This
- * doesn't have an effect on the data stored in the versions tables, so
<code>mapToMapFromObject</code> is
- * empty.
- * @author Adam Warski (adam at warski dot org)
- */
-public final class MiddleMapKeyComponentMapper implements MiddleComponentMapper {
- private final MiddleIdData relatedIdData;
-
- public MiddleMapKeyComponentMapper(MiddleIdData relatedIdData) {
- this.relatedIdData = relatedIdData;
- }
-
- public Object mapToObjectFromFullMap(EntityInstantiator entityInstantiator,
Map<String, Object> data,
- Number revision) {
- return relatedIdData.getOriginalMapper().mapToIdFromMap(data);
- }
-
- public void mapToMapFromObject(Map<String, Object> data, Object obj) {
- // Doing nothing.
- }
-
- public void addMiddleEqualToQuery(Parameters parameters, String prefix1, String
prefix2) {
- // Doing nothing.
- }
-}
\ No newline at end of file
Deleted:
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/OneToManyAttachedInitializor.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/OneToManyAttachedInitializor.java 2008-09-23
17:05:36 UTC (rev 165)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/OneToManyAttachedInitializor.java 2008-09-24
10:42:17 UTC (rev 166)
@@ -1,75 +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.entities.mapper.relation.lazy;
-
-import org.jboss.envers.reader.VersionsReaderImplementor;
-import org.jboss.envers.entities.mapper.relation.lazy.initializor.Initializor;
-import org.jboss.envers.query.VersionsRestrictions;
-import org.jboss.envers.exception.VersionsException;
-
-import java.util.Collection;
-import java.util.List;
-
-/**
- * @author Adam Warski (adam at warski dot org)
- */
-public class OneToManyAttachedInitializor<T> implements Initializor<T> {
- private VersionsReaderImplementor versionsReader;
- private Class<?> entityClass;
- private String owningReferencePropertyName;
- private Object primaryKey;
- private Number revision;
- private Class<T> collectionClass;
-
- public OneToManyAttachedInitializor(VersionsReaderImplementor versionsReader,
Class<?> entityClass,
- String owningReferencePropertyName, Object primaryKey,
Number revision,
- Class<T> collectionClass) {
- this.versionsReader = versionsReader;
- this.entityClass = entityClass;
- this.owningReferencePropertyName = owningReferencePropertyName;
- this.primaryKey = primaryKey;
- this.revision = revision;
- this.collectionClass = collectionClass;
- }
-
- @SuppressWarnings({"unchecked"})
- public T initialize() {
- List queryResult =
versionsReader.createQuery().forEntitiesAtRevision(entityClass, revision)
- .add(VersionsRestrictions.relatedIdEq(owningReferencePropertyName,
primaryKey)).getResultList();
-
- if (collectionClass.isAssignableFrom(queryResult.getClass())) {
- return (T) queryResult;
- } else {
- Collection result;
- try {
- // TODO
- result = (Collection) collectionClass.newInstance();
- } catch (Exception e) {
- throw new VersionsException(e);
- }
-
- result.addAll(queryResult);
-
- return (T) result;
- }
- }
-}
Modified:
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/AbstractCollectionInitializor.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/AbstractCollectionInitializor.java 2008-09-23
17:05:36 UTC (rev 165)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/AbstractCollectionInitializor.java 2008-09-24
10:42:17 UTC (rev 166)
@@ -1,6 +1,6 @@
package org.jboss.envers.entities.mapper.relation.lazy.initializor;
-import org.jboss.envers.entities.mapper.relation.query.MiddleTableQueryGenerator;
+import org.jboss.envers.entities.mapper.relation.query.RelationQueryGenerator;
import org.jboss.envers.entities.EntityInstantiator;
import org.jboss.envers.reader.VersionsReaderImplementor;
import org.jboss.envers.configuration.VersionsConfiguration;
@@ -13,7 +13,7 @@
*/
public abstract class AbstractCollectionInitializor<T> implements
Initializor<T> {
private final VersionsReaderImplementor versionsReader;
- private final MiddleTableQueryGenerator queryGenerator;
+ private final RelationQueryGenerator queryGenerator;
private final Object primaryKey;
protected final Number revision;
@@ -21,7 +21,7 @@
public AbstractCollectionInitializor(VersionsConfiguration verCfg,
VersionsReaderImplementor versionsReader,
- MiddleTableQueryGenerator queryGenerator,
+ RelationQueryGenerator queryGenerator,
Object primaryKey, Number revision) {
this.versionsReader = versionsReader;
this.queryGenerator = queryGenerator;
Modified:
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/ArrayCollectionInitializor.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/ArrayCollectionInitializor.java 2008-09-23
17:05:36 UTC (rev 165)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/ArrayCollectionInitializor.java 2008-09-24
10:42:17 UTC (rev 166)
@@ -1,6 +1,6 @@
package org.jboss.envers.entities.mapper.relation.lazy.initializor;
-import org.jboss.envers.entities.mapper.relation.query.MiddleTableQueryGenerator;
+import org.jboss.envers.entities.mapper.relation.query.RelationQueryGenerator;
import org.jboss.envers.entities.mapper.relation.MiddleComponentData;
import org.jboss.envers.reader.VersionsReaderImplementor;
import org.jboss.envers.configuration.VersionsConfiguration;
@@ -17,7 +17,7 @@
public ArrayCollectionInitializor(VersionsConfiguration verCfg,
VersionsReaderImplementor versionsReader,
- MiddleTableQueryGenerator queryGenerator,
+ RelationQueryGenerator queryGenerator,
Object primaryKey, Number revision,
MiddleComponentData elementComponentData,
MiddleComponentData indexComponentData) {
Modified:
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/BasicCollectionInitializor.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/BasicCollectionInitializor.java 2008-09-23
17:05:36 UTC (rev 165)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/BasicCollectionInitializor.java 2008-09-24
10:42:17 UTC (rev 166)
@@ -1,6 +1,6 @@
package org.jboss.envers.entities.mapper.relation.lazy.initializor;
-import org.jboss.envers.entities.mapper.relation.query.MiddleTableQueryGenerator;
+import org.jboss.envers.entities.mapper.relation.query.RelationQueryGenerator;
import org.jboss.envers.entities.mapper.relation.MiddleComponentData;
import org.jboss.envers.reader.VersionsReaderImplementor;
import org.jboss.envers.exception.VersionsException;
@@ -18,7 +18,7 @@
public BasicCollectionInitializor(VersionsConfiguration verCfg,
VersionsReaderImplementor versionsReader,
- MiddleTableQueryGenerator queryGenerator,
+ RelationQueryGenerator queryGenerator,
Object primaryKey, Number revision,
Class<? extends T> collectionClass,
MiddleComponentData elementComponentData) {
Modified:
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/ListCollectionInitializor.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/ListCollectionInitializor.java 2008-09-23
17:05:36 UTC (rev 165)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/ListCollectionInitializor.java 2008-09-24
10:42:17 UTC (rev 166)
@@ -1,6 +1,6 @@
package org.jboss.envers.entities.mapper.relation.lazy.initializor;
-import org.jboss.envers.entities.mapper.relation.query.MiddleTableQueryGenerator;
+import org.jboss.envers.entities.mapper.relation.query.RelationQueryGenerator;
import org.jboss.envers.entities.mapper.relation.MiddleComponentData;
import org.jboss.envers.reader.VersionsReaderImplementor;
import org.jboss.envers.configuration.VersionsConfiguration;
@@ -17,7 +17,7 @@
public ListCollectionInitializor(VersionsConfiguration verCfg,
VersionsReaderImplementor versionsReader,
- MiddleTableQueryGenerator queryGenerator,
+ RelationQueryGenerator queryGenerator,
Object primaryKey, Number revision,
MiddleComponentData elementComponentData,
MiddleComponentData indexComponentData) {
Modified:
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/MapCollectionInitializor.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/MapCollectionInitializor.java 2008-09-23
17:05:36 UTC (rev 165)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/MapCollectionInitializor.java 2008-09-24
10:42:17 UTC (rev 166)
@@ -1,6 +1,6 @@
package org.jboss.envers.entities.mapper.relation.lazy.initializor;
-import org.jboss.envers.entities.mapper.relation.query.MiddleTableQueryGenerator;
+import org.jboss.envers.entities.mapper.relation.query.RelationQueryGenerator;
import org.jboss.envers.entities.mapper.relation.MiddleComponentData;
import org.jboss.envers.reader.VersionsReaderImplementor;
import org.jboss.envers.exception.VersionsException;
@@ -19,7 +19,7 @@
public MapCollectionInitializor(VersionsConfiguration verCfg,
VersionsReaderImplementor versionsReader,
- MiddleTableQueryGenerator queryGenerator,
+ RelationQueryGenerator queryGenerator,
Object primaryKey, Number revision,
Class<? extends T> collectionClass,
MiddleComponentData elementComponentData,
Modified:
trunk/src/main/org/jboss/envers/entities/mapper/relation/query/OneEntityQueryGenerator.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/query/OneEntityQueryGenerator.java 2008-09-23
17:05:36 UTC (rev 165)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/query/OneEntityQueryGenerator.java 2008-09-24
10:42:17 UTC (rev 166)
@@ -13,9 +13,10 @@
import java.util.Collections;
/**
+ * Selects data from a relation middle-table only.
* @author Adam Warski (adam at warski dot org)
*/
-public final class OneEntityQueryGenerator implements MiddleTableQueryGenerator {
+public final class OneEntityQueryGenerator implements RelationQueryGenerator {
private final String queryString;
private final MiddleIdData referencingIdData;
Copied:
trunk/src/main/org/jboss/envers/entities/mapper/relation/query/OneVersionsEntityQueryGenerator.java
(from rev 155,
trunk/src/main/org/jboss/envers/entities/mapper/relation/query/TwoEntityQueryGenerator.java)
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/query/OneVersionsEntityQueryGenerator.java
(rev 0)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/query/OneVersionsEntityQueryGenerator.java 2008-09-24
10:42:17 UTC (rev 166)
@@ -0,0 +1,84 @@
+package org.jboss.envers.entities.mapper.relation.query;
+
+import org.jboss.envers.entities.mapper.id.QueryParameterData;
+import org.jboss.envers.entities.mapper.id.IdMapper;
+import org.jboss.envers.entities.mapper.relation.MiddleIdData;
+import org.jboss.envers.configuration.VersionsEntitiesConfiguration;
+import org.jboss.envers.reader.VersionsReaderImplementor;
+import org.jboss.envers.RevisionType;
+import org.jboss.envers.tools.query.QueryBuilder;
+import org.jboss.envers.tools.query.Parameters;
+import org.hibernate.Query;
+
+import java.util.Collections;
+
+/**
+ * Selects data from a versions entity.
+ * @author Adam Warski (adam at warski dot org)
+ */
+public final class OneVersionsEntityQueryGenerator implements RelationQueryGenerator {
+ private final String queryString;
+ private final MiddleIdData referencingIdData;
+
+ public OneVersionsEntityQueryGenerator(VersionsEntitiesConfiguration verEntCfg,
MiddleIdData referencingIdData,
+ String referencedEntityName, IdMapper
referencedIdMapper) {
+ this.referencingIdData = referencingIdData;
+
+ /*
+ * The query that we need to create:
+ * SELECT new list(e) FROM versionsReferencedEntity e
+ * WHERE
+ * (only entities referenced by the association; id_ref_ing = id of the
referencing entity)
+ * e.id_ref_ing = :id_ref_ing AND
+ * (selecting e entities at revision :revision)
+ * e.revision = (SELECT max(e2.revision) FROM versionsReferencedEntity e2
+ * WHERE e2.revision <= :revision AND e2.id = e.id) AND
+ * (only non-deleted entities)
+ * e.revision_type != DEL
+ */
+ String revisionPropertyPath = verEntCfg.getRevisionPropPath();
+ String originalIdPropertyName = verEntCfg.getOriginalIdPropName();
+
+ String versionsReferencedEntityName =
verEntCfg.getVersionsEntityName(referencedEntityName);
+
+ // SELECT new list(e) FROM versionsEntity e
+ QueryBuilder qb = new QueryBuilder(versionsReferencedEntityName, "e");
+ qb.addProjection("new list", "e", false, false);
+ // WHERE
+ Parameters rootParameters = qb.getRootParameters();
+ // e.id_ref_ed = :id_ref_ed
+ referencingIdData.getPrefixedMapper().addNamedIdEqualsToQuery(rootParameters,
null, true);
+
+ // SELECT max(e.revision) FROM versionsReferencedEntity e2
+ QueryBuilder maxERevQb = qb.newSubQueryBuilder(versionsReferencedEntityName,
"e2");
+ maxERevQb.addProjection("max", revisionPropertyPath, false);
+ // WHERE
+ Parameters maxERevQbParameters = maxERevQb.getRootParameters();
+ // e2.revision <= :revision
+ maxERevQbParameters.addWhereWithNamedParam(revisionPropertyPath,
"<=", "revision");
+ // e2.id = e.id
+ referencedIdMapper.addIdsEqualToQuery(maxERevQbParameters,
+ "e." + originalIdPropertyName, "e2." +
originalIdPropertyName);
+
+ // e.revision = (SELECT max(...) ...)
+ rootParameters.addWhere(revisionPropertyPath, false, "=", maxERevQb);
+
+ // e.revision_type != DEL
+ rootParameters.addWhereWithNamedParam(verEntCfg.getRevisionTypePropName(), false,
"!=", "delrevisiontype");
+
+ StringBuilder sb = new StringBuilder();
+ qb.build(sb, Collections.<String, Object>emptyMap());
+ queryString = sb.toString();
+ }
+
+ public Query getQuery(VersionsReaderImplementor versionsReader, Object primaryKey,
Number revision) {
+ Query query = versionsReader.getSession().createQuery(queryString);
+ query.setParameter("revision", revision);
+ query.setParameter("delrevisiontype", RevisionType.DEL);
+ for (QueryParameterData paramData:
referencingIdData.getPrefixedMapper().mapToQueryParametersFromId(primaryKey)) {
+ paramData.setParameterValue(query);
+ }
+
+ return query;
+ }
+}
\ No newline at end of file
Property changes on:
trunk/src/main/org/jboss/envers/entities/mapper/relation/query/OneVersionsEntityQueryGenerator.java
___________________________________________________________________
Name: svn:mergeinfo
+
Copied:
trunk/src/main/org/jboss/envers/entities/mapper/relation/query/RelationQueryGenerator.java
(from rev 151,
trunk/src/main/org/jboss/envers/entities/mapper/relation/query/MiddleTableQueryGenerator.java)
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/query/RelationQueryGenerator.java
(rev 0)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/query/RelationQueryGenerator.java 2008-09-24
10:42:17 UTC (rev 166)
@@ -0,0 +1,14 @@
+package org.jboss.envers.entities.mapper.relation.query;
+
+import org.hibernate.Query;
+import org.jboss.envers.reader.VersionsReaderImplementor;
+
+/**
+ * Implementations of this interface provide a method to generate queries on a relation
table (a table used
+ * for mapping relations). The query can select, apart from selecting the content of the
relation table, also data of
+ * other "related" entities.
+ * @author Adam Warski (adam at warski dot org)
+ */
+public interface RelationQueryGenerator {
+ Query getQuery(VersionsReaderImplementor versionsReader, Object primaryKey, Number
revision);
+}
Modified:
trunk/src/main/org/jboss/envers/entities/mapper/relation/query/ThreeEntityQueryGenerator.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/query/ThreeEntityQueryGenerator.java 2008-09-23
17:05:36 UTC (rev 165)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/query/ThreeEntityQueryGenerator.java 2008-09-24
10:42:17 UTC (rev 166)
@@ -8,7 +8,7 @@
/**
* @author Adam Warski (adam at warski dot org)
*/
-public final class ThreeEntityQueryGenerator implements MiddleTableQueryGenerator {
+public final class ThreeEntityQueryGenerator implements RelationQueryGenerator {
private final String queryString;
private final MiddleIdData referencingIdData;
Modified:
trunk/src/main/org/jboss/envers/entities/mapper/relation/query/TwoEntityQueryGenerator.java
===================================================================
---
trunk/src/main/org/jboss/envers/entities/mapper/relation/query/TwoEntityQueryGenerator.java 2008-09-23
17:05:36 UTC (rev 165)
+++
trunk/src/main/org/jboss/envers/entities/mapper/relation/query/TwoEntityQueryGenerator.java 2008-09-24
10:42:17 UTC (rev 166)
@@ -13,9 +13,10 @@
import java.util.Collections;
/**
+ * Selects data from a relation middle-table and a related versions entity.
* @author Adam Warski (adam at warski dot org)
*/
-public final class TwoEntityQueryGenerator implements MiddleTableQueryGenerator {
+public final class TwoEntityQueryGenerator implements RelationQueryGenerator {
private final String queryString;
private final MiddleIdData referencingIdData;
Modified:
trunk/src/test/org/jboss/envers/test/integration/collection/mapkey/IdMapKey.java
===================================================================
---
trunk/src/test/org/jboss/envers/test/integration/collection/mapkey/IdMapKey.java 2008-09-23
17:05:36 UTC (rev 165)
+++
trunk/src/test/org/jboss/envers/test/integration/collection/mapkey/IdMapKey.java 2008-09-24
10:42:17 UTC (rev 166)
@@ -76,7 +76,6 @@
IdMapKeyEntity rev1 = getVersionsReader().find(IdMapKeyEntity.class, imke_id,
1);
IdMapKeyEntity rev2 = getVersionsReader().find(IdMapKeyEntity.class, imke_id,
2);
- System.out.println(rev1.getIdmap());
assert rev1.getIdmap().equals(TestTools.makeMap(ste1.getId(), ste1));
assert rev2.getIdmap().equals(TestTools.makeMap(ste1.getId(), ste1, ste2.getId(),
ste2));
}
Deleted: trunk/src/test/org/jboss/envers/test/integration/components/Component1.java
===================================================================
--- trunk/src/test/org/jboss/envers/test/integration/components/Component1.java 2008-09-23
17:05:36 UTC (rev 165)
+++ trunk/src/test/org/jboss/envers/test/integration/components/Component1.java 2008-09-24
10:42:17 UTC (rev 166)
@@ -1,53 +0,0 @@
-package org.jboss.envers.test.integration.components;
-
-/**
- * @author Adam Warski (adam at warski dot org)
- */
-public class Component1 {
- private String str1;
-
- private String str2;
-
- public Component1(String str1, String str2) {
- this.str1 = str1;
- this.str2 = str2;
- }
-
- public Component1() {
- }
-
- public String getStr2() {
- return str2;
- }
-
- public void setStr2(String str2) {
- this.str2 = str2;
- }
-
- public String getStr1() {
- return str1;
- }
-
- public void setStr1(String str1) {
- this.str1 = str1;
- }
-
- public boolean equals(Object o) {
- if (this == o) return true;
- if (!(o instanceof Component1)) return false;
-
- Component1 that = (Component1) o;
-
- if (str1 != null ? !str1.equals(that.str1) : that.str1 != null) return false;
- if (str2 != null ? !str2.equals(that.str2) : that.str2 != null) return false;
-
- return true;
- }
-
- public int hashCode() {
- int result;
- result = (str1 != null ? str1.hashCode() : 0);
- result = 31 * result + (str2 != null ? str2.hashCode() : 0);
- return result;
- }
-}
Deleted: trunk/src/test/org/jboss/envers/test/integration/components/Component2.java
===================================================================
--- trunk/src/test/org/jboss/envers/test/integration/components/Component2.java 2008-09-23
17:05:36 UTC (rev 165)
+++ trunk/src/test/org/jboss/envers/test/integration/components/Component2.java 2008-09-24
10:42:17 UTC (rev 166)
@@ -1,53 +0,0 @@
-package org.jboss.envers.test.integration.components;
-
-/**
- * @author Adam Warski (adam at warski dot org)
- */
-public class Component2 {
- private String str5;
-
- private String str6;
-
- public Component2(String str5, String str6) {
- this.str5 = str5;
- this.str6 = str6;
- }
-
- public Component2() {
- }
-
- public String getStr5() {
- return str5;
- }
-
- public void setStr5(String str5) {
- this.str5 = str5;
- }
-
- public String getStr6() {
- return str6;
- }
-
- public void setStr6(String str6) {
- this.str6 = str6;
- }
-
- public boolean equals(Object o) {
- if (this == o) return true;
- if (!(o instanceof Component2)) return false;
-
- Component2 that = (Component2) o;
-
- if (str5 != null ? !str5.equals(that.str5) : that.str5 != null) return false;
- if (str6 != null ? !str6.equals(that.str6) : that.str6 != null) return false;
-
- return true;
- }
-
- public int hashCode() {
- int result;
- result = (str5 != null ? str5.hashCode() : 0);
- result = 31 * result + (str6 != null ? str6.hashCode() : 0);
- return result;
- }
-}
Deleted:
trunk/src/test/org/jboss/envers/test/integration/components/ComponentTestEntity.java
===================================================================
---
trunk/src/test/org/jboss/envers/test/integration/components/ComponentTestEntity.java 2008-09-23
17:05:36 UTC (rev 165)
+++
trunk/src/test/org/jboss/envers/test/integration/components/ComponentTestEntity.java 2008-09-24
10:42:17 UTC (rev 166)
@@ -1,85 +0,0 @@
-package org.jboss.envers.test.integration.components;
-
-import org.jboss.envers.Versioned;
-
-import javax.persistence.Entity;
-import javax.persistence.Id;
-import javax.persistence.GeneratedValue;
-import javax.persistence.Embedded;
-
-/**
- * @author Adam Warski (adam at warski dot org)
- */
-@Entity
-public class ComponentTestEntity {
- @Id
- @GeneratedValue
- private Integer id;
-
- @Embedded
- @Versioned
- private Component1 comp1;
-
- @Embedded
- private Component2 comp2;
-
- public ComponentTestEntity() {
- }
-
- public ComponentTestEntity(Integer id, Component1 comp1, Component2 comp2) {
- this.id = id;
- this.comp1 = comp1;
- this.comp2 = comp2;
- }
-
- public ComponentTestEntity(Component1 comp1, Component2 comp2) {
- this.comp1 = comp1;
- this.comp2 = comp2;
- }
-
- public Integer getId() {
- return id;
- }
-
- public void setId(Integer id) {
- this.id = id;
- }
-
- public Component1 getComp1() {
- return comp1;
- }
-
- public void setComp1(Component1 comp1) {
- this.comp1 = comp1;
- }
-
- public Component2 getComp2() {
- return comp2;
- }
-
- public void setComp2(Component2 comp2) {
- this.comp2 = comp2;
- }
-
- public boolean equals(Object o) {
- if (this == o) return true;
- if (!(o instanceof ComponentTestEntity)) return false;
-
- ComponentTestEntity that = (ComponentTestEntity) o;
-
- if (comp1 != null ? !comp1.equals(that.comp1) : that.comp1 != null) return
false;
- if (comp2 != null ? !comp2.equals(that.comp2) : that.comp2 != 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 + (comp1 != null ? comp1.hashCode() : 0);
- result = 31 * result + (comp2 != null ? comp2.hashCode() : 0);
- return result;
- }
-}
-
Modified: trunk/src/test/org/jboss/envers/test/integration/onetomany/BasicCollection.java
===================================================================
---
trunk/src/test/org/jboss/envers/test/integration/onetomany/BasicCollection.java 2008-09-23
17:05:36 UTC (rev 165)
+++
trunk/src/test/org/jboss/envers/test/integration/onetomany/BasicCollection.java 2008-09-24
10:42:17 UTC (rev 166)
@@ -103,9 +103,14 @@
CollectionRefEdEntity rev2 =
getVersionsReader().find(CollectionRefEdEntity.class, ed1_id, 2);
CollectionRefEdEntity rev3 =
getVersionsReader().find(CollectionRefEdEntity.class, ed1_id, 3);
- assert rev1.getReffering().equals(makeSet(ing1, ing2));
- assert rev2.getReffering().equals(makeSet(ing2));
- assert rev3.getReffering().equals(Collections.EMPTY_SET);
+ assert rev1.getReffering().containsAll(makeSet(ing1, ing2));
+ assert rev1.getReffering().size() == 2;
+
+ assert rev2.getReffering().containsAll(makeSet(ing2));
+ assert rev2.getReffering().size() == 1;
+
+ assert rev3.getReffering().containsAll(Collections.EMPTY_SET);
+ assert rev3.getReffering().size() == 0;
}
@Test
@@ -117,9 +122,15 @@
CollectionRefEdEntity rev2 =
getVersionsReader().find(CollectionRefEdEntity.class, ed2_id, 2);
CollectionRefEdEntity rev3 =
getVersionsReader().find(CollectionRefEdEntity.class, ed2_id, 3);
- assert rev1.getReffering().equals(Collections.EMPTY_SET);
- assert rev2.getReffering().equals(makeSet(ing1));
- assert rev3.getReffering().equals(makeSet(ing1, ing2));
+ assert rev1.getReffering().containsAll(Collections.EMPTY_SET);
+ assert rev1.getReffering().size() == 0;
+
+ assert rev2.getReffering().containsAll(makeSet(ing1));
+ assert rev2.getReffering().size() == 1;
+
+ assert rev3.getReffering().containsAll(makeSet(ing1, ing2));
+ assert rev3.getReffering().size() == 2;
+
}
@Test
Copied:
trunk/src/test/org/jboss/envers/test/integration/onetomany/BidirectionalMapKey.java (from
rev 165,
trunk/src/test/org/jboss/envers/test/integration/collection/mapkey/ComponentMapKey.java)
===================================================================
--- trunk/src/test/org/jboss/envers/test/integration/onetomany/BidirectionalMapKey.java
(rev 0)
+++
trunk/src/test/org/jboss/envers/test/integration/onetomany/BidirectionalMapKey.java 2008-09-24
10:42:17 UTC (rev 166)
@@ -0,0 +1,86 @@
+package org.jboss.envers.test.integration.onetomany;
+
+import org.jboss.envers.test.integration.AbstractEntityTest;
+import org.jboss.envers.test.tools.TestTools;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+import org.hibernate.ejb.Ejb3Configuration;
+
+import javax.persistence.EntityManager;
+import java.util.Arrays;
+
+/**
+ * @author Adam Warski (adam at warski dot org)
+ */
+public class BidirectionalMapKey extends AbstractEntityTest {
+ private Integer ed_id;
+
+ private Integer ing1_id;
+ private Integer ing2_id;
+
+ public void configure(Ejb3Configuration cfg) {
+ cfg.addAnnotatedClass(RefIngMapKeyEntity.class);
+ cfg.addAnnotatedClass(RefEdMapKeyEntity.class);
+ }
+
+ @BeforeClass(dependsOnMethods = "init")
+ public void initData() {
+ EntityManager em = getEntityManager();
+
+ // Revision 1 (intialy 1 relation: ing1 -> ed)
+ em.getTransaction().begin();
+
+ RefEdMapKeyEntity ed = new RefEdMapKeyEntity();
+
+ em.persist(ed);
+
+ RefIngMapKeyEntity ing1 = new RefIngMapKeyEntity();
+ ing1.setData("a");
+ ing1.setReference(ed);
+
+ RefIngMapKeyEntity ing2 = new RefIngMapKeyEntity();
+ ing2.setData("b");
+
+ em.persist(ing1);
+ em.persist(ing2);
+
+ em.getTransaction().commit();
+
+ // Revision 2 (adding second relation: ing2 -> ed)
+ em.getTransaction().begin();
+
+ ed = em.find(RefEdMapKeyEntity.class, ed.getId());
+ ing2 = em.find(RefIngMapKeyEntity.class, ing2.getId());
+
+ ing2.setReference(ed);
+
+ em.getTransaction().commit();
+
+ //
+
+ ed_id = ed.getId();
+
+ ing1_id = ing1.getId();
+ ing2_id = ing2.getId();
+ }
+
+ @Test
+ public void testRevisionsCounts() {
+ assert Arrays.asList(1,
2).equals(getVersionsReader().getRevisions(RefEdMapKeyEntity.class, ed_id));
+
+ assert
Arrays.asList(1).equals(getVersionsReader().getRevisions(RefIngMapKeyEntity.class,
ing1_id));
+ assert Arrays.asList(1,
2).equals(getVersionsReader().getRevisions(RefIngMapKeyEntity.class, ing2_id));
+ }
+
+ @Test
+ public void testHistoryOfEd() {
+ RefIngMapKeyEntity ing1 = getEntityManager().find(RefIngMapKeyEntity.class,
ing1_id);
+ RefIngMapKeyEntity ing2 = getEntityManager().find(RefIngMapKeyEntity.class,
ing2_id);
+
+ RefEdMapKeyEntity rev1 = getVersionsReader().find(RefEdMapKeyEntity.class, ed_id,
1);
+ RefEdMapKeyEntity rev2 = getVersionsReader().find(RefEdMapKeyEntity.class, ed_id,
2);
+
+ assert rev1.getIdmap().equals(TestTools.makeMap("a", ing1));
+ assert rev2.getIdmap().equals(TestTools.makeMap("a", ing1,
"b", ing2));
+ }
+}
\ No newline at end of file
Property changes on:
trunk/src/test/org/jboss/envers/test/integration/onetomany/BidirectionalMapKey.java
___________________________________________________________________
Name: svn:mergeinfo
+
Copied: trunk/src/test/org/jboss/envers/test/integration/onetomany/RefEdMapKeyEntity.java
(from rev 165,
trunk/src/test/org/jboss/envers/test/integration/collection/mapkey/ComponentMapKeyEntity.java)
===================================================================
--- trunk/src/test/org/jboss/envers/test/integration/onetomany/RefEdMapKeyEntity.java
(rev 0)
+++
trunk/src/test/org/jboss/envers/test/integration/onetomany/RefEdMapKeyEntity.java 2008-09-24
10:42:17 UTC (rev 166)
@@ -0,0 +1,61 @@
+package org.jboss.envers.test.integration.onetomany;
+
+import org.jboss.envers.Versioned;
+
+import javax.persistence.*;
+import java.util.Map;
+import java.util.HashMap;
+
+/**
+ * @author Adam Warski (adam at warski dot org)
+ */
+@Entity
+public class RefEdMapKeyEntity {
+ @Id
+ @GeneratedValue
+ private Integer id;
+
+ @Versioned
+ @OneToMany(mappedBy="reference")
+ @MapKey(name = "data")
+ private Map<String, RefIngMapKeyEntity> idmap;
+
+ public RefEdMapKeyEntity() {
+ idmap = new HashMap<String, RefIngMapKeyEntity>();
+ }
+
+ public Integer getId() {
+ return id;
+ }
+
+ public void setId(Integer id) {
+ this.id = id;
+ }
+
+ public Map<String, RefIngMapKeyEntity> getIdmap() {
+ return idmap;
+ }
+
+ public void setIdmap(Map<String, RefIngMapKeyEntity> idmap) {
+ this.idmap = idmap;
+ }
+
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (!(o instanceof RefEdMapKeyEntity)) return false;
+
+ RefEdMapKeyEntity that = (RefEdMapKeyEntity) o;
+
+ if (id != null ? !id.equals(that.id) : that.id != null) return false;
+
+ return true;
+ }
+
+ public int hashCode() {
+ return (id != null ? id.hashCode() : 0);
+ }
+
+ public String toString() {
+ return "RedMKE(id = " + id + ", idmap = " + idmap +
")";
+ }
+}
\ No newline at end of file
Property changes on:
trunk/src/test/org/jboss/envers/test/integration/onetomany/RefEdMapKeyEntity.java
___________________________________________________________________
Name: svn:mergeinfo
+
Copied: trunk/src/test/org/jboss/envers/test/integration/onetomany/RefIngMapKeyEntity.java
(from rev 165,
trunk/src/test/org/jboss/envers/test/integration/collection/mapkey/ComponentMapKeyEntity.java)
===================================================================
--- trunk/src/test/org/jboss/envers/test/integration/onetomany/RefIngMapKeyEntity.java
(rev 0)
+++
trunk/src/test/org/jboss/envers/test/integration/onetomany/RefIngMapKeyEntity.java 2008-09-24
10:42:17 UTC (rev 166)
@@ -0,0 +1,65 @@
+package org.jboss.envers.test.integration.onetomany;
+
+import org.jboss.envers.Versioned;
+
+import javax.persistence.*;
+
+/**
+ * @author Adam Warski (adam at warski dot org)
+ */
+@Entity
+public class RefIngMapKeyEntity {
+ @Id
+ @GeneratedValue
+ private Integer id;
+
+ @Versioned
+ @ManyToOne
+ private RefEdMapKeyEntity reference;
+
+ @Versioned
+ private String data;
+
+ public Integer getId() {
+ return id;
+ }
+
+ public void setId(Integer id) {
+ this.id = id;
+ }
+
+ public RefEdMapKeyEntity getReference() {
+ return reference;
+ }
+
+ public void setReference(RefEdMapKeyEntity reference) {
+ this.reference = reference;
+ }
+
+ 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 RefIngMapKeyEntity)) return false;
+
+ RefIngMapKeyEntity that = (RefIngMapKeyEntity) o;
+
+ if (id != null ? !id.equals(that.id) : that.id != null) return false;
+
+ return true;
+ }
+
+ public int hashCode() {
+ return (id != null ? id.hashCode() : 0);
+ }
+
+ public String toString() {
+ return "RingMKE(id = " + id + ", data = " + data + ",
reference = " + reference + ")";
+ }
+}
\ No newline at end of file
Property changes on:
trunk/src/test/org/jboss/envers/test/integration/onetomany/RefIngMapKeyEntity.java
___________________________________________________________________
Name: svn:mergeinfo
+