From jboss-envers-commits at lists.jboss.org Tue Sep 2 07:42:56 2008 From: jboss-envers-commits at lists.jboss.org (jboss-envers-commits at lists.jboss.org) Date: Tue, 02 Sep 2008 07:42:56 -0400 Subject: [jboss-envers-commits] JBoss Envers SVN: r137 - in trunk/src: main/org/jboss/envers/query and 7 other directories. Message-ID: Author: adamw Date: 2008-09-02 07:42:56 -0400 (Tue, 02 Sep 2008) New Revision: 137 Added: trunk/src/main/org/jboss/envers/query/impl/Parameters.java trunk/src/main/org/jboss/envers/query/impl/QueryBuilder.java trunk/src/main/org/jboss/envers/tools/MutableInteger.java trunk/src/main/org/jboss/envers/tools/Triple.java Removed: trunk/src/main/org/jboss/envers/query/VersionsProjections.java trunk/src/main/org/jboss/envers/query/criteria/IlikeVersionsExpression.java trunk/src/main/org/jboss/envers/query/criteria/PublicLogicalExpression.java trunk/src/main/org/jboss/envers/query/criteria/PublicPropertyExpression.java trunk/src/main/org/jboss/envers/query/criteria/PublicSimpleExpression.java trunk/src/main/org/jboss/envers/query/projection/DistinctVersionsProjection.java trunk/src/main/org/jboss/envers/query/projection/ProjectionWrapper.java trunk/src/main/org/jboss/envers/query/projection/VersionsProjectionList.java Modified: trunk/src/main/org/jboss/envers/entities/mapper/id/AbstractIdMapper.java trunk/src/main/org/jboss/envers/entities/mapper/id/IdMapper.java trunk/src/main/org/jboss/envers/query/RevisionProperty.java trunk/src/main/org/jboss/envers/query/RevisionTypeProperty.java trunk/src/main/org/jboss/envers/query/VersionsQuery.java trunk/src/main/org/jboss/envers/query/VersionsRestrictions.java trunk/src/main/org/jboss/envers/query/criteria/AggregatedFieldVersionsExpression.java trunk/src/main/org/jboss/envers/query/criteria/BetweenVersionsExpression.java trunk/src/main/org/jboss/envers/query/criteria/IdentifierEqVersionsExpression.java trunk/src/main/org/jboss/envers/query/criteria/InVersionsExpression.java trunk/src/main/org/jboss/envers/query/criteria/LogicalVersionsExpression.java trunk/src/main/org/jboss/envers/query/criteria/NotNullVersionsExpression.java trunk/src/main/org/jboss/envers/query/criteria/NotVersionsExpression.java trunk/src/main/org/jboss/envers/query/criteria/NullVersionsExpression.java trunk/src/main/org/jboss/envers/query/criteria/PropertyVersionsExpression.java trunk/src/main/org/jboss/envers/query/criteria/RelatedVersionsExpression.java trunk/src/main/org/jboss/envers/query/criteria/RevisionVersionsExpression.java trunk/src/main/org/jboss/envers/query/criteria/SimpleVersionsExpression.java trunk/src/main/org/jboss/envers/query/criteria/VersionsConjunction.java trunk/src/main/org/jboss/envers/query/criteria/VersionsCriterion.java trunk/src/main/org/jboss/envers/query/criteria/VersionsDisjunction.java trunk/src/main/org/jboss/envers/query/impl/AbstractVersionsQuery.java trunk/src/main/org/jboss/envers/query/impl/EntitiesAtRevisionQuery.java trunk/src/main/org/jboss/envers/query/impl/RevisionsOfEntityQuery.java trunk/src/main/org/jboss/envers/query/order/RevisionVersionsOrder.java trunk/src/main/org/jboss/envers/query/order/VersionsOrder.java trunk/src/main/org/jboss/envers/query/projection/RevisionVersionsProjection.java trunk/src/main/org/jboss/envers/query/projection/VersionsProjection.java trunk/src/main/org/jboss/envers/reader/VersionsReaderImpl.java trunk/src/main/org/jboss/envers/tools/StringTools.java trunk/src/test/org/jboss/envers/test/integration/query/AggregateQuery.java trunk/src/test/org/jboss/envers/test/integration/query/DeletedEntities.java trunk/src/test/org/jboss/envers/test/integration/query/MaximalizePropertyQuery.java trunk/src/test/org/jboss/envers/test/integration/query/OrderByLimitQuery.java trunk/src/test/org/jboss/envers/test/integration/query/RevisionConstraintQuery.java trunk/src/test/org/jboss/envers/test/integration/query/SimpleQuery.java Log: ENVERS-47: rewriting query implementation Modified: trunk/src/main/org/jboss/envers/entities/mapper/id/AbstractIdMapper.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/id/AbstractIdMapper.java 2008-08-31 08:46:36 UTC (rev 136) +++ trunk/src/main/org/jboss/envers/entities/mapper/id/AbstractIdMapper.java 2008-09-02 11:42:56 UTC (rev 137) @@ -21,9 +21,7 @@ */ package org.jboss.envers.entities.mapper.id; -import org.hibernate.criterion.Restrictions; -import org.hibernate.criterion.Criterion; -import org.hibernate.criterion.Conjunction; +import org.jboss.envers.query.impl.Parameters; import java.util.Iterator; import java.util.List; @@ -32,23 +30,34 @@ * @author Adam Warski (adam at warski dot org) */ public abstract class AbstractIdMapper implements IdMapper { - public Criterion getIdsEqualCriterion(String prefix1, String prefix2) { + public void addIdsEqualToQuery(Parameters parameters, String prefix1, String prefix2) { List paramDatas = mapToQueryParametersFromId(null); - if (paramDatas.size() == 1) { - QueryParameterData paramData = paramDatas.iterator().next(); - return Restrictions.eqProperty(paramData.getProperty(prefix1), - paramData.getProperty(prefix2)); + Parameters parametersToUse; + if (paramDatas.size() > 1) { + parametersToUse = parameters.addSubParameters("and"); } else { - Conjunction conjunction = Restrictions.conjunction(); + parametersToUse = parameters; + } - for (QueryParameterData paramData : paramDatas) { - conjunction.add(Restrictions.eqProperty(paramData.getProperty(prefix1), - paramData.getProperty(prefix2))); - } + for (QueryParameterData paramData : paramDatas) { + parametersToUse.addWhere(paramData.getProperty(prefix1), false, "=", paramData.getProperty(prefix2), false); + } + } - return conjunction; + public void addIdEqualsToQuery(Parameters parameters, Object id, String prefix, boolean equals) { + List paramDatas = mapToQueryParametersFromId(id); + + Parameters parametersToUse; + if (paramDatas.size() > 1) { + parametersToUse = parameters.addSubParameters("and"); + } else { + parametersToUse = parameters; } + + for (QueryParameterData paramData : paramDatas) { + parametersToUse.addWhereWithParam(paramData.getProperty(prefix), equals ? "=" : "<>", paramData.getValue()); + } } public String getIdsEqualQuery(String prefix1, String prefix2) { @@ -99,7 +108,7 @@ while (paramDataIter.hasNext()) { QueryParameterData paramData = paramDataIter.next(); - + query.append(paramData.getProperty(prefix)); if (equals) { query.append(" = "); @@ -107,7 +116,7 @@ query.append(" != "); } query.append(":").append(paramData.getQueryParameterName()); - + if (paramDataIter.hasNext()) { query.append(" AND "); } @@ -115,29 +124,4 @@ return query.toString(); } - - public Criterion getIdEqualsCriterion(Object id, String prefix, boolean equals) { - List paramDatas = mapToQueryParametersFromId(id); - - if (paramDatas.size() == 1) { - QueryParameterData paramData = paramDatas.iterator().next(); - if (equals) { - return Restrictions.eq(paramData.getProperty(prefix), paramData.getValue()); - } else { - return Restrictions.ne(paramData.getProperty(prefix), paramData.getValue()); - } - } else { - Conjunction conjunction = Restrictions.conjunction(); - - for (QueryParameterData paramData : paramDatas) { - if (equals) { - conjunction.add(Restrictions.eq(paramData.getProperty(prefix), paramData.getValue())); - } else { - conjunction.add(Restrictions.ne(paramData.getProperty(prefix), paramData.getValue())); - } - } - - return conjunction; - } - } } Modified: trunk/src/main/org/jboss/envers/entities/mapper/id/IdMapper.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/id/IdMapper.java 2008-08-31 08:46:36 UTC (rev 136) +++ trunk/src/main/org/jboss/envers/entities/mapper/id/IdMapper.java 2008-09-02 11:42:56 UTC (rev 137) @@ -21,7 +21,7 @@ */ package org.jboss.envers.entities.mapper.id; -import org.hibernate.criterion.Criterion; +import org.jboss.envers.query.impl.Parameters; import java.util.Map; import java.util.List; @@ -30,15 +30,15 @@ * @author Adam Warski (adam at warski dot org) */ public interface IdMapper { - public void mapToMapFromId(Map data, Object obj); + void mapToMapFromId(Map data, Object obj); - public void mapToMapFromEntity(Map data, Object obj); + void mapToMapFromEntity(Map data, Object obj); - public void mapToEntityFromMap(Object obj, Map data); + void mapToEntityFromMap(Object obj, Map data); - public Object mapToIdFromEntity(Object data); + Object mapToIdFromEntity(Object data); - public Object mapToIdFromMap(Map data); + Object mapToIdFromMap(Map data); /** * Creates a mapper with all mapped properties prefixed. A mapped property is a property which @@ -46,41 +46,41 @@ * @param prefix Prefix to add to mapped properties * @return A copy of the current property mapper, with mapped properties prefixed. */ - public IdMapper prefixMappedProperties(String prefix); + IdMapper prefixMappedProperties(String prefix); /** * @param obj Id from which to map. * @return A set parameter data, needed to build a query basing on the given id. */ - public List mapToQueryParametersFromId(Object obj); + List mapToQueryParametersFromId(Object obj); /** - * Gets a criteria object, which contains restrictions, which express the property that the id of the entity + * Gets a query string, which contains equalities, which express the property that the id of the entity * with alias prefix1, is equal to the id of the entity with alias prefix2 (the entity is the same). * @param prefix1 First alias of the entity. * @param prefix2 Second alias of the entity. - * @return A criterion object expressing the property described above. + * @return A query string expressing the property described above. */ - public Criterion getIdsEqualCriterion(String prefix1, String prefix2); + String getIdsEqualQuery(String prefix1, String prefix2); /** - * Gets a query string, which contains equalities, which express the property that the id of the entity + * Adds query statements, which contains restrictions, which express the property that the id of the entity * with alias prefix1, is equal to the id of the entity with alias prefix2 (the entity is the same). - * @param prefix1 First alias of the entity. - * @param prefix2 Second alias of the entity. - * @return A query string expressing the property described above. + * @param parameters Parameters, to which to add the statements. + * @param prefix1 First alias of the entity + prefix to add to the properties. + * @param prefix2 Second alias of the entity + prefix to add to the properties. */ - public String getIdsEqualQuery(String prefix1, String prefix2); + void addIdsEqualToQuery(Parameters parameters, String prefix1, String prefix2); /** - * Gets a criteria object, which contains restrictions, which express the property that the id of the entity + * Adds query statements, which contains restrictions, which express the property that the id of the entity * with alias prefix, is equal to the given object. + * @param parameters Parameters, to which to add the statements. * @param id Value of id. - * @param prefix Alias of the entity (may be null). - * @param equals Should this criteria express the "=" relation or the "<>" relation. - * @return A criterion object expressing the property described above. + * @param prefix Prefix to add to the properties (may be null). + * @param equals Should this query express the "=" relation or the "<>" relation. */ - public Criterion getIdEqualsCriterion(Object id, String prefix, boolean equals); + void addIdEqualsToQuery(Parameters parameters, Object id, String prefix, boolean equals); /** * Gets a query string, which contains equalities, which express the property that the id of the entity @@ -90,7 +90,7 @@ * @param equals Should this query express the "=" relation or the "<>" relation. * @return A query string expressing the property described above. */ - public String getIdEqualsQuery(String prefix, boolean equals); + String getIdEqualsQuery(String prefix, boolean equals); /** * Gets a query string, which contains equalities, which express the property that the id of the entity @@ -102,5 +102,5 @@ * @param prefix2 Second alias of the entity. * @return A query string expressing the property described above. */ - public String getIdsEqualQuery(String prefix1, IdMapper mapper2, String prefix2); + String getIdsEqualQuery(String prefix1, IdMapper mapper2, String prefix2); } Modified: trunk/src/main/org/jboss/envers/query/RevisionProperty.java =================================================================== --- trunk/src/main/org/jboss/envers/query/RevisionProperty.java 2008-08-31 08:46:36 UTC (rev 136) +++ trunk/src/main/org/jboss/envers/query/RevisionProperty.java 2008-09-02 11:42:56 UTC (rev 137) @@ -28,8 +28,7 @@ import org.jboss.envers.query.projection.VersionsProjection; import org.jboss.envers.query.projection.RevisionVersionsProjection; import org.jboss.envers.configuration.VersionsConfiguration; -import org.hibernate.criterion.Projection; -import org.hibernate.criterion.Projections; +import org.jboss.envers.tools.Triple; /** * @author Adam Warski (adam at warski dot org) @@ -109,13 +108,20 @@ } /** + * Distinct revisions + */ + public static VersionsProjection distinct() { + return new RevisionVersionsProjection(RevisionVersionsProjection.ProjectionType.DISTINCT); + } + + /** * Select the revision number */ public static VersionsProjection revisionNumber() { return new RevisionProperty(); } - public Projection getProjection(VersionsConfiguration verCfg, String entityName) { - return Projections.property(verCfg.getVerEntCfg().getRevisionPropPath()); + public Triple getProjectionData(VersionsConfiguration verCfg) { + return Triple.make(null, verCfg.getVerEntCfg().getRevisionPropPath(), false); } } Modified: trunk/src/main/org/jboss/envers/query/RevisionTypeProperty.java =================================================================== --- trunk/src/main/org/jboss/envers/query/RevisionTypeProperty.java 2008-08-31 08:46:36 UTC (rev 136) +++ trunk/src/main/org/jboss/envers/query/RevisionTypeProperty.java 2008-09-02 11:42:56 UTC (rev 137) @@ -23,8 +23,7 @@ import org.jboss.envers.query.projection.VersionsProjection; import org.jboss.envers.configuration.VersionsConfiguration; -import org.hibernate.criterion.Projection; -import org.hibernate.criterion.Projections; +import org.jboss.envers.tools.Triple; /** * @author Adam Warski (adam at warski dot org) @@ -40,7 +39,7 @@ return new RevisionTypeProperty(); } - public Projection getProjection(VersionsConfiguration verCfg, String entityName) { - return Projections.property(verCfg.getVerEntCfg().getRevisionTypePropName()); + public Triple getProjectionData(VersionsConfiguration verCfg) { + return Triple.make(null, verCfg.getVerEntCfg().getRevisionTypePropName(), false); } } \ No newline at end of file Deleted: trunk/src/main/org/jboss/envers/query/VersionsProjections.java =================================================================== --- trunk/src/main/org/jboss/envers/query/VersionsProjections.java 2008-08-31 08:46:36 UTC (rev 136) +++ trunk/src/main/org/jboss/envers/query/VersionsProjections.java 2008-09-02 11:42:56 UTC (rev 137) @@ -1,58 +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.query; - -import org.jboss.envers.query.projection.VersionsProjection; -import org.jboss.envers.query.projection.DistinctVersionsProjection; -import org.jboss.envers.query.projection.ProjectionWrapper; -import org.jboss.envers.query.projection.VersionsProjectionList; -import org.hibernate.criterion.Projection; - -/** - * @author Adam Warski (adam at warski dot org) - */ - at SuppressWarnings({"JavaDoc"}) -public class VersionsProjections { - /** - * A distinct projection on a given versions projection. - * @param projection - * @return - */ - public static VersionsProjection distinct(VersionsProjection projection) { - return new DistinctVersionsProjection(projection); - } - - /** - * A distinct projection on a given hibernate projection, which can be later used together with - * other versions projections (for example in a list). - */ - public static VersionsProjection distinct(Projection projection) { - return new DistinctVersionsProjection(new ProjectionWrapper(projection)); - } - - /** - * A list of versions and hibernate projections. - */ - public static VersionsProjectionList projectionList() { - return new VersionsProjectionList(); - } -} Modified: trunk/src/main/org/jboss/envers/query/VersionsQuery.java =================================================================== --- trunk/src/main/org/jboss/envers/query/VersionsQuery.java 2008-08-31 08:46:36 UTC (rev 136) +++ trunk/src/main/org/jboss/envers/query/VersionsQuery.java 2008-09-02 11:42:56 UTC (rev 137) @@ -25,8 +25,6 @@ import org.jboss.envers.query.projection.VersionsProjection; import org.jboss.envers.query.order.VersionsOrder; import org.jboss.envers.exception.VersionsException; -import org.hibernate.criterion.Projection; -import org.hibernate.criterion.Order; import org.hibernate.FlushMode; import org.hibernate.CacheMode; import org.hibernate.LockMode; @@ -46,11 +44,11 @@ VersionsQuery add(VersionsCriterion criterion); - VersionsQuery setProjection(Projection projection); + VersionsQuery addProjection(String function, String propertyName); - VersionsQuery setProjection(VersionsProjection projection); + VersionsQuery addProjection(VersionsProjection projection); - VersionsQuery addOrder(Order order); + VersionsQuery addOrder(String propertyName, boolean asc); VersionsQuery addOrder(VersionsOrder order); Modified: trunk/src/main/org/jboss/envers/query/VersionsRestrictions.java =================================================================== --- trunk/src/main/org/jboss/envers/query/VersionsRestrictions.java 2008-08-31 08:46:36 UTC (rev 136) +++ trunk/src/main/org/jboss/envers/query/VersionsRestrictions.java 2008-09-02 11:42:56 UTC (rev 137) @@ -27,6 +27,7 @@ import java.util.Collection; /** + * TODO: ilike * @author Adam Warski (adam at warski dot org) * @see org.hibernate.criterion.Restrictions */ @@ -84,22 +85,6 @@ } /** - * A case-insensitive "like", similar to Postgres ilike - * operator - */ - public static VersionsCriterion ilike(String propertyName, String value, MatchMode matchMode) { - return new IlikeVersionsExpression(propertyName, value, matchMode); - } - - /** - * A case-insensitive "like", similar to Postgres ilike - * operator - */ - public static VersionsCriterion ilike(String propertyName, Object value) { - return new IlikeVersionsExpression(propertyName, value); - } - - /** * Apply a "greater than" constraint to the named property */ public static VersionsCriterion gt(String propertyName, Object value) { Modified: trunk/src/main/org/jboss/envers/query/criteria/AggregatedFieldVersionsExpression.java =================================================================== --- trunk/src/main/org/jboss/envers/query/criteria/AggregatedFieldVersionsExpression.java 2008-08-31 08:46:36 UTC (rev 136) +++ trunk/src/main/org/jboss/envers/query/criteria/AggregatedFieldVersionsExpression.java 2008-09-02 11:42:56 UTC (rev 137) @@ -21,9 +21,9 @@ */ package org.jboss.envers.query.criteria; -import org.hibernate.criterion.*; -import org.jboss.envers.exception.VersionsException; import org.jboss.envers.configuration.VersionsConfiguration; +import org.jboss.envers.query.impl.QueryBuilder; +import org.jboss.envers.query.impl.Parameters; import java.util.List; import java.util.ArrayList; @@ -52,40 +52,29 @@ return this; } - public Criterion toVersionsCriterion(VersionsConfiguration verCfg, String entityName) - throws VersionsException { + public void addToQuery(VersionsConfiguration verCfg, String entityName, QueryBuilder qb, Parameters parameters) { CriteriaTools.checkPropertyNotARelation(verCfg, entityName, propertyName); - String versionsEntityName = verCfg.getVerEntCfg().getVersionsEntityName(entityName); + // This will be the aggregated query, containing all the specified conditions + QueryBuilder subQb = qb.newSubQueryBuilder(); - // This will be the aggregated criteria, containing all the specified conditions - DetachedCriteria aggregatedCriteria = DetachedCriteria.forEntityName(versionsEntityName); - - // This will contain all the specified conditions and an equal constraing on the result of the - // aggregated criteria. - Conjunction conjunction = Restrictions.conjunction(); - - // First adding all specified conditions both to the main criteria, as well as to the + // Adding all specified conditions both to the main query, as well as to the // aggregated one. for (VersionsCriterion versionsCriteria : criterions) { - Criterion transformedCriterion = versionsCriteria.toVersionsCriterion(verCfg, entityName); - - conjunction.add(transformedCriterion); - aggregatedCriteria.add(transformedCriterion); + versionsCriteria.addToQuery(verCfg, entityName, qb, parameters); + versionsCriteria.addToQuery(verCfg, entityName, subQb, subQb.getRootParameters()); } - // Setting the desired projection of the aggregated criteria + // Setting the desired projection of the aggregated query switch (mode) { case MIN: - aggregatedCriteria.setProjection(Property.forName(propertyName).min()); + subQb.addProjection("min", propertyName); break; case MAX: - aggregatedCriteria.setProjection(Property.forName(propertyName).max()); - } + subQb.addProjection("max", propertyName); + } // Adding the constrain on the result of the aggregated criteria - conjunction.add(Property.forName(propertyName).eq(aggregatedCriteria)); - - return conjunction; + parameters.addWhere(propertyName, "=", subQb); } } \ No newline at end of file Modified: trunk/src/main/org/jboss/envers/query/criteria/BetweenVersionsExpression.java =================================================================== --- trunk/src/main/org/jboss/envers/query/criteria/BetweenVersionsExpression.java 2008-08-31 08:46:36 UTC (rev 136) +++ trunk/src/main/org/jboss/envers/query/criteria/BetweenVersionsExpression.java 2008-09-02 11:42:56 UTC (rev 137) @@ -21,10 +21,9 @@ */ package org.jboss.envers.query.criteria; -import org.hibernate.criterion.Criterion; -import org.hibernate.criterion.Restrictions; -import org.jboss.envers.exception.VersionsException; import org.jboss.envers.configuration.VersionsConfiguration; +import org.jboss.envers.query.impl.QueryBuilder; +import org.jboss.envers.query.impl.Parameters; /** * @author Adam Warski (adam at warski dot org) @@ -40,9 +39,9 @@ this.hi = hi; } - public Criterion toVersionsCriterion(VersionsConfiguration verCfg, String entityName) - throws VersionsException { + public void addToQuery(VersionsConfiguration verCfg, String entityName, QueryBuilder qb, Parameters parameters) { CriteriaTools.checkPropertyNotARelation(verCfg, entityName, propertyName); - return Restrictions.between(propertyName, lo, hi); + parameters.addWhereWithParam(propertyName, ">=", lo); + parameters.addWhereWithParam(propertyName, "<=", hi); } } Modified: trunk/src/main/org/jboss/envers/query/criteria/IdentifierEqVersionsExpression.java =================================================================== --- trunk/src/main/org/jboss/envers/query/criteria/IdentifierEqVersionsExpression.java 2008-08-31 08:46:36 UTC (rev 136) +++ trunk/src/main/org/jboss/envers/query/criteria/IdentifierEqVersionsExpression.java 2008-09-02 11:42:56 UTC (rev 137) @@ -21,9 +21,9 @@ */ package org.jboss.envers.query.criteria; -import org.hibernate.criterion.Criterion; -import org.jboss.envers.exception.VersionsException; import org.jboss.envers.configuration.VersionsConfiguration; +import org.jboss.envers.query.impl.QueryBuilder; +import org.jboss.envers.query.impl.Parameters; /** * @author Adam Warski (adam at warski dot org) @@ -35,9 +35,8 @@ this.id = id; } - public Criterion toVersionsCriterion(VersionsConfiguration verCfg, String entityName) - throws VersionsException { - return verCfg.getEntCfg().get(entityName).getIdMapper() - .getIdEqualsCriterion(id, verCfg.getVerEntCfg().getOriginalIdPropName(), true); + public void addToQuery(VersionsConfiguration verCfg, String entityName, QueryBuilder qb, Parameters parameters) { + verCfg.getEntCfg().get(entityName).getIdMapper() + .addIdEqualsToQuery(parameters, id, verCfg.getVerEntCfg().getOriginalIdPropName(), true); } } Deleted: trunk/src/main/org/jboss/envers/query/criteria/IlikeVersionsExpression.java =================================================================== --- trunk/src/main/org/jboss/envers/query/criteria/IlikeVersionsExpression.java 2008-08-31 08:46:36 UTC (rev 136) +++ trunk/src/main/org/jboss/envers/query/criteria/IlikeVersionsExpression.java 2008-09-02 11:42:56 UTC (rev 137) @@ -1,51 +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.query.criteria; - -import org.hibernate.criterion.Criterion; -import org.hibernate.criterion.MatchMode; -import org.hibernate.criterion.Restrictions; -import org.jboss.envers.exception.VersionsException; -import org.jboss.envers.configuration.VersionsConfiguration; - -/** - * @author Adam Warski (adam at warski dot org) - */ -public class IlikeVersionsExpression implements VersionsCriterion { - private final String propertyName; - private final Object value; - - public IlikeVersionsExpression(String propertyName, Object value) { - this.propertyName = propertyName; - this.value = value; - } - - public IlikeVersionsExpression(String propertyName, String value, MatchMode matchMode) { - this(propertyName, matchMode.toMatchString(value)); - } - - public Criterion toVersionsCriterion(VersionsConfiguration verCfg, String entityName) - throws VersionsException { - CriteriaTools.checkPropertyNotARelation(verCfg, entityName, propertyName); - return Restrictions.ilike(propertyName, value); - } -} Modified: trunk/src/main/org/jboss/envers/query/criteria/InVersionsExpression.java =================================================================== --- trunk/src/main/org/jboss/envers/query/criteria/InVersionsExpression.java 2008-08-31 08:46:36 UTC (rev 136) +++ trunk/src/main/org/jboss/envers/query/criteria/InVersionsExpression.java 2008-09-02 11:42:56 UTC (rev 137) @@ -21,10 +21,9 @@ */ package org.jboss.envers.query.criteria; -import org.hibernate.criterion.Criterion; -import org.hibernate.criterion.Restrictions; -import org.jboss.envers.exception.VersionsException; import org.jboss.envers.configuration.VersionsConfiguration; +import org.jboss.envers.query.impl.QueryBuilder; +import org.jboss.envers.query.impl.Parameters; /** * @author Adam Warski (adam at warski dot org) @@ -38,9 +37,8 @@ this.values = values; } - public Criterion toVersionsCriterion(VersionsConfiguration verCfg, String entityName) - throws VersionsException { + public void addToQuery(VersionsConfiguration verCfg, String entityName, QueryBuilder qb, Parameters parameters) { CriteriaTools.checkPropertyNotARelation(verCfg, entityName, propertyName); - return Restrictions.in(propertyName, values); + parameters.addWhereWithParams(propertyName, "in (", values, ")"); } } Modified: trunk/src/main/org/jboss/envers/query/criteria/LogicalVersionsExpression.java =================================================================== --- trunk/src/main/org/jboss/envers/query/criteria/LogicalVersionsExpression.java 2008-08-31 08:46:36 UTC (rev 136) +++ trunk/src/main/org/jboss/envers/query/criteria/LogicalVersionsExpression.java 2008-09-02 11:42:56 UTC (rev 137) @@ -21,9 +21,9 @@ */ package org.jboss.envers.query.criteria; -import org.hibernate.criterion.Criterion; -import org.jboss.envers.exception.VersionsException; import org.jboss.envers.configuration.VersionsConfiguration; +import org.jboss.envers.query.impl.QueryBuilder; +import org.jboss.envers.query.impl.Parameters; /** * @author Adam Warski (adam at warski dot org) @@ -39,9 +39,10 @@ this.op = op; } - public Criterion toVersionsCriterion(VersionsConfiguration verCfg, String entityName) - throws VersionsException { - return new PublicLogicalExpression(lhs.toVersionsCriterion(verCfg, null), - rhs.toVersionsCriterion(verCfg, null), op); + public void addToQuery(VersionsConfiguration verCfg, String entityName, QueryBuilder qb, Parameters parameters) { + Parameters opParameters = parameters.addSubParameters(op); + + lhs.addToQuery(verCfg, entityName, qb, opParameters.addSubParameters("and")); + rhs.addToQuery(verCfg, entityName, qb, opParameters.addSubParameters("and")); } } Modified: trunk/src/main/org/jboss/envers/query/criteria/NotNullVersionsExpression.java =================================================================== --- trunk/src/main/org/jboss/envers/query/criteria/NotNullVersionsExpression.java 2008-08-31 08:46:36 UTC (rev 136) +++ trunk/src/main/org/jboss/envers/query/criteria/NotNullVersionsExpression.java 2008-09-02 11:42:56 UTC (rev 137) @@ -21,11 +21,10 @@ */ package org.jboss.envers.query.criteria; -import org.hibernate.criterion.Criterion; -import org.hibernate.criterion.Restrictions; -import org.jboss.envers.exception.VersionsException; import org.jboss.envers.entities.RelationDescription; import org.jboss.envers.configuration.VersionsConfiguration; +import org.jboss.envers.query.impl.QueryBuilder; +import org.jboss.envers.query.impl.Parameters; /** * @author Adam Warski (adam at warski dot org) @@ -37,14 +36,13 @@ this.propertyName = propertyName; } - public Criterion toVersionsCriterion(VersionsConfiguration verCfg, String entityName) - throws VersionsException { + public void addToQuery(VersionsConfiguration verCfg, String entityName, QueryBuilder qb, Parameters parameters) { RelationDescription relatedEntity = CriteriaTools.getRelatedEntity(verCfg, entityName, propertyName); if (relatedEntity == null) { - return Restrictions.isNotNull(propertyName); + parameters.addWhereWithParam(propertyName, "<>", null); } else { - return relatedEntity.getIdMapper().getIdEqualsCriterion(null, propertyName, false); + relatedEntity.getIdMapper().addIdEqualsToQuery(parameters, null, propertyName, false); } } } Modified: trunk/src/main/org/jboss/envers/query/criteria/NotVersionsExpression.java =================================================================== --- trunk/src/main/org/jboss/envers/query/criteria/NotVersionsExpression.java 2008-08-31 08:46:36 UTC (rev 136) +++ trunk/src/main/org/jboss/envers/query/criteria/NotVersionsExpression.java 2008-09-02 11:42:56 UTC (rev 137) @@ -21,10 +21,9 @@ */ package org.jboss.envers.query.criteria; -import org.hibernate.criterion.Criterion; -import org.hibernate.criterion.Restrictions; -import org.jboss.envers.exception.VersionsException; import org.jboss.envers.configuration.VersionsConfiguration; +import org.jboss.envers.query.impl.QueryBuilder; +import org.jboss.envers.query.impl.Parameters; /** * @author Adam Warski (adam at warski dot org) @@ -36,8 +35,7 @@ this.criterion = criterion; } - public Criterion toVersionsCriterion(VersionsConfiguration verCfg, String entityName) - throws VersionsException { - return Restrictions.not(criterion.toVersionsCriterion(verCfg, null)); + public void addToQuery(VersionsConfiguration verCfg, String entityName, QueryBuilder qb, Parameters parameters) { + criterion.addToQuery(verCfg, entityName, qb, parameters.addNegatedParameters()); } } Modified: trunk/src/main/org/jboss/envers/query/criteria/NullVersionsExpression.java =================================================================== --- trunk/src/main/org/jboss/envers/query/criteria/NullVersionsExpression.java 2008-08-31 08:46:36 UTC (rev 136) +++ trunk/src/main/org/jboss/envers/query/criteria/NullVersionsExpression.java 2008-09-02 11:42:56 UTC (rev 137) @@ -21,11 +21,10 @@ */ package org.jboss.envers.query.criteria; -import org.hibernate.criterion.Criterion; -import org.hibernate.criterion.Restrictions; -import org.jboss.envers.exception.VersionsException; import org.jboss.envers.entities.RelationDescription; import org.jboss.envers.configuration.VersionsConfiguration; +import org.jboss.envers.query.impl.QueryBuilder; +import org.jboss.envers.query.impl.Parameters; /** * @author Adam Warski (adam at warski dot org) @@ -37,14 +36,13 @@ this.propertyName = propertyName; } - public Criterion toVersionsCriterion(VersionsConfiguration verCfg, String entityName) - throws VersionsException { + public void addToQuery(VersionsConfiguration verCfg, String entityName, QueryBuilder qb, Parameters parameters) { RelationDescription relatedEntity = CriteriaTools.getRelatedEntity(verCfg, entityName, propertyName); if (relatedEntity == null) { - return Restrictions.isNull(propertyName); + parameters.addWhereWithParam(propertyName, "=", null); } else { - return relatedEntity.getIdMapper().getIdEqualsCriterion(null, propertyName, true); + relatedEntity.getIdMapper().addIdEqualsToQuery(parameters, null, propertyName, true); } } } Modified: trunk/src/main/org/jboss/envers/query/criteria/PropertyVersionsExpression.java =================================================================== --- trunk/src/main/org/jboss/envers/query/criteria/PropertyVersionsExpression.java 2008-08-31 08:46:36 UTC (rev 136) +++ trunk/src/main/org/jboss/envers/query/criteria/PropertyVersionsExpression.java 2008-09-02 11:42:56 UTC (rev 137) @@ -21,9 +21,9 @@ */ package org.jboss.envers.query.criteria; -import org.hibernate.criterion.Criterion; -import org.jboss.envers.exception.VersionsException; import org.jboss.envers.configuration.VersionsConfiguration; +import org.jboss.envers.query.impl.QueryBuilder; +import org.jboss.envers.query.impl.Parameters; /** * @author Adam Warski (adam at warski dot org) @@ -39,10 +39,9 @@ this.op = op; } - public Criterion toVersionsCriterion(VersionsConfiguration verCfg, String entityName) - throws VersionsException { + public void addToQuery(VersionsConfiguration verCfg, String entityName, QueryBuilder qb, Parameters parameters) { CriteriaTools.checkPropertyNotARelation(verCfg, entityName, propertyName); CriteriaTools.checkPropertyNotARelation(verCfg, entityName, otherPropertyName); - return new PublicPropertyExpression(propertyName, otherPropertyName, op); + parameters.addWhere(propertyName, op, otherPropertyName); } } Deleted: trunk/src/main/org/jboss/envers/query/criteria/PublicLogicalExpression.java =================================================================== --- trunk/src/main/org/jboss/envers/query/criteria/PublicLogicalExpression.java 2008-08-31 08:46:36 UTC (rev 136) +++ trunk/src/main/org/jboss/envers/query/criteria/PublicLogicalExpression.java 2008-09-02 11:42:56 UTC (rev 137) @@ -1,35 +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.query.criteria; - -import org.hibernate.criterion.LogicalExpression; -import org.hibernate.criterion.Criterion; - -/** - * A class which exposes LogicalExpression's protected constructor as public. - * @author Adam Warski (adam at warski dot org) - */ -public class PublicLogicalExpression extends LogicalExpression { - protected PublicLogicalExpression(Criterion lhs, Criterion rhs, String op) { - super(lhs, rhs, op); - } -} Deleted: trunk/src/main/org/jboss/envers/query/criteria/PublicPropertyExpression.java =================================================================== --- trunk/src/main/org/jboss/envers/query/criteria/PublicPropertyExpression.java 2008-08-31 08:46:36 UTC (rev 136) +++ trunk/src/main/org/jboss/envers/query/criteria/PublicPropertyExpression.java 2008-09-02 11:42:56 UTC (rev 137) @@ -1,34 +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.query.criteria; - -import org.hibernate.criterion.PropertyExpression; - -/** - * A class which exposes PropertyExpression's protected constructor as public. - * @author Adam Warski (adam at warski dot org) - */ -public class PublicPropertyExpression extends PropertyExpression { - public PublicPropertyExpression(String propertyName, String otherPropertyName, String op) { - super(propertyName, otherPropertyName, op); - } -} \ No newline at end of file Deleted: trunk/src/main/org/jboss/envers/query/criteria/PublicSimpleExpression.java =================================================================== --- trunk/src/main/org/jboss/envers/query/criteria/PublicSimpleExpression.java 2008-08-31 08:46:36 UTC (rev 136) +++ trunk/src/main/org/jboss/envers/query/criteria/PublicSimpleExpression.java 2008-09-02 11:42:56 UTC (rev 137) @@ -1,34 +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.query.criteria; - -import org.hibernate.criterion.SimpleExpression; - -/** - * A class which exposes SimpleExpression's protected constructor as public. - * @author Adam Warski (adam at warski dot org) - */ -public class PublicSimpleExpression extends SimpleExpression { - public PublicSimpleExpression(String propertyName, Object value, String op, boolean ignoreCase) { - super(propertyName, value, op, ignoreCase); - } -} Modified: trunk/src/main/org/jboss/envers/query/criteria/RelatedVersionsExpression.java =================================================================== --- trunk/src/main/org/jboss/envers/query/criteria/RelatedVersionsExpression.java 2008-08-31 08:46:36 UTC (rev 136) +++ trunk/src/main/org/jboss/envers/query/criteria/RelatedVersionsExpression.java 2008-09-02 11:42:56 UTC (rev 137) @@ -21,10 +21,11 @@ */ package org.jboss.envers.query.criteria; -import org.hibernate.criterion.Criterion; import org.jboss.envers.exception.VersionsException; import org.jboss.envers.entities.RelationDescription; import org.jboss.envers.configuration.VersionsConfiguration; +import org.jboss.envers.query.impl.QueryBuilder; +import org.jboss.envers.query.impl.Parameters; /** * @author Adam Warski (adam at warski dot org) @@ -40,15 +41,14 @@ this.equals = equals; } - public Criterion toVersionsCriterion(VersionsConfiguration verCfg, String entityName) - throws VersionsException { + public void addToQuery(VersionsConfiguration verCfg, String entityName, QueryBuilder qb, Parameters parameters) { RelationDescription relatedEntity = CriteriaTools.getRelatedEntity(verCfg, entityName, propertyName); if (relatedEntity == null) { throw new VersionsException("This criterion can only be used on a property that is " + "a relation to another property."); } else { - return relatedEntity.getIdMapper().getIdEqualsCriterion(id, propertyName, equals); + relatedEntity.getIdMapper().addIdEqualsToQuery(parameters, id, propertyName, equals); } } } \ No newline at end of file Modified: trunk/src/main/org/jboss/envers/query/criteria/RevisionVersionsExpression.java =================================================================== --- trunk/src/main/org/jboss/envers/query/criteria/RevisionVersionsExpression.java 2008-08-31 08:46:36 UTC (rev 136) +++ trunk/src/main/org/jboss/envers/query/criteria/RevisionVersionsExpression.java 2008-09-02 11:42:56 UTC (rev 137) @@ -21,9 +21,9 @@ */ package org.jboss.envers.query.criteria; -import org.hibernate.criterion.Criterion; -import org.jboss.envers.exception.VersionsException; import org.jboss.envers.configuration.VersionsConfiguration; +import org.jboss.envers.query.impl.QueryBuilder; +import org.jboss.envers.query.impl.Parameters; /** * @author Adam Warski (adam at warski dot org) @@ -37,9 +37,7 @@ this.op = op; } - public Criterion toVersionsCriterion(VersionsConfiguration verCfg, String entityName) - throws VersionsException { - return new PublicSimpleExpression(verCfg.getVerEntCfg().getRevisionPropPath(), - value, op, false); + public void addToQuery(VersionsConfiguration verCfg, String entityName, QueryBuilder qb, Parameters parameters) { + parameters.addWhereWithParam(verCfg.getVerEntCfg().getRevisionPropPath(), op, value); } } \ No newline at end of file Modified: trunk/src/main/org/jboss/envers/query/criteria/SimpleVersionsExpression.java =================================================================== --- trunk/src/main/org/jboss/envers/query/criteria/SimpleVersionsExpression.java 2008-08-31 08:46:36 UTC (rev 136) +++ trunk/src/main/org/jboss/envers/query/criteria/SimpleVersionsExpression.java 2008-09-02 11:42:56 UTC (rev 137) @@ -21,10 +21,11 @@ */ package org.jboss.envers.query.criteria; -import org.hibernate.criterion.Criterion; import org.jboss.envers.exception.VersionsException; import org.jboss.envers.entities.RelationDescription; import org.jboss.envers.configuration.VersionsConfiguration; +import org.jboss.envers.query.impl.QueryBuilder; +import org.jboss.envers.query.impl.Parameters; /** * @author Adam Warski (adam at warski dot org) @@ -33,7 +34,6 @@ private String propertyName; private Object value; private String op; - private boolean ignoreCase; public SimpleVersionsExpression(String propertyName, Object value, String op) { this.propertyName = propertyName; @@ -41,19 +41,11 @@ this.op = op; } - public SimpleVersionsExpression(String propertyName, Object value, String op, boolean ignoreCase) { - this.propertyName = propertyName; - this.value = value; - this.op = op; - this.ignoreCase = ignoreCase; - } - - public Criterion toVersionsCriterion(VersionsConfiguration verCfg, String entityName) - throws VersionsException { + public void addToQuery(VersionsConfiguration verCfg, String entityName, QueryBuilder qb, Parameters parameters) { RelationDescription relatedEntity = CriteriaTools.getRelatedEntity(verCfg, entityName, propertyName); if (relatedEntity == null) { - return new PublicSimpleExpression(propertyName, value, op, ignoreCase); + parameters.addWhereWithParam(propertyName, op, value); } else { if (!"=".equals(op) && !"<>".equals(op)) { throw new VersionsException("This type of operation: " + op + " (" + entityName + "." + propertyName + @@ -62,7 +54,7 @@ Object id = relatedEntity.getIdMapper().mapToIdFromEntity(value); - return relatedEntity.getIdMapper().getIdEqualsCriterion(id, propertyName, "=".equals(op)); + relatedEntity.getIdMapper().addIdEqualsToQuery(parameters, id, propertyName, "=".equals(op)); } } } Modified: trunk/src/main/org/jboss/envers/query/criteria/VersionsConjunction.java =================================================================== --- trunk/src/main/org/jboss/envers/query/criteria/VersionsConjunction.java 2008-08-31 08:46:36 UTC (rev 136) +++ trunk/src/main/org/jboss/envers/query/criteria/VersionsConjunction.java 2008-09-02 11:42:56 UTC (rev 137) @@ -21,11 +21,9 @@ */ package org.jboss.envers.query.criteria; -import org.hibernate.criterion.Criterion; -import org.hibernate.criterion.Conjunction; -import org.hibernate.criterion.Restrictions; -import org.jboss.envers.exception.VersionsException; import org.jboss.envers.configuration.VersionsConfiguration; +import org.jboss.envers.query.impl.QueryBuilder; +import org.jboss.envers.query.impl.Parameters; import java.util.ArrayList; import java.util.List; @@ -45,14 +43,11 @@ return this; } - public Criterion toVersionsCriterion(VersionsConfiguration verCfg, String entityName) - throws VersionsException { - Conjunction conjunction = Restrictions.conjunction(); + public void addToQuery(VersionsConfiguration verCfg, String entityName, QueryBuilder qb, Parameters parameters) { + Parameters andParameters = parameters.addSubParameters("and"); for (VersionsCriterion criterion : criterions) { - conjunction.add(criterion.toVersionsCriterion(verCfg, null)); + criterion.addToQuery(verCfg, entityName, qb, andParameters); } - - return conjunction; } } Modified: trunk/src/main/org/jboss/envers/query/criteria/VersionsCriterion.java =================================================================== --- trunk/src/main/org/jboss/envers/query/criteria/VersionsCriterion.java 2008-08-31 08:46:36 UTC (rev 136) +++ trunk/src/main/org/jboss/envers/query/criteria/VersionsCriterion.java 2008-09-02 11:42:56 UTC (rev 137) @@ -21,13 +21,13 @@ */ package org.jboss.envers.query.criteria; -import org.hibernate.criterion.Criterion; -import org.jboss.envers.exception.VersionsException; import org.jboss.envers.configuration.VersionsConfiguration; +import org.jboss.envers.query.impl.QueryBuilder; +import org.jboss.envers.query.impl.Parameters; /** * @author Adam Warski (adam at warski dot org) */ public interface VersionsCriterion { - Criterion toVersionsCriterion(VersionsConfiguration verCfg, String entityName) throws VersionsException; + void addToQuery(VersionsConfiguration verCfg, String entityName, QueryBuilder qb, Parameters parameters); } Modified: trunk/src/main/org/jboss/envers/query/criteria/VersionsDisjunction.java =================================================================== --- trunk/src/main/org/jboss/envers/query/criteria/VersionsDisjunction.java 2008-08-31 08:46:36 UTC (rev 136) +++ trunk/src/main/org/jboss/envers/query/criteria/VersionsDisjunction.java 2008-09-02 11:42:56 UTC (rev 137) @@ -21,11 +21,9 @@ */ package org.jboss.envers.query.criteria; -import org.hibernate.criterion.Disjunction; -import org.hibernate.criterion.Criterion; -import org.hibernate.criterion.Restrictions; -import org.jboss.envers.exception.VersionsException; import org.jboss.envers.configuration.VersionsConfiguration; +import org.jboss.envers.query.impl.QueryBuilder; +import org.jboss.envers.query.impl.Parameters; import java.util.List; import java.util.ArrayList; @@ -45,14 +43,11 @@ return this; } - public Criterion toVersionsCriterion(VersionsConfiguration verCfg, String entityName) - throws VersionsException { - Disjunction conjunction = Restrictions.disjunction(); + public void addToQuery(VersionsConfiguration verCfg, String entityName, QueryBuilder qb, Parameters parameters) { + Parameters orParameters = parameters.addSubParameters("or"); for (VersionsCriterion criterion : criterions) { - conjunction.add(criterion.toVersionsCriterion(verCfg, null)); + criterion.addToQuery(verCfg, entityName, qb, orParameters); } - - return conjunction; } } \ No newline at end of file Modified: trunk/src/main/org/jboss/envers/query/impl/AbstractVersionsQuery.java =================================================================== --- trunk/src/main/org/jboss/envers/query/impl/AbstractVersionsQuery.java 2008-08-31 08:46:36 UTC (rev 136) +++ trunk/src/main/org/jboss/envers/query/impl/AbstractVersionsQuery.java 2008-09-02 11:42:56 UTC (rev 137) @@ -25,48 +25,72 @@ import org.jboss.envers.entities.EntityInstantiator; import org.jboss.envers.query.criteria.VersionsCriterion; import org.jboss.envers.query.VersionsQuery; +import org.jboss.envers.query.order.VersionsOrder; import org.jboss.envers.query.projection.VersionsProjection; -import org.jboss.envers.query.order.VersionsOrder; import org.jboss.envers.exception.VersionsException; import org.jboss.envers.configuration.VersionsConfiguration; -import org.hibernate.criterion.Projection; -import org.hibernate.criterion.Order; +import org.jboss.envers.tools.MutableInteger; +import org.jboss.envers.tools.Pair; +import org.jboss.envers.tools.Triple; import org.hibernate.*; import javax.persistence.NonUniqueResultException; import javax.persistence.NoResultException; import java.util.List; import java.util.ArrayList; +import java.util.Map; +import java.util.HashMap; /** * @author Adam Warski (adam at warski dot org) */ public abstract class AbstractVersionsQuery implements VersionsQuery { - //protected VersionsReaderImplementor versionsReader; protected EntityInstantiator entityInstantiator; protected List criterions; protected String entityName; protected String versionsEntityName; - protected Criteria versionsCriteria; + protected QueryBuilder qb; protected boolean hasProjection; protected boolean hasOrder; protected final VersionsConfiguration verCfg; + private final VersionsReaderImplementor versionsReader; protected AbstractVersionsQuery(VersionsConfiguration verCfg, VersionsReaderImplementor versionsReader, Class cls) { this.verCfg = verCfg; + this.versionsReader = versionsReader; criterions = new ArrayList(); entityInstantiator = new EntityInstantiator(verCfg, versionsReader); entityName = cls.getName(); versionsEntityName = verCfg.getVerEntCfg().getVersionsEntityName(entityName); - versionsCriteria = versionsReader.getSession().createCriteria(versionsEntityName, "e"); + + qb = new QueryBuilder(versionsEntityName, "e", new MutableInteger(), new MutableInteger()); } + protected List listQuery() { + StringBuilder querySb = new StringBuilder(); + Map paramValues = new HashMap(); + + qb.build(querySb, paramValues); + + Query query = versionsReader.getSession().createQuery(querySb.toString()); + for (Map.Entry paramValue : paramValues.entrySet()) { + query.setParameter(paramValue.getKey(), paramValue.getValue()); + } + + setQueryProperties(query); + + System.out.println("QUERY: " + querySb); + System.out.println("PARAMS: " + paramValues); + + return query.list(); + } + public abstract List list() throws VersionsException; public List getResultList() throws VersionsException { @@ -92,74 +116,98 @@ return this; } - // Various options + // Projection and order - public VersionsQuery setProjection(Projection projection) { + public VersionsQuery addProjection(String function, String propertyName) { hasProjection = true; - versionsCriteria.setProjection(projection); + qb.addProjection(function, propertyName, false); return this; } - public VersionsQuery setProjection(VersionsProjection projection) { + public VersionsQuery addProjection(VersionsProjection projection) { + Triple projectionData = projection.getProjectionData(verCfg); hasProjection = true; - versionsCriteria.setProjection(projection.getProjection(verCfg, entityName)); + qb.addProjection(projectionData.getFirst(), projectionData.getSecond(), projectionData.getThird()); return this; } - public VersionsQuery addOrder(Order order) { + public VersionsQuery addOrder(String propertyName, boolean asc) { hasOrder = true; - versionsCriteria.addOrder(order); + qb.addOrder(propertyName, asc); return this; } public VersionsQuery addOrder(VersionsOrder order) { - hasOrder = true; - versionsCriteria.addOrder(order.getOrder(verCfg, entityName)); - return this; + Pair orderData = order.getOrderData(verCfg); + return addOrder(orderData.getFirst(), orderData.getSecond()); } + // Query properties + + private Integer maxResults; + private Integer firstResult; + private Boolean cacheable; + private String cacheRegion; + private String comment; + private FlushMode flushMode; + private CacheMode cacheMode; + private Integer timeout; + private LockMode lockMode; + public VersionsQuery setMaxResults(int maxResults) { - versionsCriteria.setMaxResults(maxResults); + this.maxResults = maxResults; return this; } public VersionsQuery setFirstResult(int firstResult) { - versionsCriteria.setFirstResult(firstResult); + this.firstResult = firstResult; return this; } public VersionsQuery setCacheable(boolean cacheable) { - versionsCriteria.setCacheable(cacheable); + this.cacheable = cacheable; return this; } public VersionsQuery setCacheRegion(String cacheRegion) { - versionsCriteria.setCacheRegion(cacheRegion); + this.cacheRegion = cacheRegion; return this; } public VersionsQuery setComment(String comment) { - versionsCriteria.setComment(comment); + this.comment = comment; return this; } public VersionsQuery setFlushMode(FlushMode flushMode) { - versionsCriteria.setFlushMode(flushMode); + this.flushMode = flushMode; return this; } public VersionsQuery setCacheMode(CacheMode cacheMode) { - versionsCriteria.setCacheMode(cacheMode); + this.cacheMode = cacheMode; return this; } public VersionsQuery setTimeout(int timeout) { - versionsCriteria.setTimeout(timeout); + this.timeout = timeout; return this; } public VersionsQuery setLockMode(LockMode lockMode) { - versionsCriteria.setLockMode(lockMode); + this.lockMode = lockMode; return this; } + + protected void setQueryProperties(Query query) { + if (maxResults != null) query.setMaxResults(maxResults); + if (firstResult != null) query.setFirstResult(firstResult); + if (cacheable != null) query.setCacheable(cacheable); + if (cacheRegion != null) query.setCacheRegion(cacheRegion); + if (comment != null) query.setComment(comment); + if (flushMode != null) query.setFlushMode(flushMode); + if (cacheMode != null) query.setCacheMode(cacheMode); + if (timeout != null) query.setTimeout(timeout); + if (lockMode != null) query.setLockMode("e", lockMode); + } } Modified: trunk/src/main/org/jboss/envers/query/impl/EntitiesAtRevisionQuery.java =================================================================== --- trunk/src/main/org/jboss/envers/query/impl/EntitiesAtRevisionQuery.java 2008-08-31 08:46:36 UTC (rev 136) +++ trunk/src/main/org/jboss/envers/query/impl/EntitiesAtRevisionQuery.java 2008-09-02 11:42:56 UTC (rev 137) @@ -26,7 +26,6 @@ import org.jboss.envers.RevisionType; import org.jboss.envers.configuration.VersionsEntitiesConfiguration; import org.jboss.envers.configuration.VersionsConfiguration; -import org.hibernate.criterion.*; import java.util.List; import java.util.ArrayList; @@ -55,7 +54,7 @@ e2.revision <= :revision AND e2.originalId.id = e.originalId.id) */ - DetachedCriteria maxRevCriteria = DetachedCriteria.forEntityName(versionsEntityName, "e2"); + QueryBuilder maxRevQb = qb.newSubQueryBuilder(versionsEntityName, "e2"); VersionsEntitiesConfiguration verEntCfg = verCfg.getVerEntCfg(); @@ -63,23 +62,23 @@ String originalIdPropertyName = verEntCfg.getOriginalIdPropName(); // SELECT max(e2.revision) - maxRevCriteria.setProjection(Property.forName(revisionPropertyPath).max()); + maxRevQb.addProjection("max", revisionPropertyPath); // e2.revision <= :revision - maxRevCriteria.add(Restrictions.le(revisionPropertyPath, revision)); + maxRevQb.getRootParameters().addWhereWithParam(revisionPropertyPath, "<=", revision); // e2.id = e.id - maxRevCriteria.add(verCfg.getEntCfg().get(entityName).getIdMapper().getIdsEqualCriterion( - "e." + originalIdPropertyName, "e2." + originalIdPropertyName)); + verCfg.getEntCfg().get(entityName).getIdMapper().addIdsEqualToQuery(maxRevQb.getRootParameters(), + "e." + originalIdPropertyName, "e2." + originalIdPropertyName); // e.revision_type != DEL AND - versionsCriteria.add(Property.forName(verEntCfg.getRevisionTypePropName()).ne(RevisionType.DEL)); + qb.getRootParameters().addWhereWithParam(verEntCfg.getRevisionTypePropName(), "<>", RevisionType.DEL); // e.revision = (SELECT max(...) ...) - versionsCriteria.add(Property.forName(revisionPropertyPath).eq(maxRevCriteria)); - // all specified conditions, transformed + qb.getRootParameters().addWhere(revisionPropertyPath, "=", maxRevQb); + // all specified conditions for (VersionsCriterion criterion : criterions) { - versionsCriteria.add(criterion.toVersionsCriterion(verCfg, entityName)); + criterion.addToQuery(verCfg, entityName, qb, qb.getRootParameters()); } - List queryResult = versionsCriteria.list(); + List queryResult = listQuery(); if (hasProjection) { return queryResult; Added: trunk/src/main/org/jboss/envers/query/impl/Parameters.java =================================================================== --- trunk/src/main/org/jboss/envers/query/impl/Parameters.java (rev 0) +++ trunk/src/main/org/jboss/envers/query/impl/Parameters.java 2008-09-02 11:42:56 UTC (rev 137) @@ -0,0 +1,175 @@ +package org.jboss.envers.query.impl; + +import org.jboss.envers.tools.MutableInteger; +import org.jboss.envers.tools.MutableBoolean; + +import java.util.List; +import java.util.ArrayList; +import java.util.Map; +import java.util.HashMap; + +/** + * Parameters of a query. + * @author Adam Warski (adam at warski dot org) + */ +public class Parameters { + /** + * Main alias of the entity. + */ + private final String alias; + /** + * Connective between these parameters - "and" or "or". + */ + private final String connective; + /** + * For use by the parameter generator. + */ + private final MutableInteger queryParamCounter; + + private final List subParameters; + private final List negatedParameters; + private final List expressions; + /** + * Values of parameters used in expressions. + */ + private final Map queryParamValues; + + Parameters(String alias, String connective, MutableInteger queryParamCounter) { + this.alias = alias; + this.connective = connective; + this.queryParamCounter = queryParamCounter; + + subParameters = new ArrayList(); + negatedParameters = new ArrayList(); + expressions = new ArrayList(); + queryParamValues = new HashMap(); + } + + private String generateQueryParam() { + return "_p" + queryParamCounter.getAndIncrease(); + } + + public Parameters addSubParameters(String newConnective) { + if (connective.equals(newConnective)) { + return this; + } else { + Parameters newParams = new Parameters(alias, newConnective, queryParamCounter); + subParameters.add(newParams); + return newParams; + } + } + + public Parameters addNegatedParameters() { + Parameters newParams = new Parameters(alias, "and", queryParamCounter); + negatedParameters.add(newParams); + return newParams; + } + + public void addWhere(String left, String op, String right) { + addWhere(left, true, op, right, true); + } + + public void addWhere(String left, boolean addAliasLeft, String op, String right, boolean addAliasRight) { + StringBuilder expression = new StringBuilder(); + + if (addAliasLeft) { expression.append(alias).append("."); } + expression.append(left); + + expression.append(" ").append(op).append(" "); + + if (addAliasRight) { expression.append(alias).append("."); } + expression.append(right); + + expressions.add(expression.toString()); + } + + public void addWhereWithParam(String left, String op, Object paramValue) { + addWhereWithParam(left, true, op, paramValue); + } + + public void addWhereWithParam(String left, boolean addAlias, String op, Object paramValue) { + StringBuilder expression = new StringBuilder(); + + if (addAlias) { expression.append(alias).append("."); } + expression.append(left); + + expression.append(" ").append(op).append(" "); + + String paramName = generateQueryParam(); + queryParamValues.put(paramName, paramValue); + expression.append(":").append(paramName); + + expressions.add(expression.toString()); + } + + public void addWhereWithParams(String left, String opStart, Object[] paramValues, String opEnd) { + StringBuilder expression = new StringBuilder(); + + expression.append(alias).append(".").append(left).append(" ").append(opStart); + + for (int i=0; i paramValues) { + MutableBoolean isFirst = new MutableBoolean(true); + + for (String expression : expressions) { + append(sb, expression, isFirst); + } + + for (Parameters sub : subParameters) { + append(sb, "(", isFirst); + sub.build(sb, paramValues); + sb.append(")"); + } + + for (Parameters negated : negatedParameters) { + append(sb, "not (", isFirst); + negated.build(sb, paramValues); + sb.append(")"); + } + + paramValues.putAll(queryParamValues); + } +} Added: trunk/src/main/org/jboss/envers/query/impl/QueryBuilder.java =================================================================== --- trunk/src/main/org/jboss/envers/query/impl/QueryBuilder.java (rev 0) +++ trunk/src/main/org/jboss/envers/query/impl/QueryBuilder.java 2008-09-02 11:42:56 UTC (rev 137) @@ -0,0 +1,138 @@ +package org.jboss.envers.query.impl; + +import org.jboss.envers.tools.MutableInteger; +import org.jboss.envers.tools.Pair; +import org.jboss.envers.tools.StringTools; + +import java.util.Map; +import java.util.List; +import java.util.ArrayList; + +/** + * @author Adam Warski (adam at warski dot org) + */ +public class QueryBuilder { + private final String entityName; + private final String alias; + + /** + * For use by alias generator (in case an alias is not provided by the user). + */ + private final MutableInteger aliasCounter; + private final MutableInteger paramCounter; + private final Parameters parameters; + + private final List> froms; + private final List> orders; + private final List projections; + + public QueryBuilder(String entityName, String alias, MutableInteger aliasCounter, MutableInteger paramCounter) { + this.entityName = entityName; + this.alias = alias; + this.aliasCounter = aliasCounter; + this.paramCounter = paramCounter; + + parameters = new Parameters(alias, "and", paramCounter); + + froms = new ArrayList>(); + orders = new ArrayList>(); + projections = new ArrayList(); + + addFrom(entityName, alias); + } + + public void addFrom(String entityName, String alias) { + froms.add(Pair.make(entityName, alias)); + } + + private String generateAlias() { + return "_e" + aliasCounter.getAndIncrease(); + } + + /** + * @return A sub-query builder for the same entity (with an auto-generated alias). + */ + public QueryBuilder newSubQueryBuilder() { + return newSubQueryBuilder(entityName, generateAlias()); + } + + /** + * @param entityName Entity name, which will be the main entity for the sub-query. + * @param alias Alias of the entity, which can later be used in parameters. + * @return A sub-query builder for the given entity, with the given alias. + */ + public QueryBuilder newSubQueryBuilder(String entityName, String alias) { + return new QueryBuilder(entityName, alias, aliasCounter, paramCounter); + } + + public Parameters getRootParameters() { + return parameters; + } + + public void addOrder(String propertyName, boolean ascending) { + orders.add(Pair.make(propertyName, ascending)); + } + + public void addProjection(String function, String propertyName) { + addProjection(function, propertyName, false); + } + + public void addProjection(String function, String propertyName, boolean distinct) { + if (function == null) { + projections.add((distinct ? "distinct " : "") + alias + "." + propertyName); + } else { + projections.add(function + "(" + (distinct ? "distinct " : "") + alias + "." + propertyName + ")"); + } + } + + public void build(StringBuilder sb, Map paramValues) { + sb.append("select "); + if (projections.size() > 0) { + // all projections separated with commas + StringTools.append(sb, projections.iterator(), ", "); + } else { + // all aliases separated with commas + StringTools.append(sb, getAliasList().iterator(), ", "); + } + sb.append(" from "); + // all from entities with aliases, separated with commas + StringTools.append(sb, getFromList().iterator(), ", "); + // where part - parameters + if (!parameters.isEmpty()) { + sb.append(" where "); + parameters.build(sb, paramValues); + } + // orders + if (orders.size() > 0) { + sb.append(" order by "); + StringTools.append(sb, getOrderList().iterator(), ", "); + } + } + + private List getAliasList() { + List aliasList = new ArrayList(); + for (Pair from : froms) { + aliasList.add(from.getSecond()); + } + + return aliasList; + } + + private List getFromList() { + List fromList = new ArrayList(); + for (Pair from : froms) { + fromList.add(from.getFirst() + " " + from.getSecond()); + } + + return fromList; + } + + private List getOrderList() { + List orderList = new ArrayList(); + for (Pair order : orders) { + orderList.add(order.getFirst() + " " + (order.getSecond() ? "asc" : "desc")); + } + + return orderList; + } +} Modified: trunk/src/main/org/jboss/envers/query/impl/RevisionsOfEntityQuery.java =================================================================== --- trunk/src/main/org/jboss/envers/query/impl/RevisionsOfEntityQuery.java 2008-08-31 08:46:36 UTC (rev 136) +++ trunk/src/main/org/jboss/envers/query/impl/RevisionsOfEntityQuery.java 2008-09-02 11:42:56 UTC (rev 137) @@ -27,8 +27,6 @@ import org.jboss.envers.RevisionType; import org.jboss.envers.configuration.VersionsEntitiesConfiguration; import org.jboss.envers.configuration.VersionsConfiguration; -import org.hibernate.criterion.Order; -import org.hibernate.criterion.Property; import org.hibernate.proxy.HibernateProxy; import java.util.List; @@ -81,26 +79,26 @@ */ if (!selectDeletedEntities) { // e.revision_type != DEL AND - versionsCriteria.add(Property.forName(verEntCfg.getRevisionTypePropName()).ne(RevisionType.DEL)); + qb.getRootParameters().addWhereWithParam(verEntCfg.getRevisionTypePropName(), "<>", RevisionType.DEL); } // all specified conditions, transformed for (VersionsCriterion criterion : criterions) { - versionsCriteria.add(criterion.toVersionsCriterion(verCfg, entityName)); + criterion.addToQuery(verCfg, entityName, qb, qb.getRootParameters()); } if (!hasProjection && !hasOrder) { String revisionPropertyPath = verEntCfg.getRevisionPropPath(); - versionsCriteria.addOrder(Order.asc(revisionPropertyPath)); + qb.addOrder(revisionPropertyPath, true); } // TODO if (!selectEntitiesOnly) { //versionsCriteria.setFetchMode("e._revision", FetchMode.JOIN); - //versionsCriteria.createCriteria("e.originalId._revision").add(Property.forName("revision_id").eq(1)); + //versionsCriteria.createCriteria("e.originalId._revision").add(Property.forName("id").eq(1)); } - List queryResult = versionsCriteria.list(); + List queryResult = listQuery(); if (hasProjection) { return queryResult; } else { Modified: trunk/src/main/org/jboss/envers/query/order/RevisionVersionsOrder.java =================================================================== --- trunk/src/main/org/jboss/envers/query/order/RevisionVersionsOrder.java 2008-08-31 08:46:36 UTC (rev 136) +++ trunk/src/main/org/jboss/envers/query/order/RevisionVersionsOrder.java 2008-09-02 11:42:56 UTC (rev 137) @@ -21,8 +21,8 @@ */ package org.jboss.envers.query.order; -import org.hibernate.criterion.Order; import org.jboss.envers.configuration.VersionsConfiguration; +import org.jboss.envers.tools.Pair; /** * @author Adam Warski (adam at warski dot org) @@ -34,13 +34,8 @@ this.asc = asc; } - public Order getOrder(VersionsConfiguration verCfg, String entityName) { + public Pair getOrderData(VersionsConfiguration verCfg) { String revisionPropPath = verCfg.getVerEntCfg().getRevisionPropPath(); - - if (asc) { - return Order.asc(revisionPropPath); - } else { - return Order.desc(revisionPropPath); - } + return Pair.make(revisionPropPath, asc); } } Modified: trunk/src/main/org/jboss/envers/query/order/VersionsOrder.java =================================================================== --- trunk/src/main/org/jboss/envers/query/order/VersionsOrder.java 2008-08-31 08:46:36 UTC (rev 136) +++ trunk/src/main/org/jboss/envers/query/order/VersionsOrder.java 2008-09-02 11:42:56 UTC (rev 137) @@ -21,13 +21,12 @@ */ package org.jboss.envers.query.order; -import org.hibernate.criterion.Order; -import org.jboss.envers.reader.VersionsReaderImplementor; import org.jboss.envers.configuration.VersionsConfiguration; +import org.jboss.envers.tools.Pair; /** * @author Adam Warski (adam at warski dot org) */ public interface VersionsOrder { - public Order getOrder(VersionsConfiguration verCfg, String entityName); + Pair getOrderData(VersionsConfiguration verCfg); } Deleted: trunk/src/main/org/jboss/envers/query/projection/DistinctVersionsProjection.java =================================================================== --- trunk/src/main/org/jboss/envers/query/projection/DistinctVersionsProjection.java 2008-08-31 08:46:36 UTC (rev 136) +++ trunk/src/main/org/jboss/envers/query/projection/DistinctVersionsProjection.java 2008-09-02 11:42:56 UTC (rev 137) @@ -1,41 +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.query.projection; - -import org.hibernate.criterion.Projection; -import org.hibernate.criterion.Projections; -import org.jboss.envers.configuration.VersionsConfiguration; - -/** - * @author Adam Warski (adam at warski dot org) - */ -public class DistinctVersionsProjection implements VersionsProjection { - private final VersionsProjection projection; - - public DistinctVersionsProjection(VersionsProjection projection) { - this.projection = projection; - } - - public Projection getProjection(VersionsConfiguration verCfg, String entityName) { - return Projections.distinct(projection.getProjection(verCfg, entityName)); - } -} Deleted: trunk/src/main/org/jboss/envers/query/projection/ProjectionWrapper.java =================================================================== --- trunk/src/main/org/jboss/envers/query/projection/ProjectionWrapper.java 2008-08-31 08:46:36 UTC (rev 136) +++ trunk/src/main/org/jboss/envers/query/projection/ProjectionWrapper.java 2008-09-02 11:42:56 UTC (rev 137) @@ -1,41 +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.query.projection; - -import org.hibernate.criterion.Projection; -import org.jboss.envers.configuration.VersionsConfiguration; - -/** - * Wraps a hibernate projection so that it can be used as a versions projection. - * @author Adam Warski (adam at warski dot org) - */ -public class ProjectionWrapper implements VersionsProjection { - private final Projection wrapped; - - public ProjectionWrapper(Projection wrapped) { - this.wrapped = wrapped; - } - - public Projection getProjection(VersionsConfiguration verCfg, String entityName) { - return wrapped; - } -} Modified: trunk/src/main/org/jboss/envers/query/projection/RevisionVersionsProjection.java =================================================================== --- trunk/src/main/org/jboss/envers/query/projection/RevisionVersionsProjection.java 2008-08-31 08:46:36 UTC (rev 136) +++ trunk/src/main/org/jboss/envers/query/projection/RevisionVersionsProjection.java 2008-09-02 11:42:56 UTC (rev 137) @@ -21,9 +21,8 @@ */ package org.jboss.envers.query.projection; -import org.hibernate.criterion.Projection; -import org.hibernate.criterion.Projections; import org.jboss.envers.configuration.VersionsConfiguration; +import org.jboss.envers.tools.Triple; /** * @author Adam Warski (adam at warski dot org) @@ -33,7 +32,8 @@ MAX, MIN, COUNT, - COUNT_DISTINCT + COUNT_DISTINCT, + DISTINCT } private final ProjectionType type; @@ -42,14 +42,15 @@ this.type = type; } - public Projection getProjection(VersionsConfiguration verCfg, String entityName) { + public Triple getProjectionData(VersionsConfiguration verCfg) { String revisionPropPath = verCfg.getVerEntCfg().getRevisionPropPath(); switch (type) { - case MAX: return Projections.max(revisionPropPath); - case MIN: return Projections.min(revisionPropPath); - case COUNT: return Projections.count(revisionPropPath); - case COUNT_DISTINCT: return Projections.countDistinct(revisionPropPath); + case MAX: return Triple.make("max", revisionPropPath, false); + case MIN: return Triple.make("min", revisionPropPath, false); + case COUNT: return Triple.make("count", revisionPropPath, false); + case COUNT_DISTINCT: return Triple.make("count", revisionPropPath, true); + case DISTINCT: return Triple.make(null, revisionPropPath, true); } throw new IllegalArgumentException("Unknown type " + type + "."); Modified: trunk/src/main/org/jboss/envers/query/projection/VersionsProjection.java =================================================================== --- trunk/src/main/org/jboss/envers/query/projection/VersionsProjection.java 2008-08-31 08:46:36 UTC (rev 136) +++ trunk/src/main/org/jboss/envers/query/projection/VersionsProjection.java 2008-09-02 11:42:56 UTC (rev 137) @@ -22,11 +22,12 @@ package org.jboss.envers.query.projection; import org.jboss.envers.configuration.VersionsConfiguration; -import org.hibernate.criterion.Projection; +import org.jboss.envers.tools.Pair; +import org.jboss.envers.tools.Triple; /** * @author Adam Warski (adam at warski dot org) */ public interface VersionsProjection { - public Projection getProjection(VersionsConfiguration verCfg, String entityName); + Triple getProjectionData(VersionsConfiguration verCfg); } Deleted: trunk/src/main/org/jboss/envers/query/projection/VersionsProjectionList.java =================================================================== --- trunk/src/main/org/jboss/envers/query/projection/VersionsProjectionList.java 2008-08-31 08:46:36 UTC (rev 136) +++ trunk/src/main/org/jboss/envers/query/projection/VersionsProjectionList.java 2008-09-02 11:42:56 UTC (rev 137) @@ -1,61 +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.query.projection; - -import org.hibernate.criterion.Projection; -import org.hibernate.criterion.Projections; -import org.hibernate.criterion.ProjectionList; -import org.jboss.envers.configuration.VersionsConfiguration; - -import java.util.List; -import java.util.ArrayList; - -/** - * @author Adam Warski (adam at warski dot org) - */ -public class VersionsProjectionList implements VersionsProjection { - private final List projections; - - public VersionsProjectionList() { - projections = new ArrayList(); - } - - public VersionsProjectionList add(VersionsProjection projection) { - projections.add(projection); - return this; - } - - public VersionsProjectionList add(Projection projection) { - projections.add(new ProjectionWrapper(projection)); - return this; - } - - public Projection getProjection(VersionsConfiguration verCfg, String entityName) { - ProjectionList list = Projections.projectionList(); - - for (VersionsProjection projection : projections) { - list.add(projection.getProjection(verCfg, entityName)); - } - - return list; - } -} Modified: trunk/src/main/org/jboss/envers/reader/VersionsReaderImpl.java =================================================================== --- trunk/src/main/org/jboss/envers/reader/VersionsReaderImpl.java 2008-08-31 08:46:36 UTC (rev 136) +++ trunk/src/main/org/jboss/envers/reader/VersionsReaderImpl.java 2008-09-02 11:42:56 UTC (rev 137) @@ -108,7 +108,7 @@ } return createQuery().forRevisionsOfEntity(cls, false, true) - .setProjection(RevisionProperty.revisionNumber()) + .addProjection(RevisionProperty.revisionNumber()) .add(VersionsRestrictions.idEq(primaryKey)) .getResultList(); } Copied: trunk/src/main/org/jboss/envers/tools/MutableInteger.java (from rev 132, trunk/src/main/org/jboss/envers/tools/MutableBoolean.java) =================================================================== --- trunk/src/main/org/jboss/envers/tools/MutableInteger.java (rev 0) +++ trunk/src/main/org/jboss/envers/tools/MutableInteger.java 2008-09-02 11:42:56 UTC (rev 137) @@ -0,0 +1,48 @@ +/* + * 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.tools; + +/** + * @author Adam Warski (adam at warski dot org) + */ +public class MutableInteger { + private int value; + + public MutableInteger() { + } + + public MutableInteger(int value) { + this.value = value; + } + + public int get() { + return value; + } + + public void set(int value) { + this.value = value; + } + + public int getAndIncrease() { + return value++; + } +} \ No newline at end of file Property changes on: trunk/src/main/org/jboss/envers/tools/MutableInteger.java ___________________________________________________________________ Name: svn:mergeinfo + Modified: trunk/src/main/org/jboss/envers/tools/StringTools.java =================================================================== --- trunk/src/main/org/jboss/envers/tools/StringTools.java 2008-08-31 08:46:36 UTC (rev 136) +++ trunk/src/main/org/jboss/envers/tools/StringTools.java 2008-09-02 11:42:56 UTC (rev 137) @@ -21,6 +21,8 @@ */ package org.jboss.envers.tools; +import java.util.Iterator; + /** * @author Adam Warski (adam at warski dot org) */ @@ -29,6 +31,11 @@ return s == null || "".equals(s); } + /** + * @param s String, from which to get the last component. + * @return The last component of the dot-separated string s. For example, for a string + * "a.b.c", the result is "c". + */ public static String getLastComponent(String s) { if (s == null) { return null; @@ -41,4 +48,25 @@ return s.substring(lastDot + 1); } } + + /** + * To the given string builder, appends all strings in the given iterator, separating them with the given + * separator. For example, for an interator "a" "b" "c" and separator ":" the output is "a:b:c". + * @param sb String builder, to which to append. + * @param contents Strings to be appended. + * @param separator Separator between subsequent content. + */ + public static void append(StringBuilder sb, Iterator contents, String separator) { + boolean isFirst = true; + + while (contents.hasNext()) { + if (!isFirst) { + sb.append(separator); + } + + sb.append(contents.next()); + + isFirst = false; + } + } } Copied: trunk/src/main/org/jboss/envers/tools/Triple.java (from rev 132, trunk/src/main/org/jboss/envers/tools/Pair.java) =================================================================== --- trunk/src/main/org/jboss/envers/tools/Triple.java (rev 0) +++ trunk/src/main/org/jboss/envers/tools/Triple.java 2008-09-02 11:42:56 UTC (rev 137) @@ -0,0 +1,93 @@ +/* + * 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.tools; + +/** + * A triple of objects. + * @param + * @param + * @param + * @author Adam Warski (adamw at aster.pl) + */ +public class Triple { + private T1 obj1; + private T2 obj2; + private T3 obj3; + + public Triple() { + } + + public Triple(T1 obj1, T2 obj2, T3 obj3) { + this.obj1 = obj1; + this.obj2 = obj2; + this.obj3 = obj3; + } + + public T1 getFirst() { + return obj1; + } + + public T2 getSecond() { + return obj2; + } + + public T3 getThird() { + return obj3; + } + + public void setFirst(T1 obj1) { + this.obj1 = obj1; + } + + public void setSecond(T2 obj2) { + this.obj2 = obj2; + } + + public void setThird(T3 obj3) { + this.obj3 = obj3; + } + + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Triple)) return false; + + Triple triple = (Triple) o; + + if (obj1 != null ? !obj1.equals(triple.obj1) : triple.obj1 != null) return false; + if (obj2 != null ? !obj2.equals(triple.obj2) : triple.obj2 != null) return false; + if (obj3 != null ? !obj3.equals(triple.obj3) : triple.obj3 != null) return false; + + return true; + } + + public int hashCode() { + int result; + result = (obj1 != null ? obj1.hashCode() : 0); + result = 31 * result + (obj2 != null ? obj2.hashCode() : 0); + result = 31 * result + (obj3 != null ? obj3.hashCode() : 0); + return result; + } + + public static Triple make(T1 obj1, T2 obj2, T3 obj3) { + return new Triple(obj1, obj2, obj3); + } +} \ No newline at end of file Property changes on: trunk/src/main/org/jboss/envers/tools/Triple.java ___________________________________________________________________ Name: svn:mergeinfo + Modified: trunk/src/test/org/jboss/envers/test/integration/query/AggregateQuery.java =================================================================== --- trunk/src/test/org/jboss/envers/test/integration/query/AggregateQuery.java 2008-08-31 08:46:36 UTC (rev 136) +++ trunk/src/test/org/jboss/envers/test/integration/query/AggregateQuery.java 2008-09-02 11:42:56 UTC (rev 137) @@ -3,7 +3,6 @@ import org.jboss.envers.test.integration.AbstractEntityTest; import org.jboss.envers.test.entities.IntTestEntity; import org.hibernate.ejb.Ejb3Configuration; -import org.hibernate.criterion.Projections; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; @@ -60,23 +59,20 @@ public void testEntitiesAvgMaxQuery() { Object[] ver1 = (Object[]) getVersionsReader().createQuery() .forEntitiesAtRevision(IntTestEntity.class, 1) - .setProjection(Projections.projectionList() - .add(Projections.max("number")) - .add(Projections.avg("number"))) + .addProjection("max", "number") + .addProjection("avg", "number") .getSingleResult(); Object[] ver2 = (Object[]) getVersionsReader().createQuery() .forEntitiesAtRevision(IntTestEntity.class, 2) - .setProjection(Projections.projectionList() - .add(Projections.max("number")) - .add(Projections.avg("number"))) + .addProjection("max", "number") + .addProjection("avg", "number") .getSingleResult(); Object[] ver3 = (Object[]) getVersionsReader().createQuery() .forEntitiesAtRevision(IntTestEntity.class, 3) - .setProjection(Projections.projectionList() - .add(Projections.max("number")) - .add(Projections.avg("number"))) + .addProjection("max", "number") + .addProjection("avg", "number") .getSingleResult(); assert (Integer) ver1[0] == 10; Modified: trunk/src/test/org/jboss/envers/test/integration/query/DeletedEntities.java =================================================================== --- trunk/src/test/org/jboss/envers/test/integration/query/DeletedEntities.java 2008-08-31 08:46:36 UTC (rev 136) +++ trunk/src/test/org/jboss/envers/test/integration/query/DeletedEntities.java 2008-09-02 11:42:56 UTC (rev 137) @@ -5,7 +5,6 @@ import org.jboss.envers.query.VersionsRestrictions; import org.jboss.envers.RevisionType; import org.hibernate.ejb.Ejb3Configuration; -import org.hibernate.criterion.Projections; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; @@ -54,10 +53,10 @@ assert getVersionsReader().createQuery().forEntitiesAtRevision(StrIntTestEntity.class, 2) .getResultList().size() == 1; - assert (Integer) getVersionsReader().createQuery().forEntitiesAtRevision(StrIntTestEntity.class, 1) - .setProjection(Projections.count("originalId.id")).getResultList().get(0) == 2; - assert (Integer) getVersionsReader().createQuery().forEntitiesAtRevision(StrIntTestEntity.class, 2) - .setProjection(Projections.count("originalId.id")).getResultList().get(0) == 1; + assert (Long) getVersionsReader().createQuery().forEntitiesAtRevision(StrIntTestEntity.class, 1) + .addProjection("count", "originalId.id").getResultList().get(0) == 2; + assert (Long) getVersionsReader().createQuery().forEntitiesAtRevision(StrIntTestEntity.class, 2) + .addProjection("count", "originalId.id").getResultList().get(0) == 1; } @Test Modified: trunk/src/test/org/jboss/envers/test/integration/query/MaximalizePropertyQuery.java =================================================================== --- trunk/src/test/org/jboss/envers/test/integration/query/MaximalizePropertyQuery.java 2008-08-31 08:46:36 UTC (rev 136) +++ trunk/src/test/org/jboss/envers/test/integration/query/MaximalizePropertyQuery.java 2008-09-02 11:42:56 UTC (rev 137) @@ -79,7 +79,7 @@ public void testMaximizeWithIdEq() { List revs_id1 = getVersionsReader().createQuery() .forRevisionsOfEntity(StrIntTestEntity.class, false, true) - .setProjection(RevisionProperty.revisionNumber()) + .addProjection(RevisionProperty.revisionNumber()) .add(VersionsRestrictions.maximizeProperty("number") .add(VersionsRestrictions.idEq(id2))) .getResultList(); @@ -91,7 +91,7 @@ public void testMinimizeWithPropertyEq() { List result = getVersionsReader().createQuery() .forRevisionsOfEntity(StrIntTestEntity.class, false, true) - .setProjection(RevisionProperty.revisionNumber()) + .addProjection(RevisionProperty.revisionNumber()) .add(VersionsRestrictions.minimizeProperty("number") .add(VersionsRestrictions.eq("str1", "a"))) .getResultList(); Modified: trunk/src/test/org/jboss/envers/test/integration/query/OrderByLimitQuery.java =================================================================== --- trunk/src/test/org/jboss/envers/test/integration/query/OrderByLimitQuery.java 2008-08-31 08:46:36 UTC (rev 136) +++ trunk/src/test/org/jboss/envers/test/integration/query/OrderByLimitQuery.java 2008-09-02 11:42:56 UTC (rev 137) @@ -3,8 +3,6 @@ import org.jboss.envers.test.integration.AbstractEntityTest; import org.jboss.envers.test.entities.IntTestEntity; import org.hibernate.ejb.Ejb3Configuration; -import org.hibernate.criterion.Projections; -import org.hibernate.criterion.Order; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; @@ -70,21 +68,21 @@ public void testEntitiesOrderLimitByQueryRev1() { List res_0_to_1 = getVersionsReader().createQuery() .forEntitiesAtRevision(IntTestEntity.class, 1) - .addOrder(Order.desc("number")) + .addOrder("number", false) .setFirstResult(0) .setMaxResults(2) .getResultList(); List res_2_to_3 = getVersionsReader().createQuery() .forEntitiesAtRevision(IntTestEntity.class, 1) - .addOrder(Order.desc("number")) + .addOrder("number", false) .setFirstResult(2) .setMaxResults(2) .getResultList(); List res_empty = getVersionsReader().createQuery() .forEntitiesAtRevision(IntTestEntity.class, 1) - .addOrder(Order.desc("number")) + .addOrder("number", false) .setFirstResult(4) .setMaxResults(2) .getResultList(); @@ -98,21 +96,21 @@ public void testEntitiesOrderLimitByQueryRev2() { List res_0_to_1 = getVersionsReader().createQuery() .forEntitiesAtRevision(IntTestEntity.class, 2) - .addOrder(Order.desc("number")) + .addOrder("number", false) .setFirstResult(0) .setMaxResults(2) .getResultList(); List res_2_to_3 = getVersionsReader().createQuery() .forEntitiesAtRevision(IntTestEntity.class, 2) - .addOrder(Order.desc("number")) + .addOrder("number", false) .setFirstResult(2) .setMaxResults(2) .getResultList(); List res_4 = getVersionsReader().createQuery() .forEntitiesAtRevision(IntTestEntity.class, 2) - .addOrder(Order.desc("number")) + .addOrder("number", false) .setFirstResult(4) .setMaxResults(2) .getResultList(); Modified: trunk/src/test/org/jboss/envers/test/integration/query/RevisionConstraintQuery.java =================================================================== --- trunk/src/test/org/jboss/envers/test/integration/query/RevisionConstraintQuery.java 2008-08-31 08:46:36 UTC (rev 136) +++ trunk/src/test/org/jboss/envers/test/integration/query/RevisionConstraintQuery.java 2008-09-02 11:42:56 UTC (rev 137) @@ -4,7 +4,6 @@ import org.jboss.envers.test.entities.StrIntTestEntity; import org.jboss.envers.query.VersionsRestrictions; import org.jboss.envers.query.RevisionProperty; -import org.jboss.envers.query.VersionsProjections; import org.hibernate.ejb.Ejb3Configuration; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; @@ -79,7 +78,7 @@ public void testRevisionsLtQuery() { List result = getVersionsReader().createQuery() .forRevisionsOfEntity(StrIntTestEntity.class, false, true) - .setProjection(VersionsProjections.distinct(RevisionProperty.revisionNumber())) + .addProjection(RevisionProperty.distinct()) .add(RevisionProperty.lt(3)) .getResultList(); @@ -90,7 +89,7 @@ public void testRevisionsGeQuery() { List result = getVersionsReader().createQuery() .forRevisionsOfEntity(StrIntTestEntity.class, false, true) - .setProjection(VersionsProjections.distinct(RevisionProperty.revisionNumber())) + .addProjection(RevisionProperty.distinct()) .add(RevisionProperty.ge(2)) .getResultList(); @@ -101,7 +100,7 @@ public void testRevisionsLeWithPropertyQuery() { List result = getVersionsReader().createQuery() .forRevisionsOfEntity(StrIntTestEntity.class, false, true) - .setProjection(RevisionProperty.revisionNumber()) + .addProjection(RevisionProperty.revisionNumber()) .add(RevisionProperty.le(3)) .add(VersionsRestrictions.eq("str1", "a")) .getResultList(); @@ -113,7 +112,7 @@ public void testRevisionsGtWithPropertyQuery() { List result = getVersionsReader().createQuery() .forRevisionsOfEntity(StrIntTestEntity.class, false, true) - .setProjection(RevisionProperty.revisionNumber()) + .addProjection(RevisionProperty.revisionNumber()) .add(RevisionProperty.gt(1)) .add(VersionsRestrictions.lt("number", 10)) .getResultList(); @@ -125,24 +124,24 @@ public void testRevisionProjectionQuery() { Object[] result = (Object[]) getVersionsReader().createQuery() .forRevisionsOfEntity(StrIntTestEntity.class, false, true) - .setProjection( - VersionsProjections.projectionList() - .add(RevisionProperty.max()) - .add(RevisionProperty.count()) - .add(RevisionProperty.min())) + .addProjection(RevisionProperty.max()) + .addProjection(RevisionProperty.count()) + .addProjection(RevisionProperty.countDistinct()) + .addProjection(RevisionProperty.min()) .add(VersionsRestrictions.idEq(id1)) .getSingleResult(); assert (Integer) result[0] == 4; - assert (Integer) result[1] == 4; - assert (Integer) result[2] == 1; + assert (Long) result[1] == 4; + assert (Long) result[2] == 4; + assert (Integer) result[3] == 1; } @Test public void testRevisionOrderQuery() { List result = getVersionsReader().createQuery() .forRevisionsOfEntity(StrIntTestEntity.class, false, true) - .setProjection(RevisionProperty.revisionNumber()) + .addProjection(RevisionProperty.revisionNumber()) .add(VersionsRestrictions.idEq(id1)) .addOrder(RevisionProperty.desc()) .getResultList(); @@ -155,10 +154,10 @@ // The query shouldn't be ordered as always, otherwise - we get an exception. Object result = getVersionsReader().createQuery() .forRevisionsOfEntity(StrIntTestEntity.class, false, true) - .setProjection(RevisionProperty.count()) + .addProjection(RevisionProperty.count()) .add(VersionsRestrictions.idEq(id1)) .getSingleResult(); - assert (Integer) result == 4; + assert (Long) result == 4; } } \ No newline at end of file Modified: trunk/src/test/org/jboss/envers/test/integration/query/SimpleQuery.java =================================================================== --- trunk/src/test/org/jboss/envers/test/integration/query/SimpleQuery.java 2008-08-31 08:46:36 UTC (rev 136) +++ trunk/src/test/org/jboss/envers/test/integration/query/SimpleQuery.java 2008-09-02 11:42:56 UTC (rev 137) @@ -142,21 +142,21 @@ public void testRevisionsPropertyEqQuery() { List revs_id1 = getVersionsReader().createQuery() .forRevisionsOfEntity(StrIntTestEntity.class, false, true) - .setProjection(RevisionProperty.revisionNumber()) + .addProjection(RevisionProperty.revisionNumber()) .add(VersionsRestrictions.le("str1", "a")) .add(VersionsRestrictions.idEq(id1)) .getResultList(); List revs_id2 = getVersionsReader().createQuery() .forRevisionsOfEntity(StrIntTestEntity.class, false, true) - .setProjection(RevisionProperty.revisionNumber()) + .addProjection(RevisionProperty.revisionNumber()) .add(VersionsRestrictions.le("str1", "a")) .add(VersionsRestrictions.idEq(id2)) .getResultList(); List revs_id3 = getVersionsReader().createQuery() .forRevisionsOfEntity(StrIntTestEntity.class, false, true) - .setProjection(RevisionProperty.revisionNumber()) + .addProjection(RevisionProperty.revisionNumber()) .add(VersionsRestrictions.le("str1", "a")) .add(VersionsRestrictions.idEq(id3)) .getResultList(); @@ -205,7 +205,7 @@ public void testSelectRevisionTypeQuery() { List result = getVersionsReader().createQuery() .forRevisionsOfEntity(StrIntTestEntity.class, false, true) - .setProjection(RevisionTypeProperty.revisionType()) + .addProjection(RevisionTypeProperty.revisionType()) .add(VersionsRestrictions.idEq(id1)) .getResultList(); From jboss-envers-commits at lists.jboss.org Wed Sep 3 02:04:12 2008 From: jboss-envers-commits at lists.jboss.org (jboss-envers-commits at lists.jboss.org) Date: Wed, 03 Sep 2008 02:04:12 -0400 Subject: [jboss-envers-commits] JBoss Envers SVN: r138 - in trunk/src/main/org/jboss/envers/query: criteria and 3 other directories. Message-ID: Author: adamw Date: 2008-09-03 02:04:11 -0400 (Wed, 03 Sep 2008) New Revision: 138 Modified: trunk/src/main/org/jboss/envers/query/RevisionProperty.java trunk/src/main/org/jboss/envers/query/RevisionTypeProperty.java trunk/src/main/org/jboss/envers/query/criteria/AggregatedFieldVersionsExpression.java trunk/src/main/org/jboss/envers/query/criteria/VersionsConjunction.java trunk/src/main/org/jboss/envers/query/criteria/VersionsDisjunction.java trunk/src/main/org/jboss/envers/query/impl/AbstractVersionsQuery.java trunk/src/main/org/jboss/envers/query/impl/EntitiesAtRevisionQuery.java trunk/src/main/org/jboss/envers/query/impl/Parameters.java trunk/src/main/org/jboss/envers/query/impl/QueryBuilder.java trunk/src/main/org/jboss/envers/query/impl/RevisionsOfEntityQuery.java trunk/src/main/org/jboss/envers/query/order/RevisionVersionsOrder.java trunk/src/main/org/jboss/envers/query/order/VersionsOrder.java trunk/src/main/org/jboss/envers/query/projection/RevisionVersionsProjection.java trunk/src/main/org/jboss/envers/query/projection/VersionsProjection.java Log: ENVERS-47: some comments Modified: trunk/src/main/org/jboss/envers/query/RevisionProperty.java =================================================================== --- trunk/src/main/org/jboss/envers/query/RevisionProperty.java 2008-09-02 11:42:56 UTC (rev 137) +++ trunk/src/main/org/jboss/envers/query/RevisionProperty.java 2008-09-03 06:04:11 UTC (rev 138) @@ -115,13 +115,13 @@ } /** - * Select the revision number + * Projection the revision number */ public static VersionsProjection revisionNumber() { return new RevisionProperty(); } - public Triple getProjectionData(VersionsConfiguration verCfg) { + public Triple getData(VersionsConfiguration verCfg) { return Triple.make(null, verCfg.getVerEntCfg().getRevisionPropPath(), false); } } Modified: trunk/src/main/org/jboss/envers/query/RevisionTypeProperty.java =================================================================== --- trunk/src/main/org/jboss/envers/query/RevisionTypeProperty.java 2008-09-02 11:42:56 UTC (rev 137) +++ trunk/src/main/org/jboss/envers/query/RevisionTypeProperty.java 2008-09-03 06:04:11 UTC (rev 138) @@ -33,13 +33,13 @@ private RevisionTypeProperty() { } /** - * Select the revision type + * Projection on the revision type */ public static VersionsProjection revisionType() { return new RevisionTypeProperty(); } - public Triple getProjectionData(VersionsConfiguration verCfg) { + public Triple getData(VersionsConfiguration verCfg) { return Triple.make(null, verCfg.getVerEntCfg().getRevisionTypePropName(), false); } } \ No newline at end of file Modified: trunk/src/main/org/jboss/envers/query/criteria/AggregatedFieldVersionsExpression.java =================================================================== --- trunk/src/main/org/jboss/envers/query/criteria/AggregatedFieldVersionsExpression.java 2008-09-02 11:42:56 UTC (rev 137) +++ trunk/src/main/org/jboss/envers/query/criteria/AggregatedFieldVersionsExpression.java 2008-09-03 06:04:11 UTC (rev 138) @@ -68,10 +68,10 @@ // Setting the desired projection of the aggregated query switch (mode) { case MIN: - subQb.addProjection("min", propertyName); + subQb.addProjection("min", propertyName, false); break; case MAX: - subQb.addProjection("max", propertyName); + subQb.addProjection("max", propertyName, false); } // Adding the constrain on the result of the aggregated criteria Modified: trunk/src/main/org/jboss/envers/query/criteria/VersionsConjunction.java =================================================================== --- trunk/src/main/org/jboss/envers/query/criteria/VersionsConjunction.java 2008-09-02 11:42:56 UTC (rev 137) +++ trunk/src/main/org/jboss/envers/query/criteria/VersionsConjunction.java 2008-09-03 06:04:11 UTC (rev 138) @@ -44,7 +44,7 @@ } public void addToQuery(VersionsConfiguration verCfg, String entityName, QueryBuilder qb, Parameters parameters) { - Parameters andParameters = parameters.addSubParameters("and"); + Parameters andParameters = parameters.addSubParameters(Parameters.AND); for (VersionsCriterion criterion : criterions) { criterion.addToQuery(verCfg, entityName, qb, andParameters); Modified: trunk/src/main/org/jboss/envers/query/criteria/VersionsDisjunction.java =================================================================== --- trunk/src/main/org/jboss/envers/query/criteria/VersionsDisjunction.java 2008-09-02 11:42:56 UTC (rev 137) +++ trunk/src/main/org/jboss/envers/query/criteria/VersionsDisjunction.java 2008-09-03 06:04:11 UTC (rev 138) @@ -44,7 +44,7 @@ } public void addToQuery(VersionsConfiguration verCfg, String entityName, QueryBuilder qb, Parameters parameters) { - Parameters orParameters = parameters.addSubParameters("or"); + Parameters orParameters = parameters.addSubParameters(Parameters.OR); for (VersionsCriterion criterion : criterions) { criterion.addToQuery(verCfg, entityName, qb, orParameters); Modified: trunk/src/main/org/jboss/envers/query/impl/AbstractVersionsQuery.java =================================================================== --- trunk/src/main/org/jboss/envers/query/impl/AbstractVersionsQuery.java 2008-09-02 11:42:56 UTC (rev 137) +++ trunk/src/main/org/jboss/envers/query/impl/AbstractVersionsQuery.java 2008-09-03 06:04:11 UTC (rev 138) @@ -29,7 +29,6 @@ import org.jboss.envers.query.projection.VersionsProjection; import org.jboss.envers.exception.VersionsException; import org.jboss.envers.configuration.VersionsConfiguration; -import org.jboss.envers.tools.MutableInteger; import org.jboss.envers.tools.Pair; import org.jboss.envers.tools.Triple; import org.hibernate.*; @@ -69,24 +68,24 @@ entityName = cls.getName(); versionsEntityName = verCfg.getVerEntCfg().getVersionsEntityName(entityName); - qb = new QueryBuilder(versionsEntityName, "e", new MutableInteger(), new MutableInteger()); + qb = new QueryBuilder(versionsEntityName, "e"); } - protected List listQuery() { + protected List buildAndExecuteQuery() { StringBuilder querySb = new StringBuilder(); - Map paramValues = new HashMap(); + Map queryParamValues = new HashMap(); - qb.build(querySb, paramValues); + qb.build(querySb, queryParamValues); Query query = versionsReader.getSession().createQuery(querySb.toString()); - for (Map.Entry paramValue : paramValues.entrySet()) { + for (Map.Entry paramValue : queryParamValues.entrySet()) { query.setParameter(paramValue.getKey(), paramValue.getValue()); } setQueryProperties(query); System.out.println("QUERY: " + querySb); - System.out.println("PARAMS: " + paramValues); + System.out.println("PARAMS: " + queryParamValues); return query.list(); } @@ -125,7 +124,7 @@ } public VersionsQuery addProjection(VersionsProjection projection) { - Triple projectionData = projection.getProjectionData(verCfg); + Triple projectionData = projection.getData(verCfg); hasProjection = true; qb.addProjection(projectionData.getFirst(), projectionData.getSecond(), projectionData.getThird()); return this; @@ -138,7 +137,7 @@ } public VersionsQuery addOrder(VersionsOrder order) { - Pair orderData = order.getOrderData(verCfg); + Pair orderData = order.getData(verCfg); return addOrder(orderData.getFirst(), orderData.getSecond()); } Modified: trunk/src/main/org/jboss/envers/query/impl/EntitiesAtRevisionQuery.java =================================================================== --- trunk/src/main/org/jboss/envers/query/impl/EntitiesAtRevisionQuery.java 2008-09-02 11:42:56 UTC (rev 137) +++ trunk/src/main/org/jboss/envers/query/impl/EntitiesAtRevisionQuery.java 2008-09-03 06:04:11 UTC (rev 138) @@ -62,7 +62,7 @@ String originalIdPropertyName = verEntCfg.getOriginalIdPropName(); // SELECT max(e2.revision) - maxRevQb.addProjection("max", revisionPropertyPath); + maxRevQb.addProjection("max", revisionPropertyPath, false); // e2.revision <= :revision maxRevQb.getRootParameters().addWhereWithParam(revisionPropertyPath, "<=", revision); // e2.id = e.id @@ -78,7 +78,7 @@ criterion.addToQuery(verCfg, entityName, qb, qb.getRootParameters()); } - List queryResult = listQuery(); + List queryResult = buildAndExecuteQuery(); if (hasProjection) { return queryResult; Modified: trunk/src/main/org/jboss/envers/query/impl/Parameters.java =================================================================== --- trunk/src/main/org/jboss/envers/query/impl/Parameters.java 2008-09-02 11:42:56 UTC (rev 137) +++ trunk/src/main/org/jboss/envers/query/impl/Parameters.java 2008-09-03 06:04:11 UTC (rev 138) @@ -9,10 +9,13 @@ import java.util.HashMap; /** - * Parameters of a query. + * Parameters of a query, built using {@link org.jboss.envers.query.impl.QueryBuilder}. * @author Adam Warski (adam at warski dot org) */ public class Parameters { + public final static String AND = "and"; + public final static String OR = "or"; + /** * Main alias of the entity. */ @@ -22,17 +25,26 @@ */ private final String connective; /** - * For use by the parameter generator. + * For use by the parameter generator. Must be the same in all "child" (and parent) parameters. */ private final MutableInteger queryParamCounter; + /** + * A list of sub-parameters (parameters with a different connective). + */ private final List subParameters; + /** + * A list of negated parameters. + */ private final List negatedParameters; + /** + * A list of complete where-expressions. + */ private final List expressions; /** * Values of parameters used in expressions. */ - private final Map queryParamValues; + private final Map localQueryParamValues; Parameters(String alias, String connective, MutableInteger queryParamCounter) { this.alias = alias; @@ -42,13 +54,20 @@ subParameters = new ArrayList(); negatedParameters = new ArrayList(); expressions = new ArrayList(); - queryParamValues = new HashMap(); + localQueryParamValues = new HashMap(); } private String generateQueryParam() { return "_p" + queryParamCounter.getAndIncrease(); } + /** + * Adds sub-parameters with a new connective. That is, the parameters will be grouped in parentheses in the + * generated query, e.g.: ... and (exp1 or exp2) and ..., assuming the old connective is "and", and the + * new connective is "or". + * @param newConnective New connective of the parameters. + * @return Sub-parameters with the given connective. + */ public Parameters addSubParameters(String newConnective) { if (connective.equals(newConnective)) { return this; @@ -59,8 +78,13 @@ } } + /** + * Adds negated parameters, by default with the "and" connective. These paremeters will be grouped in parentheses + * in the generated query and negated, e.g. ... not (exp1 and exp2) ... + * @return Negated sub paremters. + */ public Parameters addNegatedParameters() { - Parameters newParams = new Parameters(alias, "and", queryParamCounter); + Parameters newParams = new Parameters(alias, AND, queryParamCounter); negatedParameters.add(newParams); return newParams; } @@ -96,7 +120,7 @@ expression.append(" ").append(op).append(" "); String paramName = generateQueryParam(); - queryParamValues.put(paramName, paramValue); + localQueryParamValues.put(paramName, paramValue); expression.append(":").append(paramName); expressions.add(expression.toString()); @@ -110,7 +134,7 @@ for (int i=0; i paramValues) { + void build(StringBuilder sb, Map queryParamValues) { MutableBoolean isFirst = new MutableBoolean(true); for (String expression : expressions) { @@ -159,17 +183,22 @@ } for (Parameters sub : subParameters) { - append(sb, "(", isFirst); - sub.build(sb, paramValues); - sb.append(")"); + if (!subParameters.isEmpty()) { + append(sb, "(", isFirst); + sub.build(sb, queryParamValues); + sb.append(")"); + } } for (Parameters negated : negatedParameters) { - append(sb, "not (", isFirst); - negated.build(sb, paramValues); - sb.append(")"); + if (!negatedParameters.isEmpty()) { + append(sb, "not (", isFirst); + negated.build(sb, queryParamValues); + sb.append(")"); + } } - paramValues.putAll(queryParamValues); + queryParamValues.putAll(localQueryParamValues); } } + Modified: trunk/src/main/org/jboss/envers/query/impl/QueryBuilder.java =================================================================== --- trunk/src/main/org/jboss/envers/query/impl/QueryBuilder.java 2008-09-02 11:42:56 UTC (rev 137) +++ trunk/src/main/org/jboss/envers/query/impl/QueryBuilder.java 2008-09-03 06:04:11 UTC (rev 138) @@ -9,6 +9,7 @@ import java.util.ArrayList; /** + * A class for incrementaly building a HQL query. * @author Adam Warski (adam at warski dot org) */ public class QueryBuilder { @@ -19,20 +20,46 @@ * For use by alias generator (in case an alias is not provided by the user). */ private final MutableInteger aliasCounter; + /** + * For use by parameter generator, in {@link org.jboss.envers.query.impl.Parameters}. This counter must be + * the same in all parameters and sub-queries of this query. + */ private final MutableInteger paramCounter; - private final Parameters parameters; + /** + * Main "where" parameters for this query. + */ + private final Parameters rootParameters; + /** + * A list of pairs (from entity name, alias name). + */ private final List> froms; + /** + * A list of pairs (property name, order ascending?). + */ private final List> orders; + /** + * A list of complete projection definitions: either a sole property name, or a function(property name). + */ private final List projections; - public QueryBuilder(String entityName, String alias, MutableInteger aliasCounter, MutableInteger paramCounter) { + /** + * + * @param entityName Main entity which should be selected. Its properties don't have to be referred to + * using the alias. + * @param alias Alias of the entity + */ + public QueryBuilder(String entityName, String alias) { + this(entityName, alias, new MutableInteger(), new MutableInteger()); + } + + private QueryBuilder(String entityName, String alias, MutableInteger aliasCounter, MutableInteger paramCounter) { this.entityName = entityName; this.alias = alias; this.aliasCounter = aliasCounter; this.paramCounter = paramCounter; - parameters = new Parameters(alias, "and", paramCounter); + rootParameters = new Parameters(alias, "and", paramCounter); froms = new ArrayList>(); orders = new ArrayList>(); @@ -41,6 +68,12 @@ addFrom(entityName, alias); } + /** + * Add an entity from which to select. + * @param entityName Name of the entity from which to select. Its properties always have to be referred to + * using the alias. + * @param alias Alias of the entity. Should be different than all other aliases. + */ public void addFrom(String entityName, String alias) { froms.add(Pair.make(entityName, alias)); } @@ -50,7 +83,8 @@ } /** - * @return A sub-query builder for the same entity (with an auto-generated alias). + * @return A sub-query builder for the same entity (with an auto-generated alias). The sub-query can + * be later used as a value of a parameter. */ public QueryBuilder newSubQueryBuilder() { return newSubQueryBuilder(entityName, generateAlias()); @@ -59,24 +93,21 @@ /** * @param entityName Entity name, which will be the main entity for the sub-query. * @param alias Alias of the entity, which can later be used in parameters. - * @return A sub-query builder for the given entity, with the given alias. + * @return A sub-query builder for the given entity, with the given alias. The sub-query can + * be later used as a value of a parameter. */ public QueryBuilder newSubQueryBuilder(String entityName, String alias) { return new QueryBuilder(entityName, alias, aliasCounter, paramCounter); } public Parameters getRootParameters() { - return parameters; + return rootParameters; } public void addOrder(String propertyName, boolean ascending) { orders.add(Pair.make(propertyName, ascending)); } - public void addProjection(String function, String propertyName) { - addProjection(function, propertyName, false); - } - public void addProjection(String function, String propertyName, boolean distinct) { if (function == null) { projections.add((distinct ? "distinct " : "") + alias + "." + propertyName); @@ -85,7 +116,13 @@ } } - public void build(StringBuilder sb, Map paramValues) { + /** + * Builds the given query, appending results to the given string buffer, and adding all query parameter values + * that are used to the map provided. + * @param sb String builder to which the query will be appended. + * @param queryParamValues Values of parameters used in the query. + */ + public void build(StringBuilder sb, Map queryParamValues) { sb.append("select "); if (projections.size() > 0) { // all projections separated with commas @@ -97,10 +134,10 @@ sb.append(" from "); // all from entities with aliases, separated with commas StringTools.append(sb, getFromList().iterator(), ", "); - // where part - parameters - if (!parameters.isEmpty()) { + // where part - rootParameters + if (!rootParameters.isEmpty()) { sb.append(" where "); - parameters.build(sb, paramValues); + rootParameters.build(sb, queryParamValues); } // orders if (orders.size() > 0) { Modified: trunk/src/main/org/jboss/envers/query/impl/RevisionsOfEntityQuery.java =================================================================== --- trunk/src/main/org/jboss/envers/query/impl/RevisionsOfEntityQuery.java 2008-09-02 11:42:56 UTC (rev 137) +++ trunk/src/main/org/jboss/envers/query/impl/RevisionsOfEntityQuery.java 2008-09-03 06:04:11 UTC (rev 138) @@ -98,7 +98,7 @@ //versionsCriteria.createCriteria("e.originalId._revision").add(Property.forName("id").eq(1)); } - List queryResult = listQuery(); + List queryResult = buildAndExecuteQuery(); if (hasProjection) { return queryResult; } else { Modified: trunk/src/main/org/jboss/envers/query/order/RevisionVersionsOrder.java =================================================================== --- trunk/src/main/org/jboss/envers/query/order/RevisionVersionsOrder.java 2008-09-02 11:42:56 UTC (rev 137) +++ trunk/src/main/org/jboss/envers/query/order/RevisionVersionsOrder.java 2008-09-03 06:04:11 UTC (rev 138) @@ -34,7 +34,7 @@ this.asc = asc; } - public Pair getOrderData(VersionsConfiguration verCfg) { + public Pair getData(VersionsConfiguration verCfg) { String revisionPropPath = verCfg.getVerEntCfg().getRevisionPropPath(); return Pair.make(revisionPropPath, asc); } Modified: trunk/src/main/org/jboss/envers/query/order/VersionsOrder.java =================================================================== --- trunk/src/main/org/jboss/envers/query/order/VersionsOrder.java 2008-09-02 11:42:56 UTC (rev 137) +++ trunk/src/main/org/jboss/envers/query/order/VersionsOrder.java 2008-09-03 06:04:11 UTC (rev 138) @@ -28,5 +28,9 @@ * @author Adam Warski (adam at warski dot org) */ public interface VersionsOrder { - Pair getOrderData(VersionsConfiguration verCfg); + /** + * @param verCfg Configuration. + * @return A pair: (property name, ascending?). + */ + Pair getData(VersionsConfiguration verCfg); } Modified: trunk/src/main/org/jboss/envers/query/projection/RevisionVersionsProjection.java =================================================================== --- trunk/src/main/org/jboss/envers/query/projection/RevisionVersionsProjection.java 2008-09-02 11:42:56 UTC (rev 137) +++ trunk/src/main/org/jboss/envers/query/projection/RevisionVersionsProjection.java 2008-09-03 06:04:11 UTC (rev 138) @@ -42,7 +42,7 @@ this.type = type; } - public Triple getProjectionData(VersionsConfiguration verCfg) { + public Triple getData(VersionsConfiguration verCfg) { String revisionPropPath = verCfg.getVerEntCfg().getRevisionPropPath(); switch (type) { Modified: trunk/src/main/org/jboss/envers/query/projection/VersionsProjection.java =================================================================== --- trunk/src/main/org/jboss/envers/query/projection/VersionsProjection.java 2008-09-02 11:42:56 UTC (rev 137) +++ trunk/src/main/org/jboss/envers/query/projection/VersionsProjection.java 2008-09-03 06:04:11 UTC (rev 138) @@ -29,5 +29,10 @@ * @author Adam Warski (adam at warski dot org) */ public interface VersionsProjection { - Triple getProjectionData(VersionsConfiguration verCfg); + /** + * + * @param verCfg Configuration. + * @return A triple: (function name - possibly null, property name, add distinct?). + */ + Triple getData(VersionsConfiguration verCfg); } From jboss-envers-commits at lists.jboss.org Wed Sep 3 02:27:28 2008 From: jboss-envers-commits at lists.jboss.org (jboss-envers-commits at lists.jboss.org) Date: Wed, 03 Sep 2008 02:27:28 -0400 Subject: [jboss-envers-commits] JBoss Envers SVN: r139 - trunk/src/test/org/jboss/envers/test. Message-ID: Author: adamw Date: 2008-09-03 02:27:28 -0400 (Wed, 03 Sep 2008) New Revision: 139 Modified: trunk/src/test/org/jboss/envers/test/ Log: Removing the working-test directory from svn Property changes on: trunk/src/test/org/jboss/envers/test ___________________________________________________________________ Name: svn:ignore + working From jboss-envers-commits at lists.jboss.org Wed Sep 3 02:27:36 2008 From: jboss-envers-commits at lists.jboss.org (jboss-envers-commits at lists.jboss.org) Date: Wed, 03 Sep 2008 02:27:36 -0400 Subject: [jboss-envers-commits] JBoss Envers SVN: r140 - trunk/src/test/org/jboss/envers/test. Message-ID: Author: adamw Date: 2008-09-03 02:27:36 -0400 (Wed, 03 Sep 2008) New Revision: 140 Removed: trunk/src/test/org/jboss/envers/test/working/ Log: Removing the working-test directory from svn From jboss-envers-commits at lists.jboss.org Wed Sep 3 03:29:49 2008 From: jboss-envers-commits at lists.jboss.org (jboss-envers-commits at lists.jboss.org) Date: Wed, 03 Sep 2008 03:29:49 -0400 Subject: [jboss-envers-commits] JBoss Envers SVN: r141 - in trunk/src: main/org/jboss/envers/configuration and 3 other directories. Message-ID: Author: adamw Date: 2008-09-03 03:29:48 -0400 (Wed, 03 Sep 2008) New Revision: 141 Modified: trunk/src/main/org/jboss/envers/DefaultRevisionEntity.java trunk/src/main/org/jboss/envers/configuration/RevisionInfoConfiguration.java trunk/src/main/org/jboss/envers/configuration/VersionsConfiguration.java trunk/src/main/org/jboss/envers/configuration/VersionsEntitiesConfiguration.java trunk/src/main/org/jboss/envers/query/VersionsQueryCreator.java trunk/src/main/org/jboss/envers/query/impl/AbstractVersionsQuery.java trunk/src/main/org/jboss/envers/query/impl/QueryBuilder.java trunk/src/main/org/jboss/envers/query/impl/RevisionsOfEntityQuery.java trunk/src/test/org/jboss/envers/test/integration/query/DeletedEntities.java trunk/src/test/org/jboss/envers/test/integration/query/SimpleQuery.java Log: ENVERS-47: selecting all revision information in revisions-of-entity query Modified: trunk/src/main/org/jboss/envers/DefaultRevisionEntity.java =================================================================== --- trunk/src/main/org/jboss/envers/DefaultRevisionEntity.java 2008-09-03 06:27:36 UTC (rev 140) +++ trunk/src/main/org/jboss/envers/DefaultRevisionEntity.java 2008-09-03 07:29:48 UTC (rev 141) @@ -24,6 +24,9 @@ import javax.persistence.MappedSuperclass; import javax.persistence.Id; import javax.persistence.GeneratedValue; +import javax.persistence.Transient; +import java.util.Date; +import java.text.DateFormat; /** * @author Adam Warski (adam at warski dot org) @@ -46,6 +49,11 @@ this.id = id; } + @Transient + public Date getRevisionDate() { + return new Date(timestamp); + } + public long getTimestamp() { return timestamp; } @@ -72,4 +80,8 @@ result = 31 * result + (int) (timestamp ^ (timestamp >>> 32)); return result; } + + public String toString() { + return "DefaultRevisionEntity(id = " + id + ", revisionDate = " + DateFormat.getDateTimeInstance().format(getRevisionDate()) + ")"; + } } Modified: trunk/src/main/org/jboss/envers/configuration/RevisionInfoConfiguration.java =================================================================== --- trunk/src/main/org/jboss/envers/configuration/RevisionInfoConfiguration.java 2008-09-03 06:27:36 UTC (rev 140) +++ trunk/src/main/org/jboss/envers/configuration/RevisionInfoConfiguration.java 2008-09-03 07:29:48 UTC (rev 141) @@ -164,7 +164,7 @@ revisionInfoEntityName = pc.getEntityName(); revisionInfoClass = pc.getMappedClass(); - revisionInfoGenerator = new DefaultRevisionInfoGenerator(revisionInfoEntityName, pc.getMappedClass(), + revisionInfoGenerator = new DefaultRevisionInfoGenerator(revisionInfoEntityName, revisionInfoClass, revisionEntity.value(), revisionInfoTimestampName); } } @@ -173,10 +173,8 @@ Document revisionInfoXmlMapping = null; if (revisionInfoGenerator == null) { - // TODO - remove default - //revisionInfoGenerator = new DefaultRevisionInfoGenerator(revisionInfoEntityName, revisionInfoTimestampName); revisionInfoClass = DefaultRevisionEntity.class; - revisionInfoGenerator = new DefaultRevisionInfoGenerator(revisionInfoEntityName, DefaultRevisionEntity.class, + revisionInfoGenerator = new DefaultRevisionInfoGenerator(revisionInfoEntityName, revisionInfoClass, RevisionListener.class, revisionInfoTimestampName); revisionInfoXmlMapping = generateDefaultRevisionInfoXmlMapping(); } @@ -184,8 +182,8 @@ return new RevisionInfoConfigurationResult( revisionInfoGenerator, revisionPropType, revisionInfoXmlMapping, new RevisionInfoQueryCreator(revisionInfoEntityName, revisionInfoIdName, revisionInfoTimestampName), - generateRevisionInfoRelationMapping(), revisionInfoIdName, - new RevisionInfoNumberReader(revisionInfoClass, revisionInfoIdName)); + generateRevisionInfoRelationMapping(), + new RevisionInfoNumberReader(revisionInfoClass, revisionInfoIdName), revisionInfoEntityName); } } @@ -195,21 +193,20 @@ private final Document revisionInfoXmlMapping; private final RevisionInfoQueryCreator revisionInfoQueryCreator; private final Element revisionInfoRelationMapping; - // TODO - remove ? - private final String revisionInfoIdName; private final RevisionInfoNumberReader revisionInfoNumberReader; + private final String revisionInfoEntityName; RevisionInfoConfigurationResult(RevisionInfoGenerator revisionInfoGenerator, String revisionPropType, Document revisionInfoXmlMapping, RevisionInfoQueryCreator revisionInfoQueryCreator, - Element revisionInfoRelationMapping, String revisionInfoIdName, - RevisionInfoNumberReader revisionInfoNumberReader) { + Element revisionInfoRelationMapping, + RevisionInfoNumberReader revisionInfoNumberReader, String revisionInfoEntityName) { this.revisionInfoGenerator = revisionInfoGenerator; this.revisionPropType = revisionPropType; this.revisionInfoXmlMapping = revisionInfoXmlMapping; this.revisionInfoQueryCreator = revisionInfoQueryCreator; this.revisionInfoRelationMapping = revisionInfoRelationMapping; - this.revisionInfoIdName = revisionInfoIdName; this.revisionInfoNumberReader = revisionInfoNumberReader; + this.revisionInfoEntityName = revisionInfoEntityName; } public RevisionInfoGenerator getRevisionInfoGenerator() { @@ -232,11 +229,11 @@ return revisionInfoRelationMapping; } - public String getRevisionInfoIdName() { - return revisionInfoIdName; - } - public RevisionInfoNumberReader getRevisionInfoNumberReader() { return revisionInfoNumberReader; } + + public String getRevisionInfoEntityName() { + return revisionInfoEntityName; + } } \ No newline at end of file Modified: trunk/src/main/org/jboss/envers/configuration/VersionsConfiguration.java =================================================================== --- trunk/src/main/org/jboss/envers/configuration/VersionsConfiguration.java 2008-09-03 06:27:36 UTC (rev 140) +++ trunk/src/main/org/jboss/envers/configuration/VersionsConfiguration.java 2008-09-03 07:29:48 UTC (rev 141) @@ -73,7 +73,7 @@ RevisionInfoConfiguration revInfoCfg = new RevisionInfoConfiguration(); RevisionInfoConfigurationResult revInfoCfgResult = revInfoCfg.configure(cfg, reflectionManager); verEntCfg = new VersionsEntitiesConfiguration(properties, revInfoCfgResult.getRevisionPropType(), - revInfoCfgResult.getRevisionInfoIdName()); + revInfoCfgResult.getRevisionInfoEntityName()); globalCfg = new GlobalConfiguration(properties); versionsSyncManager = new VersionsSyncManager(revInfoCfgResult.getRevisionInfoGenerator()); revisionInfoQueryCreator = revInfoCfgResult.getRevisionInfoQueryCreator(); Modified: trunk/src/main/org/jboss/envers/configuration/VersionsEntitiesConfiguration.java =================================================================== --- trunk/src/main/org/jboss/envers/configuration/VersionsEntitiesConfiguration.java 2008-09-03 06:27:36 UTC (rev 140) +++ trunk/src/main/org/jboss/envers/configuration/VersionsEntitiesConfiguration.java 2008-09-03 07:29:48 UTC (rev 141) @@ -42,12 +42,12 @@ private final String revisionTypePropName; private final String revisionTypePropType; - private final String revisionInfoIdName; + private final String revisionInfoEntityName; private final Map customVersionsTablesNames; - public VersionsEntitiesConfiguration(Properties properties, String revisionPropType, String revisionInfoIdName) { - this.revisionInfoIdName = revisionInfoIdName; + public VersionsEntitiesConfiguration(Properties properties, String revisionPropType, String revisionInfoEntityName) { + this.revisionInfoEntityName = revisionInfoEntityName; versionsTablePrefix = properties.getProperty("org.jboss.envers.versionsTablePrefix", ""); versionsTableSuffix = properties.getProperty("org.jboss.envers.versionsTableSuffix", "_versions"); @@ -90,14 +90,14 @@ return revisionTypePropType; } - public String getRevisionInfoIdName() { - return revisionInfoIdName; - } - public String getRevisionEntityPath() { return revisionEntityPath; } + public String getRevisionInfoEntityName() { + return revisionInfoEntityName; + } + // public void addCustomVersionsTableName(String entityName, String tableName) { Modified: trunk/src/main/org/jboss/envers/query/VersionsQueryCreator.java =================================================================== --- trunk/src/main/org/jboss/envers/query/VersionsQueryCreator.java 2008-09-03 06:27:36 UTC (rev 140) +++ trunk/src/main/org/jboss/envers/query/VersionsQueryCreator.java 2008-09-03 07:29:48 UTC (rev 141) @@ -55,10 +55,14 @@ } /** - * Creates a query, which will return a list of three-element arrays, containing the entity instance, the number - * of the revision (unless a projection is set), at which instances of the given entity were modified, and the type - * of the revision ({@link org.jboss.envers.RevisionType}). Additional conditions that the results must satisfy - * may be specified. + * Creates a query, which selects the revisions, at which the given entity was modified. + * Unless an explicit projection is set, the result will be a list of three-element arrays, containing: + *
    + *
  1. the entity instance
  2. + *
  3. revision entity, corresponding to the revision at which the entity was modified. If no custom + * revision entity is used, this will be an instance of {@link org.jboss.envers.DefaultRevisionEntity}
  4. + *
  5. type of the revision (an enum instance of class {@link org.jboss.envers.RevisionType})
  6. . + * Additional conditions that the results must satisfy may be specified. * @param c Class of the entities for which to query. * @param selectEntitiesOnly If true, instead of a list of three-element arrays, a list of entites will be * returned as a result of executing this query. Modified: trunk/src/main/org/jboss/envers/query/impl/AbstractVersionsQuery.java =================================================================== --- trunk/src/main/org/jboss/envers/query/impl/AbstractVersionsQuery.java 2008-09-03 06:27:36 UTC (rev 140) +++ trunk/src/main/org/jboss/envers/query/impl/AbstractVersionsQuery.java 2008-09-03 07:29:48 UTC (rev 141) @@ -84,9 +84,6 @@ setQueryProperties(query); - System.out.println("QUERY: " + querySb); - System.out.println("PARAMS: " + queryParamValues); - return query.list(); } Modified: trunk/src/main/org/jboss/envers/query/impl/QueryBuilder.java =================================================================== --- trunk/src/main/org/jboss/envers/query/impl/QueryBuilder.java 2008-09-03 06:27:36 UTC (rev 140) +++ trunk/src/main/org/jboss/envers/query/impl/QueryBuilder.java 2008-09-03 07:29:48 UTC (rev 141) @@ -45,8 +45,7 @@ /** * - * @param entityName Main entity which should be selected. Its properties don't have to be referred to - * using the alias. + * @param entityName Main entity which should be selected. * @param alias Alias of the entity */ public QueryBuilder(String entityName, String alias) { @@ -70,8 +69,7 @@ /** * Add an entity from which to select. - * @param entityName Name of the entity from which to select. Its properties always have to be referred to - * using the alias. + * @param entityName Name of the entity from which to select. * @param alias Alias of the entity. Should be different than all other aliases. */ public void addFrom(String entityName, String alias) { @@ -167,7 +165,7 @@ private List getOrderList() { List orderList = new ArrayList(); for (Pair order : orders) { - orderList.add(order.getFirst() + " " + (order.getSecond() ? "asc" : "desc")); + orderList.add(alias + "." + order.getFirst() + " " + (order.getSecond() ? "asc" : "desc")); } return orderList; Modified: trunk/src/main/org/jboss/envers/query/impl/RevisionsOfEntityQuery.java =================================================================== --- trunk/src/main/org/jboss/envers/query/impl/RevisionsOfEntityQuery.java 2008-09-03 06:27:36 UTC (rev 140) +++ trunk/src/main/org/jboss/envers/query/impl/RevisionsOfEntityQuery.java 2008-09-03 07:29:48 UTC (rev 141) @@ -72,8 +72,9 @@ /* The query that should be executed in the versions table: - SELECT e (unless another projection is specified) FROM ent_ver e WHERE - e.revision_type != DEL AND (if selectDeletedEntities == false) + SELECT e (unless another projection is specified) FROM ent_ver e, rev_entity r WHERE + e.revision_type != DEL (if selectDeletedEntities == false) AND + e.revision = r.revision AND (all specified conditions, transformed, on the "e" entity) ORDER BY e.revision ASC (unless another order or projection is specified) */ @@ -92,26 +93,37 @@ qb.addOrder(revisionPropertyPath, true); } - // TODO if (!selectEntitiesOnly) { - //versionsCriteria.setFetchMode("e._revision", FetchMode.JOIN); - //versionsCriteria.createCriteria("e.originalId._revision").add(Property.forName("id").eq(1)); + qb.addFrom(verCfg.getVerEntCfg().getRevisionInfoEntityName(), "r"); + qb.getRootParameters().addWhere(verCfg.getVerEntCfg().getRevisionPropPath(), true, "=", "r.id", false); } - List queryResult = buildAndExecuteQuery(); + List queryResult = buildAndExecuteQuery(); if (hasProjection) { return queryResult; } else { List entities = new ArrayList(); String revisionTypePropertyName = verEntCfg.getRevisionTypePropName(); - for (Map versionsEntity : queryResult) { + for (Object resultRow : queryResult) { + Map versionsEntity; + Object revisionData; + + if (selectEntitiesOnly) { + versionsEntity = (Map) resultRow; + revisionData = null; + } else { + Object[] arrayResultRow = (Object[]) resultRow; + versionsEntity = (Map) arrayResultRow[0]; + revisionData = arrayResultRow[1]; + } + Number revision = getRevisionNumber(versionsEntity); Object entity = entityInstantiator.createInstanceFromVersionsEntity(entityName, versionsEntity, revision); if (!selectEntitiesOnly) { - entities.add(new Object[] { entity, revision, versionsEntity.get(revisionTypePropertyName) }); + entities.add(new Object[] { entity, revisionData, versionsEntity.get(revisionTypePropertyName) }); } else { entities.add(entity); } Modified: trunk/src/test/org/jboss/envers/test/integration/query/DeletedEntities.java =================================================================== --- trunk/src/test/org/jboss/envers/test/integration/query/DeletedEntities.java 2008-09-03 06:27:36 UTC (rev 140) +++ trunk/src/test/org/jboss/envers/test/integration/query/DeletedEntities.java 2008-09-03 07:29:48 UTC (rev 141) @@ -4,6 +4,7 @@ import org.jboss.envers.test.entities.StrIntTestEntity; import org.jboss.envers.query.VersionsRestrictions; import org.jboss.envers.RevisionType; +import org.jboss.envers.DefaultRevisionEntity; import org.hibernate.ejb.Ejb3Configuration; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; @@ -69,7 +70,7 @@ assert result.size() == 1; assert ((Object []) result.get(0))[0].equals(new StrIntTestEntity("b", 11, id2)); - assert ((Object []) result.get(0))[1].equals(1); + assert ((DefaultRevisionEntity) ((Object []) result.get(0))[1]).getId() == 1; assert ((Object []) result.get(0))[2].equals(RevisionType.ADD); } } Modified: trunk/src/test/org/jboss/envers/test/integration/query/SimpleQuery.java =================================================================== --- trunk/src/test/org/jboss/envers/test/integration/query/SimpleQuery.java 2008-09-03 06:27:36 UTC (rev 140) +++ trunk/src/test/org/jboss/envers/test/integration/query/SimpleQuery.java 2008-09-03 07:29:48 UTC (rev 141) @@ -7,6 +7,7 @@ import org.jboss.envers.query.RevisionProperty; import org.jboss.envers.query.RevisionTypeProperty; import org.jboss.envers.RevisionType; +import org.jboss.envers.DefaultRevisionEntity; import org.hibernate.ejb.Ejb3Configuration; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; @@ -192,9 +193,9 @@ assert ((Object []) result.get(1))[0].equals(new StrIntTestEntity("c", 10, id1)); assert ((Object []) result.get(2))[0].equals(new StrIntTestEntity(null, null, id1)); - assert ((Object []) result.get(0))[1].equals(1); - assert ((Object []) result.get(1))[1].equals(2); - assert ((Object []) result.get(2))[1].equals(4); + assert ((DefaultRevisionEntity) ((Object []) result.get(0))[1]).getId() == 1; + assert ((DefaultRevisionEntity) ((Object []) result.get(1))[1]).getId() == 2; + assert ((DefaultRevisionEntity) ((Object []) result.get(2))[1]).getId() == 4; assert ((Object []) result.get(0))[2].equals(RevisionType.ADD); assert ((Object []) result.get(1))[2].equals(RevisionType.MOD); From jboss-envers-commits at lists.jboss.org Wed Sep 3 03:50:01 2008 From: jboss-envers-commits at lists.jboss.org (jboss-envers-commits at lists.jboss.org) Date: Wed, 03 Sep 2008 03:50:01 -0400 Subject: [jboss-envers-commits] JBoss Envers SVN: r142 - in trunk/src/test/org/jboss/envers/test: entities/reventity and 2 other directories. Message-ID: Author: adamw Date: 2008-09-03 03:50:01 -0400 (Wed, 03 Sep 2008) New Revision: 142 Added: trunk/src/test/org/jboss/envers/test/entities/reventity/ trunk/src/test/org/jboss/envers/test/entities/reventity/CustomRevEntity.java trunk/src/test/org/jboss/envers/test/integration/query/CustomRevEntityQuery.java Modified: trunk/src/test/org/jboss/envers/test/integration/reventity/Custom.java Log: ENVERS-47: custom revision entity + rev-of-entities query test Copied: trunk/src/test/org/jboss/envers/test/entities/reventity/CustomRevEntity.java (from rev 138, trunk/src/test/org/jboss/envers/test/integration/reventity/CustomRevEntity.java) =================================================================== --- trunk/src/test/org/jboss/envers/test/entities/reventity/CustomRevEntity.java (rev 0) +++ trunk/src/test/org/jboss/envers/test/entities/reventity/CustomRevEntity.java 2008-09-03 07:50:01 UTC (rev 142) @@ -0,0 +1,59 @@ +package org.jboss.envers.test.entities.reventity; + +import org.jboss.envers.RevisionNumber; +import org.jboss.envers.RevisionTimestamp; +import org.jboss.envers.RevisionEntity; + +import javax.persistence.Id; +import javax.persistence.GeneratedValue; +import javax.persistence.Entity; + +/** + * @author Adam Warski (adam at warski dot org) + */ + at Entity + at RevisionEntity +public class CustomRevEntity { + @Id + @GeneratedValue + @RevisionNumber + private int customId; + + @RevisionTimestamp + private long customTimestamp; + + public int getCustomId() { + return customId; + } + + public void setCustomId(int customId) { + this.customId = customId; + } + + public long getCustomTimestamp() { + return customTimestamp; + } + + public void setCustomTimestamp(long customTimestamp) { + this.customTimestamp = customTimestamp; + } + + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof CustomRevEntity)) return false; + + CustomRevEntity that = (CustomRevEntity) o; + + if (customId != that.customId) return false; + if (customTimestamp != that.customTimestamp) return false; + + return true; + } + + public int hashCode() { + int result; + result = customId; + result = 31 * result + (int) (customTimestamp ^ (customTimestamp >>> 32)); + return result; + } +} Copied: trunk/src/test/org/jboss/envers/test/integration/query/CustomRevEntityQuery.java (from rev 138, trunk/src/test/org/jboss/envers/test/integration/query/RevisionConstraintQuery.java) =================================================================== --- trunk/src/test/org/jboss/envers/test/integration/query/CustomRevEntityQuery.java (rev 0) +++ trunk/src/test/org/jboss/envers/test/integration/query/CustomRevEntityQuery.java 2008-09-03 07:50:01 UTC (rev 142) @@ -0,0 +1,83 @@ +package org.jboss.envers.test.integration.query; + +import org.jboss.envers.test.integration.AbstractEntityTest; +import org.jboss.envers.test.entities.StrIntTestEntity; +import org.jboss.envers.test.entities.reventity.CustomRevEntity; +import org.jboss.envers.query.VersionsRestrictions; +import org.jboss.envers.query.RevisionProperty; +import org.hibernate.ejb.Ejb3Configuration; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +import javax.persistence.EntityManager; +import java.util.List; +import java.util.Arrays; + +/** + * @author Adam Warski (adam at warski dot org) + */ + at SuppressWarnings({"unchecked"}) +public class CustomRevEntityQuery extends AbstractEntityTest { + private Integer id1; + private Integer id2; + + public void configure(Ejb3Configuration cfg) { + cfg.addAnnotatedClass(CustomRevEntity.class); + cfg.addAnnotatedClass(StrIntTestEntity.class); + } + + @BeforeClass(dependsOnMethods = "init") + public void initData() { + // Revision 1 + EntityManager em = getEntityManager(); + em.getTransaction().begin(); + + StrIntTestEntity site1 = new StrIntTestEntity("a", 10); + StrIntTestEntity site2 = new StrIntTestEntity("b", 15); + + em.persist(site1); + em.persist(site2); + + id1 = site1.getId(); + id2 = site2.getId(); + + em.getTransaction().commit(); + + // Revision 2 + em.getTransaction().begin(); + + site1 = em.find(StrIntTestEntity.class, id1); + + site1.setStr1("c"); + + em.getTransaction().commit(); + } + + @Test + public void testRevisionsOfId1Query() { + List result = getVersionsReader().createQuery() + .forRevisionsOfEntity(StrIntTestEntity.class, false, true) + .add(VersionsRestrictions.idEq(id1)) + .getResultList(); + + assert result.get(0)[0].equals(new StrIntTestEntity("a", 10, id1)); + assert result.get(0)[1] instanceof CustomRevEntity; + assert ((CustomRevEntity) result.get(0)[1]).getCustomId() == 1; + + assert result.get(1)[0].equals(new StrIntTestEntity("c", 10, id1)); + assert result.get(1)[1] instanceof CustomRevEntity; + assert ((CustomRevEntity) result.get(1)[1]).getCustomId() == 2; + } + + @Test + public void testRevisionsOfId2Query() { + List result = getVersionsReader().createQuery() + .forRevisionsOfEntity(StrIntTestEntity.class, false, true) + .add(VersionsRestrictions.idEq(id2)) + .getResultList(); + + assert result.get(0)[0].equals(new StrIntTestEntity("b", 15, id2)); + assert result.get(0)[1] instanceof CustomRevEntity; + assert ((CustomRevEntity) result.get(0)[1]).getCustomId() == 1; + } +} \ No newline at end of file Property changes on: trunk/src/test/org/jboss/envers/test/integration/query/CustomRevEntityQuery.java ___________________________________________________________________ Name: svn:mergeinfo + Modified: trunk/src/test/org/jboss/envers/test/integration/reventity/Custom.java =================================================================== --- trunk/src/test/org/jboss/envers/test/integration/reventity/Custom.java 2008-09-03 07:29:48 UTC (rev 141) +++ trunk/src/test/org/jboss/envers/test/integration/reventity/Custom.java 2008-09-03 07:50:01 UTC (rev 142) @@ -2,6 +2,7 @@ import org.jboss.envers.test.integration.AbstractEntityTest; import org.jboss.envers.test.entities.StrTestEntity; +import org.jboss.envers.test.entities.reventity.CustomRevEntity; import org.jboss.envers.exception.RevisionDoesNotExistException; import org.jboss.envers.VersionsReader; import org.hibernate.ejb.Ejb3Configuration; From jboss-envers-commits at lists.jboss.org Wed Sep 3 04:10:24 2008 From: jboss-envers-commits at lists.jboss.org (jboss-envers-commits at lists.jboss.org) Date: Wed, 03 Sep 2008 04:10:24 -0400 Subject: [jboss-envers-commits] JBoss Envers SVN: r143 - in trunk: resources/main/META-INF and 10 other directories. Message-ID: Author: adamw Date: 2008-09-03 04:10:24 -0400 (Wed, 03 Sep 2008) New Revision: 143 Added: trunk/resources/main/META-INF/copyright.txt Modified: trunk/ trunk/src/main/org/jboss/envers/configuration/GlobalConfiguration.java trunk/src/main/org/jboss/envers/configuration/RevisionInfoConfiguration.java trunk/src/main/org/jboss/envers/configuration/metadata/EntityMappingData.java trunk/src/main/org/jboss/envers/entities/RelationType.java trunk/src/main/org/jboss/envers/entities/RevisionTypeType.java trunk/src/main/org/jboss/envers/entities/mapper/PersistentCollectionChangeData.java trunk/src/main/org/jboss/envers/entities/mapper/relation/AbstractOneToManyMapper.java trunk/src/main/org/jboss/envers/entities/mapper/relation/DetachedRelationQueryGenerator.java trunk/src/main/org/jboss/envers/entities/mapper/relation/ManyToManyNotOwningMapper.java trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyDetachedMapper.java trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/DetachedRelationInitializor.java trunk/src/main/org/jboss/envers/query/impl/Parameters.java trunk/src/main/org/jboss/envers/query/impl/QueryBuilder.java trunk/src/main/org/jboss/envers/revisioninfo/RevisionInfoQueryCreator.java trunk/src/main/org/jboss/envers/synchronization/work/PersistentCollectionChangeWorkUnit.java trunk/src/test/org/jboss/envers/test/integration/query/CustomRevEntityQuery.java Log: Adding missing license headers Property changes on: trunk ___________________________________________________________________ Name: svn:ignore - build.properties target build dist doc + header.txt build.properties target build dist doc Added: trunk/resources/main/META-INF/copyright.txt =================================================================== --- trunk/resources/main/META-INF/copyright.txt (rev 0) +++ trunk/resources/main/META-INF/copyright.txt 2008-09-03 08:10:24 UTC (rev 143) @@ -0,0 +1 @@ +(c) 2008, Adam Warski, JBoss Inc. \ No newline at end of file Modified: trunk/src/main/org/jboss/envers/configuration/GlobalConfiguration.java =================================================================== --- trunk/src/main/org/jboss/envers/configuration/GlobalConfiguration.java 2008-09-03 07:50:01 UTC (rev 142) +++ trunk/src/main/org/jboss/envers/configuration/GlobalConfiguration.java 2008-09-03 08:10:24 UTC (rev 143) @@ -1,3 +1,26 @@ +/* + * JBoss, Home of Professional Open Source + * + * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated + * by the @authors tag. All rights reserved. + * + * See the copyright.txt in the distribution for a full listing of individual + * contributors. 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.configuration; import java.util.Properties; @@ -12,7 +35,7 @@ // Should a warning, instead of an error and an exception, be logged, when an unsupported type is versioned private final boolean warnOnUnsupportedTypes; - + // Should the optimistic locking property of an entity be considered unversioned private final boolean unversionedOptimisticLockingField; @@ -24,9 +47,9 @@ String warnOnUnsupportedTypesStr = properties.getProperty("org.jboss.envers.warnOnUnsupportedTypes", "false"); warnOnUnsupportedTypes = Boolean.parseBoolean(warnOnUnsupportedTypesStr); - + String ignoreOptimisticLockingPropertyStr = properties.getProperty("org.jboss.envers.unversionedOptimisticLockingField", - "false"); + "false"); unversionedOptimisticLockingField = Boolean.parseBoolean(ignoreOptimisticLockingPropertyStr); } @@ -37,9 +60,9 @@ public boolean isWarnOnUnsupportedTypes() { return warnOnUnsupportedTypes; } - + public boolean isUnversionedOptimisticLockingField() { return unversionedOptimisticLockingField; } - + } Modified: trunk/src/main/org/jboss/envers/configuration/RevisionInfoConfiguration.java =================================================================== --- trunk/src/main/org/jboss/envers/configuration/RevisionInfoConfiguration.java 2008-09-03 07:50:01 UTC (rev 142) +++ trunk/src/main/org/jboss/envers/configuration/RevisionInfoConfiguration.java 2008-09-03 08:10:24 UTC (rev 143) @@ -1,3 +1,26 @@ +/* + * JBoss, Home of Professional Open Source + * + * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated + * by the @authors tag. All rights reserved. + * + * See the copyright.txt in the distribution for a full listing of individual + * contributors. 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.configuration; import org.jboss.envers.tools.reflection.YClass; Modified: trunk/src/main/org/jboss/envers/configuration/metadata/EntityMappingData.java =================================================================== --- trunk/src/main/org/jboss/envers/configuration/metadata/EntityMappingData.java 2008-09-03 07:50:01 UTC (rev 142) +++ trunk/src/main/org/jboss/envers/configuration/metadata/EntityMappingData.java 2008-09-03 08:10:24 UTC (rev 143) @@ -1,3 +1,26 @@ +/* + * JBoss, Home of Professional Open Source + * + * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated + * by the @authors tag. All rights reserved. + * + * See the copyright.txt in the distribution for a full listing of individual + * contributors. 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.configuration.metadata; import org.dom4j.Document; Modified: trunk/src/main/org/jboss/envers/entities/RelationType.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/RelationType.java 2008-09-03 07:50:01 UTC (rev 142) +++ trunk/src/main/org/jboss/envers/entities/RelationType.java 2008-09-03 08:10:24 UTC (rev 143) @@ -1,3 +1,26 @@ +/* + * JBoss, Home of Professional Open Source + * + * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated + * by the @authors tag. All rights reserved. + * + * See the copyright.txt in the distribution for a full listing of individual + * contributors. 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; /** Modified: trunk/src/main/org/jboss/envers/entities/RevisionTypeType.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/RevisionTypeType.java 2008-09-03 07:50:01 UTC (rev 142) +++ trunk/src/main/org/jboss/envers/entities/RevisionTypeType.java 2008-09-03 08:10:24 UTC (rev 143) @@ -1,3 +1,26 @@ +/* + * JBoss, Home of Professional Open Source + * + * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated + * by the @authors tag. All rights reserved. + * + * See the copyright.txt in the distribution for a full listing of individual + * contributors. 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; import org.hibernate.usertype.UserType; Modified: trunk/src/main/org/jboss/envers/entities/mapper/PersistentCollectionChangeData.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/PersistentCollectionChangeData.java 2008-09-03 07:50:01 UTC (rev 142) +++ trunk/src/main/org/jboss/envers/entities/mapper/PersistentCollectionChangeData.java 2008-09-03 08:10:24 UTC (rev 143) @@ -1,3 +1,26 @@ +/* + * JBoss, Home of Professional Open Source + * + * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated + * by the @authors tag. All rights reserved. + * + * See the copyright.txt in the distribution for a full listing of individual + * contributors. 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; import java.util.Map; Modified: trunk/src/main/org/jboss/envers/entities/mapper/relation/AbstractOneToManyMapper.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/AbstractOneToManyMapper.java 2008-09-03 07:50:01 UTC (rev 142) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/AbstractOneToManyMapper.java 2008-09-03 08:10:24 UTC (rev 143) @@ -1,3 +1,26 @@ +/* + * JBoss, Home of Professional Open Source + * + * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated + * by the @authors tag. All rights reserved. + * + * See the copyright.txt in the distribution for a full listing of individual + * contributors. 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.reader.VersionsReaderImplementor; Modified: trunk/src/main/org/jboss/envers/entities/mapper/relation/DetachedRelationQueryGenerator.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/DetachedRelationQueryGenerator.java 2008-09-03 07:50:01 UTC (rev 142) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/DetachedRelationQueryGenerator.java 2008-09-03 08:10:24 UTC (rev 143) @@ -1,3 +1,26 @@ +/* + * JBoss, Home of Professional Open Source + * + * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated + * by the @authors tag. All rights reserved. + * + * See the copyright.txt in the distribution for a full listing of individual + * contributors. 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.id.QueryParameterData; Modified: trunk/src/main/org/jboss/envers/entities/mapper/relation/ManyToManyNotOwningMapper.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/ManyToManyNotOwningMapper.java 2008-09-03 07:50:01 UTC (rev 142) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/ManyToManyNotOwningMapper.java 2008-09-03 08:10:24 UTC (rev 143) @@ -1,3 +1,26 @@ +/* + * JBoss, Home of Professional Open Source + * + * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated + * by the @authors tag. All rights reserved. + * + * See the copyright.txt in the distribution for a full listing of individual + * contributors. 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; Modified: trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyDetachedMapper.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyDetachedMapper.java 2008-09-03 07:50:01 UTC (rev 142) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyDetachedMapper.java 2008-09-03 08:10:24 UTC (rev 143) @@ -1,3 +1,26 @@ +/* + * JBoss, Home of Professional Open Source + * + * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated + * by the @authors tag. All rights reserved. + * + * See the copyright.txt in the distribution for a full listing of individual + * contributors. 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; Modified: trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/DetachedRelationInitializor.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/DetachedRelationInitializor.java 2008-09-03 07:50:01 UTC (rev 142) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/DetachedRelationInitializor.java 2008-09-03 08:10:24 UTC (rev 143) @@ -1,3 +1,26 @@ +/* + * JBoss, Home of Professional Open Source + * + * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated + * by the @authors tag. All rights reserved. + * + * See the copyright.txt in the distribution for a full listing of individual + * contributors. 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.entities.mapper.relation.lazy.proxy.Initializor; Modified: trunk/src/main/org/jboss/envers/query/impl/Parameters.java =================================================================== --- trunk/src/main/org/jboss/envers/query/impl/Parameters.java 2008-09-03 07:50:01 UTC (rev 142) +++ trunk/src/main/org/jboss/envers/query/impl/Parameters.java 2008-09-03 08:10:24 UTC (rev 143) @@ -1,3 +1,26 @@ +/* + * JBoss, Home of Professional Open Source + * + * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated + * by the @authors tag. All rights reserved. + * + * See the copyright.txt in the distribution for a full listing of individual + * contributors. 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.query.impl; import org.jboss.envers.tools.MutableInteger; Modified: trunk/src/main/org/jboss/envers/query/impl/QueryBuilder.java =================================================================== --- trunk/src/main/org/jboss/envers/query/impl/QueryBuilder.java 2008-09-03 07:50:01 UTC (rev 142) +++ trunk/src/main/org/jboss/envers/query/impl/QueryBuilder.java 2008-09-03 08:10:24 UTC (rev 143) @@ -1,3 +1,26 @@ +/* + * JBoss, Home of Professional Open Source + * + * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated + * by the @authors tag. All rights reserved. + * + * See the copyright.txt in the distribution for a full listing of individual + * contributors. 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.query.impl; import org.jboss.envers.tools.MutableInteger; Modified: trunk/src/main/org/jboss/envers/revisioninfo/RevisionInfoQueryCreator.java =================================================================== --- trunk/src/main/org/jboss/envers/revisioninfo/RevisionInfoQueryCreator.java 2008-09-03 07:50:01 UTC (rev 142) +++ trunk/src/main/org/jboss/envers/revisioninfo/RevisionInfoQueryCreator.java 2008-09-03 08:10:24 UTC (rev 143) @@ -1,3 +1,26 @@ +/* + * JBoss, Home of Professional Open Source + * + * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated + * by the @authors tag. All rights reserved. + * + * See the copyright.txt in the distribution for a full listing of individual + * contributors. 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.revisioninfo; import org.hibernate.Session; Modified: trunk/src/main/org/jboss/envers/synchronization/work/PersistentCollectionChangeWorkUnit.java =================================================================== --- trunk/src/main/org/jboss/envers/synchronization/work/PersistentCollectionChangeWorkUnit.java 2008-09-03 07:50:01 UTC (rev 142) +++ trunk/src/main/org/jboss/envers/synchronization/work/PersistentCollectionChangeWorkUnit.java 2008-09-03 08:10:24 UTC (rev 143) @@ -1,3 +1,26 @@ +/* + * JBoss, Home of Professional Open Source + * + * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated + * by the @authors tag. All rights reserved. + * + * See the copyright.txt in the distribution for a full listing of individual + * contributors. 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; Modified: trunk/src/test/org/jboss/envers/test/integration/query/CustomRevEntityQuery.java =================================================================== --- trunk/src/test/org/jboss/envers/test/integration/query/CustomRevEntityQuery.java 2008-09-03 07:50:01 UTC (rev 142) +++ trunk/src/test/org/jboss/envers/test/integration/query/CustomRevEntityQuery.java 2008-09-03 08:10:24 UTC (rev 143) @@ -4,14 +4,12 @@ import org.jboss.envers.test.entities.StrIntTestEntity; import org.jboss.envers.test.entities.reventity.CustomRevEntity; import org.jboss.envers.query.VersionsRestrictions; -import org.jboss.envers.query.RevisionProperty; import org.hibernate.ejb.Ejb3Configuration; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import javax.persistence.EntityManager; import java.util.List; -import java.util.Arrays; /** * @author Adam Warski (adam at warski dot org) From jboss-envers-commits at lists.jboss.org Wed Sep 3 04:12:08 2008 From: jboss-envers-commits at lists.jboss.org (jboss-envers-commits at lists.jboss.org) Date: Wed, 03 Sep 2008 04:12:08 -0400 Subject: [jboss-envers-commits] JBoss Envers SVN: r144 - in trunk/src/main/org/jboss/envers: query/criteria and 3 other directories. Message-ID: Author: adamw Date: 2008-09-03 04:12:08 -0400 (Wed, 03 Sep 2008) New Revision: 144 Added: trunk/src/main/org/jboss/envers/tools/query/ trunk/src/main/org/jboss/envers/tools/query/Parameters.java trunk/src/main/org/jboss/envers/tools/query/QueryBuilder.java Modified: trunk/src/main/org/jboss/envers/entities/mapper/id/AbstractIdMapper.java trunk/src/main/org/jboss/envers/entities/mapper/id/IdMapper.java trunk/src/main/org/jboss/envers/query/criteria/AggregatedFieldVersionsExpression.java trunk/src/main/org/jboss/envers/query/criteria/BetweenVersionsExpression.java trunk/src/main/org/jboss/envers/query/criteria/IdentifierEqVersionsExpression.java trunk/src/main/org/jboss/envers/query/criteria/InVersionsExpression.java trunk/src/main/org/jboss/envers/query/criteria/LogicalVersionsExpression.java trunk/src/main/org/jboss/envers/query/criteria/NotNullVersionsExpression.java trunk/src/main/org/jboss/envers/query/criteria/NotVersionsExpression.java trunk/src/main/org/jboss/envers/query/criteria/NullVersionsExpression.java trunk/src/main/org/jboss/envers/query/criteria/PropertyVersionsExpression.java trunk/src/main/org/jboss/envers/query/criteria/RelatedVersionsExpression.java trunk/src/main/org/jboss/envers/query/criteria/RevisionVersionsExpression.java trunk/src/main/org/jboss/envers/query/criteria/SimpleVersionsExpression.java trunk/src/main/org/jboss/envers/query/criteria/VersionsConjunction.java trunk/src/main/org/jboss/envers/query/criteria/VersionsCriterion.java trunk/src/main/org/jboss/envers/query/criteria/VersionsDisjunction.java trunk/src/main/org/jboss/envers/query/impl/AbstractVersionsQuery.java trunk/src/main/org/jboss/envers/query/impl/EntitiesAtRevisionQuery.java Log: ENVERS-47: moving query builder to tools Modified: trunk/src/main/org/jboss/envers/entities/mapper/id/AbstractIdMapper.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/id/AbstractIdMapper.java 2008-09-03 08:10:24 UTC (rev 143) +++ trunk/src/main/org/jboss/envers/entities/mapper/id/AbstractIdMapper.java 2008-09-03 08:12:08 UTC (rev 144) @@ -21,7 +21,7 @@ */ package org.jboss.envers.entities.mapper.id; -import org.jboss.envers.query.impl.Parameters; +import org.jboss.envers.tools.query.Parameters; import java.util.Iterator; import java.util.List; Modified: trunk/src/main/org/jboss/envers/entities/mapper/id/IdMapper.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/id/IdMapper.java 2008-09-03 08:10:24 UTC (rev 143) +++ trunk/src/main/org/jboss/envers/entities/mapper/id/IdMapper.java 2008-09-03 08:12:08 UTC (rev 144) @@ -21,7 +21,7 @@ */ package org.jboss.envers.entities.mapper.id; -import org.jboss.envers.query.impl.Parameters; +import org.jboss.envers.tools.query.Parameters; import java.util.Map; import java.util.List; Modified: trunk/src/main/org/jboss/envers/query/criteria/AggregatedFieldVersionsExpression.java =================================================================== --- trunk/src/main/org/jboss/envers/query/criteria/AggregatedFieldVersionsExpression.java 2008-09-03 08:10:24 UTC (rev 143) +++ trunk/src/main/org/jboss/envers/query/criteria/AggregatedFieldVersionsExpression.java 2008-09-03 08:12:08 UTC (rev 144) @@ -22,8 +22,8 @@ package org.jboss.envers.query.criteria; import org.jboss.envers.configuration.VersionsConfiguration; -import org.jboss.envers.query.impl.QueryBuilder; -import org.jboss.envers.query.impl.Parameters; +import org.jboss.envers.tools.query.QueryBuilder; +import org.jboss.envers.tools.query.Parameters; import java.util.List; import java.util.ArrayList; Modified: trunk/src/main/org/jboss/envers/query/criteria/BetweenVersionsExpression.java =================================================================== --- trunk/src/main/org/jboss/envers/query/criteria/BetweenVersionsExpression.java 2008-09-03 08:10:24 UTC (rev 143) +++ trunk/src/main/org/jboss/envers/query/criteria/BetweenVersionsExpression.java 2008-09-03 08:12:08 UTC (rev 144) @@ -22,8 +22,8 @@ package org.jboss.envers.query.criteria; import org.jboss.envers.configuration.VersionsConfiguration; -import org.jboss.envers.query.impl.QueryBuilder; -import org.jboss.envers.query.impl.Parameters; +import org.jboss.envers.tools.query.QueryBuilder; +import org.jboss.envers.tools.query.Parameters; /** * @author Adam Warski (adam at warski dot org) Modified: trunk/src/main/org/jboss/envers/query/criteria/IdentifierEqVersionsExpression.java =================================================================== --- trunk/src/main/org/jboss/envers/query/criteria/IdentifierEqVersionsExpression.java 2008-09-03 08:10:24 UTC (rev 143) +++ trunk/src/main/org/jboss/envers/query/criteria/IdentifierEqVersionsExpression.java 2008-09-03 08:12:08 UTC (rev 144) @@ -22,8 +22,8 @@ package org.jboss.envers.query.criteria; import org.jboss.envers.configuration.VersionsConfiguration; -import org.jboss.envers.query.impl.QueryBuilder; -import org.jboss.envers.query.impl.Parameters; +import org.jboss.envers.tools.query.QueryBuilder; +import org.jboss.envers.tools.query.Parameters; /** * @author Adam Warski (adam at warski dot org) Modified: trunk/src/main/org/jboss/envers/query/criteria/InVersionsExpression.java =================================================================== --- trunk/src/main/org/jboss/envers/query/criteria/InVersionsExpression.java 2008-09-03 08:10:24 UTC (rev 143) +++ trunk/src/main/org/jboss/envers/query/criteria/InVersionsExpression.java 2008-09-03 08:12:08 UTC (rev 144) @@ -22,8 +22,8 @@ package org.jboss.envers.query.criteria; import org.jboss.envers.configuration.VersionsConfiguration; -import org.jboss.envers.query.impl.QueryBuilder; -import org.jboss.envers.query.impl.Parameters; +import org.jboss.envers.tools.query.QueryBuilder; +import org.jboss.envers.tools.query.Parameters; /** * @author Adam Warski (adam at warski dot org) Modified: trunk/src/main/org/jboss/envers/query/criteria/LogicalVersionsExpression.java =================================================================== --- trunk/src/main/org/jboss/envers/query/criteria/LogicalVersionsExpression.java 2008-09-03 08:10:24 UTC (rev 143) +++ trunk/src/main/org/jboss/envers/query/criteria/LogicalVersionsExpression.java 2008-09-03 08:12:08 UTC (rev 144) @@ -22,8 +22,8 @@ package org.jboss.envers.query.criteria; import org.jboss.envers.configuration.VersionsConfiguration; -import org.jboss.envers.query.impl.QueryBuilder; -import org.jboss.envers.query.impl.Parameters; +import org.jboss.envers.tools.query.QueryBuilder; +import org.jboss.envers.tools.query.Parameters; /** * @author Adam Warski (adam at warski dot org) Modified: trunk/src/main/org/jboss/envers/query/criteria/NotNullVersionsExpression.java =================================================================== --- trunk/src/main/org/jboss/envers/query/criteria/NotNullVersionsExpression.java 2008-09-03 08:10:24 UTC (rev 143) +++ trunk/src/main/org/jboss/envers/query/criteria/NotNullVersionsExpression.java 2008-09-03 08:12:08 UTC (rev 144) @@ -23,8 +23,8 @@ import org.jboss.envers.entities.RelationDescription; import org.jboss.envers.configuration.VersionsConfiguration; -import org.jboss.envers.query.impl.QueryBuilder; -import org.jboss.envers.query.impl.Parameters; +import org.jboss.envers.tools.query.QueryBuilder; +import org.jboss.envers.tools.query.Parameters; /** * @author Adam Warski (adam at warski dot org) Modified: trunk/src/main/org/jboss/envers/query/criteria/NotVersionsExpression.java =================================================================== --- trunk/src/main/org/jboss/envers/query/criteria/NotVersionsExpression.java 2008-09-03 08:10:24 UTC (rev 143) +++ trunk/src/main/org/jboss/envers/query/criteria/NotVersionsExpression.java 2008-09-03 08:12:08 UTC (rev 144) @@ -22,8 +22,8 @@ package org.jboss.envers.query.criteria; import org.jboss.envers.configuration.VersionsConfiguration; -import org.jboss.envers.query.impl.QueryBuilder; -import org.jboss.envers.query.impl.Parameters; +import org.jboss.envers.tools.query.QueryBuilder; +import org.jboss.envers.tools.query.Parameters; /** * @author Adam Warski (adam at warski dot org) Modified: trunk/src/main/org/jboss/envers/query/criteria/NullVersionsExpression.java =================================================================== --- trunk/src/main/org/jboss/envers/query/criteria/NullVersionsExpression.java 2008-09-03 08:10:24 UTC (rev 143) +++ trunk/src/main/org/jboss/envers/query/criteria/NullVersionsExpression.java 2008-09-03 08:12:08 UTC (rev 144) @@ -23,8 +23,8 @@ import org.jboss.envers.entities.RelationDescription; import org.jboss.envers.configuration.VersionsConfiguration; -import org.jboss.envers.query.impl.QueryBuilder; -import org.jboss.envers.query.impl.Parameters; +import org.jboss.envers.tools.query.QueryBuilder; +import org.jboss.envers.tools.query.Parameters; /** * @author Adam Warski (adam at warski dot org) Modified: trunk/src/main/org/jboss/envers/query/criteria/PropertyVersionsExpression.java =================================================================== --- trunk/src/main/org/jboss/envers/query/criteria/PropertyVersionsExpression.java 2008-09-03 08:10:24 UTC (rev 143) +++ trunk/src/main/org/jboss/envers/query/criteria/PropertyVersionsExpression.java 2008-09-03 08:12:08 UTC (rev 144) @@ -22,8 +22,8 @@ package org.jboss.envers.query.criteria; import org.jboss.envers.configuration.VersionsConfiguration; -import org.jboss.envers.query.impl.QueryBuilder; -import org.jboss.envers.query.impl.Parameters; +import org.jboss.envers.tools.query.QueryBuilder; +import org.jboss.envers.tools.query.Parameters; /** * @author Adam Warski (adam at warski dot org) Modified: trunk/src/main/org/jboss/envers/query/criteria/RelatedVersionsExpression.java =================================================================== --- trunk/src/main/org/jboss/envers/query/criteria/RelatedVersionsExpression.java 2008-09-03 08:10:24 UTC (rev 143) +++ trunk/src/main/org/jboss/envers/query/criteria/RelatedVersionsExpression.java 2008-09-03 08:12:08 UTC (rev 144) @@ -24,8 +24,8 @@ import org.jboss.envers.exception.VersionsException; import org.jboss.envers.entities.RelationDescription; import org.jboss.envers.configuration.VersionsConfiguration; -import org.jboss.envers.query.impl.QueryBuilder; -import org.jboss.envers.query.impl.Parameters; +import org.jboss.envers.tools.query.QueryBuilder; +import org.jboss.envers.tools.query.Parameters; /** * @author Adam Warski (adam at warski dot org) Modified: trunk/src/main/org/jboss/envers/query/criteria/RevisionVersionsExpression.java =================================================================== --- trunk/src/main/org/jboss/envers/query/criteria/RevisionVersionsExpression.java 2008-09-03 08:10:24 UTC (rev 143) +++ trunk/src/main/org/jboss/envers/query/criteria/RevisionVersionsExpression.java 2008-09-03 08:12:08 UTC (rev 144) @@ -22,8 +22,8 @@ package org.jboss.envers.query.criteria; import org.jboss.envers.configuration.VersionsConfiguration; -import org.jboss.envers.query.impl.QueryBuilder; -import org.jboss.envers.query.impl.Parameters; +import org.jboss.envers.tools.query.QueryBuilder; +import org.jboss.envers.tools.query.Parameters; /** * @author Adam Warski (adam at warski dot org) Modified: trunk/src/main/org/jboss/envers/query/criteria/SimpleVersionsExpression.java =================================================================== --- trunk/src/main/org/jboss/envers/query/criteria/SimpleVersionsExpression.java 2008-09-03 08:10:24 UTC (rev 143) +++ trunk/src/main/org/jboss/envers/query/criteria/SimpleVersionsExpression.java 2008-09-03 08:12:08 UTC (rev 144) @@ -24,8 +24,8 @@ import org.jboss.envers.exception.VersionsException; import org.jboss.envers.entities.RelationDescription; import org.jboss.envers.configuration.VersionsConfiguration; -import org.jboss.envers.query.impl.QueryBuilder; -import org.jboss.envers.query.impl.Parameters; +import org.jboss.envers.tools.query.QueryBuilder; +import org.jboss.envers.tools.query.Parameters; /** * @author Adam Warski (adam at warski dot org) Modified: trunk/src/main/org/jboss/envers/query/criteria/VersionsConjunction.java =================================================================== --- trunk/src/main/org/jboss/envers/query/criteria/VersionsConjunction.java 2008-09-03 08:10:24 UTC (rev 143) +++ trunk/src/main/org/jboss/envers/query/criteria/VersionsConjunction.java 2008-09-03 08:12:08 UTC (rev 144) @@ -22,8 +22,8 @@ package org.jboss.envers.query.criteria; import org.jboss.envers.configuration.VersionsConfiguration; -import org.jboss.envers.query.impl.QueryBuilder; -import org.jboss.envers.query.impl.Parameters; +import org.jboss.envers.tools.query.QueryBuilder; +import org.jboss.envers.tools.query.Parameters; import java.util.ArrayList; import java.util.List; Modified: trunk/src/main/org/jboss/envers/query/criteria/VersionsCriterion.java =================================================================== --- trunk/src/main/org/jboss/envers/query/criteria/VersionsCriterion.java 2008-09-03 08:10:24 UTC (rev 143) +++ trunk/src/main/org/jboss/envers/query/criteria/VersionsCriterion.java 2008-09-03 08:12:08 UTC (rev 144) @@ -22,8 +22,8 @@ package org.jboss.envers.query.criteria; import org.jboss.envers.configuration.VersionsConfiguration; -import org.jboss.envers.query.impl.QueryBuilder; -import org.jboss.envers.query.impl.Parameters; +import org.jboss.envers.tools.query.QueryBuilder; +import org.jboss.envers.tools.query.Parameters; /** * @author Adam Warski (adam at warski dot org) Modified: trunk/src/main/org/jboss/envers/query/criteria/VersionsDisjunction.java =================================================================== --- trunk/src/main/org/jboss/envers/query/criteria/VersionsDisjunction.java 2008-09-03 08:10:24 UTC (rev 143) +++ trunk/src/main/org/jboss/envers/query/criteria/VersionsDisjunction.java 2008-09-03 08:12:08 UTC (rev 144) @@ -22,8 +22,8 @@ package org.jboss.envers.query.criteria; import org.jboss.envers.configuration.VersionsConfiguration; -import org.jboss.envers.query.impl.QueryBuilder; -import org.jboss.envers.query.impl.Parameters; +import org.jboss.envers.tools.query.QueryBuilder; +import org.jboss.envers.tools.query.Parameters; import java.util.List; import java.util.ArrayList; Modified: trunk/src/main/org/jboss/envers/query/impl/AbstractVersionsQuery.java =================================================================== --- trunk/src/main/org/jboss/envers/query/impl/AbstractVersionsQuery.java 2008-09-03 08:10:24 UTC (rev 143) +++ trunk/src/main/org/jboss/envers/query/impl/AbstractVersionsQuery.java 2008-09-03 08:12:08 UTC (rev 144) @@ -31,6 +31,7 @@ import org.jboss.envers.configuration.VersionsConfiguration; import org.jboss.envers.tools.Pair; import org.jboss.envers.tools.Triple; +import org.jboss.envers.tools.query.QueryBuilder; import org.hibernate.*; import javax.persistence.NonUniqueResultException; Modified: trunk/src/main/org/jboss/envers/query/impl/EntitiesAtRevisionQuery.java =================================================================== --- trunk/src/main/org/jboss/envers/query/impl/EntitiesAtRevisionQuery.java 2008-09-03 08:10:24 UTC (rev 143) +++ trunk/src/main/org/jboss/envers/query/impl/EntitiesAtRevisionQuery.java 2008-09-03 08:12:08 UTC (rev 144) @@ -24,6 +24,7 @@ import org.jboss.envers.reader.VersionsReaderImplementor; import org.jboss.envers.query.criteria.VersionsCriterion; import org.jboss.envers.RevisionType; +import org.jboss.envers.tools.query.QueryBuilder; import org.jboss.envers.configuration.VersionsEntitiesConfiguration; import org.jboss.envers.configuration.VersionsConfiguration; Copied: trunk/src/main/org/jboss/envers/tools/query/Parameters.java (from rev 143, trunk/src/main/org/jboss/envers/query/impl/Parameters.java) =================================================================== --- trunk/src/main/org/jboss/envers/tools/query/Parameters.java (rev 0) +++ trunk/src/main/org/jboss/envers/tools/query/Parameters.java 2008-09-03 08:12:08 UTC (rev 144) @@ -0,0 +1,227 @@ +/* + * JBoss, Home of Professional Open Source + * + * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated + * by the @authors tag. All rights reserved. + * + * See the copyright.txt in the distribution for a full listing of individual + * contributors. 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.tools.query; + +import org.jboss.envers.tools.MutableInteger; +import org.jboss.envers.tools.MutableBoolean; + +import java.util.List; +import java.util.ArrayList; +import java.util.Map; +import java.util.HashMap; + +/** + * Parameters of a query, built using {@link QueryBuilder}. + * @author Adam Warski (adam at warski dot org) + */ +public class Parameters { + public final static String AND = "and"; + public final static String OR = "or"; + + /** + * Main alias of the entity. + */ + private final String alias; + /** + * Connective between these parameters - "and" or "or". + */ + private final String connective; + /** + * For use by the parameter generator. Must be the same in all "child" (and parent) parameters. + */ + private final MutableInteger queryParamCounter; + + /** + * A list of sub-parameters (parameters with a different connective). + */ + private final List subParameters; + /** + * A list of negated parameters. + */ + private final List negatedParameters; + /** + * A list of complete where-expressions. + */ + private final List expressions; + /** + * Values of parameters used in expressions. + */ + private final Map localQueryParamValues; + + Parameters(String alias, String connective, MutableInteger queryParamCounter) { + this.alias = alias; + this.connective = connective; + this.queryParamCounter = queryParamCounter; + + subParameters = new ArrayList(); + negatedParameters = new ArrayList(); + expressions = new ArrayList(); + localQueryParamValues = new HashMap(); + } + + private String generateQueryParam() { + return "_p" + queryParamCounter.getAndIncrease(); + } + + /** + * Adds sub-parameters with a new connective. That is, the parameters will be grouped in parentheses in the + * generated query, e.g.: ... and (exp1 or exp2) and ..., assuming the old connective is "and", and the + * new connective is "or". + * @param newConnective New connective of the parameters. + * @return Sub-parameters with the given connective. + */ + public Parameters addSubParameters(String newConnective) { + if (connective.equals(newConnective)) { + return this; + } else { + Parameters newParams = new Parameters(alias, newConnective, queryParamCounter); + subParameters.add(newParams); + return newParams; + } + } + + /** + * Adds negated parameters, by default with the "and" connective. These paremeters will be grouped in parentheses + * in the generated query and negated, e.g. ... not (exp1 and exp2) ... + * @return Negated sub paremters. + */ + public Parameters addNegatedParameters() { + Parameters newParams = new Parameters(alias, AND, queryParamCounter); + negatedParameters.add(newParams); + return newParams; + } + + public void addWhere(String left, String op, String right) { + addWhere(left, true, op, right, true); + } + + public void addWhere(String left, boolean addAliasLeft, String op, String right, boolean addAliasRight) { + StringBuilder expression = new StringBuilder(); + + if (addAliasLeft) { expression.append(alias).append("."); } + expression.append(left); + + expression.append(" ").append(op).append(" "); + + if (addAliasRight) { expression.append(alias).append("."); } + expression.append(right); + + expressions.add(expression.toString()); + } + + public void addWhereWithParam(String left, String op, Object paramValue) { + addWhereWithParam(left, true, op, paramValue); + } + + public void addWhereWithParam(String left, boolean addAlias, String op, Object paramValue) { + StringBuilder expression = new StringBuilder(); + + if (addAlias) { expression.append(alias).append("."); } + expression.append(left); + + expression.append(" ").append(op).append(" "); + + String paramName = generateQueryParam(); + localQueryParamValues.put(paramName, paramValue); + expression.append(":").append(paramName); + + expressions.add(expression.toString()); + } + + public void addWhereWithParams(String left, String opStart, Object[] paramValues, String opEnd) { + StringBuilder expression = new StringBuilder(); + + expression.append(alias).append(".").append(left).append(" ").append(opStart); + + for (int i=0; i queryParamValues) { + MutableBoolean isFirst = new MutableBoolean(true); + + for (String expression : expressions) { + append(sb, expression, isFirst); + } + + for (Parameters sub : subParameters) { + if (!subParameters.isEmpty()) { + append(sb, "(", isFirst); + sub.build(sb, queryParamValues); + sb.append(")"); + } + } + + for (Parameters negated : negatedParameters) { + if (!negatedParameters.isEmpty()) { + append(sb, "not (", isFirst); + negated.build(sb, queryParamValues); + sb.append(")"); + } + } + + queryParamValues.putAll(localQueryParamValues); + } +} + Copied: trunk/src/main/org/jboss/envers/tools/query/QueryBuilder.java (from rev 143, trunk/src/main/org/jboss/envers/query/impl/QueryBuilder.java) =================================================================== --- trunk/src/main/org/jboss/envers/tools/query/QueryBuilder.java (rev 0) +++ trunk/src/main/org/jboss/envers/tools/query/QueryBuilder.java 2008-09-03 08:12:08 UTC (rev 144) @@ -0,0 +1,196 @@ +/* + * JBoss, Home of Professional Open Source + * + * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated + * by the @authors tag. All rights reserved. + * + * See the copyright.txt in the distribution for a full listing of individual + * contributors. 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.tools.query; + +import org.jboss.envers.tools.MutableInteger; +import org.jboss.envers.tools.Pair; +import org.jboss.envers.tools.StringTools; + +import java.util.Map; +import java.util.List; +import java.util.ArrayList; + +/** + * A class for incrementaly building a HQL query. + * @author Adam Warski (adam at warski dot org) + */ +public class QueryBuilder { + private final String entityName; + private final String alias; + + /** + * For use by alias generator (in case an alias is not provided by the user). + */ + private final MutableInteger aliasCounter; + /** + * For use by parameter generator, in {@link Parameters}. This counter must be + * the same in all parameters and sub-queries of this query. + */ + private final MutableInteger paramCounter; + /** + * Main "where" parameters for this query. + */ + private final Parameters rootParameters; + + /** + * A list of pairs (from entity name, alias name). + */ + private final List> froms; + /** + * A list of pairs (property name, order ascending?). + */ + private final List> orders; + /** + * A list of complete projection definitions: either a sole property name, or a function(property name). + */ + private final List projections; + + /** + * + * @param entityName Main entity which should be selected. + * @param alias Alias of the entity + */ + public QueryBuilder(String entityName, String alias) { + this(entityName, alias, new MutableInteger(), new MutableInteger()); + } + + private QueryBuilder(String entityName, String alias, MutableInteger aliasCounter, MutableInteger paramCounter) { + this.entityName = entityName; + this.alias = alias; + this.aliasCounter = aliasCounter; + this.paramCounter = paramCounter; + + rootParameters = new Parameters(alias, "and", paramCounter); + + froms = new ArrayList>(); + orders = new ArrayList>(); + projections = new ArrayList(); + + addFrom(entityName, alias); + } + + /** + * Add an entity from which to select. + * @param entityName Name of the entity from which to select. + * @param alias Alias of the entity. Should be different than all other aliases. + */ + public void addFrom(String entityName, String alias) { + froms.add(Pair.make(entityName, alias)); + } + + private String generateAlias() { + return "_e" + aliasCounter.getAndIncrease(); + } + + /** + * @return A sub-query builder for the same entity (with an auto-generated alias). The sub-query can + * be later used as a value of a parameter. + */ + public QueryBuilder newSubQueryBuilder() { + return newSubQueryBuilder(entityName, generateAlias()); + } + + /** + * @param entityName Entity name, which will be the main entity for the sub-query. + * @param alias Alias of the entity, which can later be used in parameters. + * @return A sub-query builder for the given entity, with the given alias. The sub-query can + * be later used as a value of a parameter. + */ + public QueryBuilder newSubQueryBuilder(String entityName, String alias) { + return new QueryBuilder(entityName, alias, aliasCounter, paramCounter); + } + + public Parameters getRootParameters() { + return rootParameters; + } + + public void addOrder(String propertyName, boolean ascending) { + orders.add(Pair.make(propertyName, ascending)); + } + + public void addProjection(String function, String propertyName, boolean distinct) { + if (function == null) { + projections.add((distinct ? "distinct " : "") + alias + "." + propertyName); + } else { + projections.add(function + "(" + (distinct ? "distinct " : "") + alias + "." + propertyName + ")"); + } + } + + /** + * Builds the given query, appending results to the given string buffer, and adding all query parameter values + * that are used to the map provided. + * @param sb String builder to which the query will be appended. + * @param queryParamValues Values of parameters used in the query. + */ + public void build(StringBuilder sb, Map queryParamValues) { + sb.append("select "); + if (projections.size() > 0) { + // all projections separated with commas + StringTools.append(sb, projections.iterator(), ", "); + } else { + // all aliases separated with commas + StringTools.append(sb, getAliasList().iterator(), ", "); + } + sb.append(" from "); + // all from entities with aliases, separated with commas + StringTools.append(sb, getFromList().iterator(), ", "); + // where part - rootParameters + if (!rootParameters.isEmpty()) { + sb.append(" where "); + rootParameters.build(sb, queryParamValues); + } + // orders + if (orders.size() > 0) { + sb.append(" order by "); + StringTools.append(sb, getOrderList().iterator(), ", "); + } + } + + private List getAliasList() { + List aliasList = new ArrayList(); + for (Pair from : froms) { + aliasList.add(from.getSecond()); + } + + return aliasList; + } + + private List getFromList() { + List fromList = new ArrayList(); + for (Pair from : froms) { + fromList.add(from.getFirst() + " " + from.getSecond()); + } + + return fromList; + } + + private List getOrderList() { + List orderList = new ArrayList(); + for (Pair order : orders) { + orderList.add(alias + "." + order.getFirst() + " " + (order.getSecond() ? "asc" : "desc")); + } + + return orderList; + } +} From jboss-envers-commits at lists.jboss.org Wed Sep 3 04:13:16 2008 From: jboss-envers-commits at lists.jboss.org (jboss-envers-commits at lists.jboss.org) Date: Wed, 03 Sep 2008 04:13:16 -0400 Subject: [jboss-envers-commits] JBoss Envers SVN: r145 - in trunk/src: test/org/jboss/envers/test/integration/reventity and 1 other directory. Message-ID: Author: adamw Date: 2008-09-03 04:13:16 -0400 (Wed, 03 Sep 2008) New Revision: 145 Removed: trunk/src/main/org/jboss/envers/query/impl/Parameters.java trunk/src/main/org/jboss/envers/query/impl/QueryBuilder.java trunk/src/test/org/jboss/envers/test/integration/reventity/CustomRevEntity.java Log: Cleanup Deleted: trunk/src/main/org/jboss/envers/query/impl/Parameters.java =================================================================== --- trunk/src/main/org/jboss/envers/query/impl/Parameters.java 2008-09-03 08:12:08 UTC (rev 144) +++ trunk/src/main/org/jboss/envers/query/impl/Parameters.java 2008-09-03 08:13:16 UTC (rev 145) @@ -1,227 +0,0 @@ -/* - * JBoss, Home of Professional Open Source - * - * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated - * by the @authors tag. All rights reserved. - * - * See the copyright.txt in the distribution for a full listing of individual - * contributors. 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.query.impl; - -import org.jboss.envers.tools.MutableInteger; -import org.jboss.envers.tools.MutableBoolean; - -import java.util.List; -import java.util.ArrayList; -import java.util.Map; -import java.util.HashMap; - -/** - * Parameters of a query, built using {@link org.jboss.envers.query.impl.QueryBuilder}. - * @author Adam Warski (adam at warski dot org) - */ -public class Parameters { - public final static String AND = "and"; - public final static String OR = "or"; - - /** - * Main alias of the entity. - */ - private final String alias; - /** - * Connective between these parameters - "and" or "or". - */ - private final String connective; - /** - * For use by the parameter generator. Must be the same in all "child" (and parent) parameters. - */ - private final MutableInteger queryParamCounter; - - /** - * A list of sub-parameters (parameters with a different connective). - */ - private final List subParameters; - /** - * A list of negated parameters. - */ - private final List negatedParameters; - /** - * A list of complete where-expressions. - */ - private final List expressions; - /** - * Values of parameters used in expressions. - */ - private final Map localQueryParamValues; - - Parameters(String alias, String connective, MutableInteger queryParamCounter) { - this.alias = alias; - this.connective = connective; - this.queryParamCounter = queryParamCounter; - - subParameters = new ArrayList(); - negatedParameters = new ArrayList(); - expressions = new ArrayList(); - localQueryParamValues = new HashMap(); - } - - private String generateQueryParam() { - return "_p" + queryParamCounter.getAndIncrease(); - } - - /** - * Adds sub-parameters with a new connective. That is, the parameters will be grouped in parentheses in the - * generated query, e.g.: ... and (exp1 or exp2) and ..., assuming the old connective is "and", and the - * new connective is "or". - * @param newConnective New connective of the parameters. - * @return Sub-parameters with the given connective. - */ - public Parameters addSubParameters(String newConnective) { - if (connective.equals(newConnective)) { - return this; - } else { - Parameters newParams = new Parameters(alias, newConnective, queryParamCounter); - subParameters.add(newParams); - return newParams; - } - } - - /** - * Adds negated parameters, by default with the "and" connective. These paremeters will be grouped in parentheses - * in the generated query and negated, e.g. ... not (exp1 and exp2) ... - * @return Negated sub paremters. - */ - public Parameters addNegatedParameters() { - Parameters newParams = new Parameters(alias, AND, queryParamCounter); - negatedParameters.add(newParams); - return newParams; - } - - public void addWhere(String left, String op, String right) { - addWhere(left, true, op, right, true); - } - - public void addWhere(String left, boolean addAliasLeft, String op, String right, boolean addAliasRight) { - StringBuilder expression = new StringBuilder(); - - if (addAliasLeft) { expression.append(alias).append("."); } - expression.append(left); - - expression.append(" ").append(op).append(" "); - - if (addAliasRight) { expression.append(alias).append("."); } - expression.append(right); - - expressions.add(expression.toString()); - } - - public void addWhereWithParam(String left, String op, Object paramValue) { - addWhereWithParam(left, true, op, paramValue); - } - - public void addWhereWithParam(String left, boolean addAlias, String op, Object paramValue) { - StringBuilder expression = new StringBuilder(); - - if (addAlias) { expression.append(alias).append("."); } - expression.append(left); - - expression.append(" ").append(op).append(" "); - - String paramName = generateQueryParam(); - localQueryParamValues.put(paramName, paramValue); - expression.append(":").append(paramName); - - expressions.add(expression.toString()); - } - - public void addWhereWithParams(String left, String opStart, Object[] paramValues, String opEnd) { - StringBuilder expression = new StringBuilder(); - - expression.append(alias).append(".").append(left).append(" ").append(opStart); - - for (int i=0; i queryParamValues) { - MutableBoolean isFirst = new MutableBoolean(true); - - for (String expression : expressions) { - append(sb, expression, isFirst); - } - - for (Parameters sub : subParameters) { - if (!subParameters.isEmpty()) { - append(sb, "(", isFirst); - sub.build(sb, queryParamValues); - sb.append(")"); - } - } - - for (Parameters negated : negatedParameters) { - if (!negatedParameters.isEmpty()) { - append(sb, "not (", isFirst); - negated.build(sb, queryParamValues); - sb.append(")"); - } - } - - queryParamValues.putAll(localQueryParamValues); - } -} - Deleted: trunk/src/main/org/jboss/envers/query/impl/QueryBuilder.java =================================================================== --- trunk/src/main/org/jboss/envers/query/impl/QueryBuilder.java 2008-09-03 08:12:08 UTC (rev 144) +++ trunk/src/main/org/jboss/envers/query/impl/QueryBuilder.java 2008-09-03 08:13:16 UTC (rev 145) @@ -1,196 +0,0 @@ -/* - * JBoss, Home of Professional Open Source - * - * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated - * by the @authors tag. All rights reserved. - * - * See the copyright.txt in the distribution for a full listing of individual - * contributors. 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.query.impl; - -import org.jboss.envers.tools.MutableInteger; -import org.jboss.envers.tools.Pair; -import org.jboss.envers.tools.StringTools; - -import java.util.Map; -import java.util.List; -import java.util.ArrayList; - -/** - * A class for incrementaly building a HQL query. - * @author Adam Warski (adam at warski dot org) - */ -public class QueryBuilder { - private final String entityName; - private final String alias; - - /** - * For use by alias generator (in case an alias is not provided by the user). - */ - private final MutableInteger aliasCounter; - /** - * For use by parameter generator, in {@link org.jboss.envers.query.impl.Parameters}. This counter must be - * the same in all parameters and sub-queries of this query. - */ - private final MutableInteger paramCounter; - /** - * Main "where" parameters for this query. - */ - private final Parameters rootParameters; - - /** - * A list of pairs (from entity name, alias name). - */ - private final List> froms; - /** - * A list of pairs (property name, order ascending?). - */ - private final List> orders; - /** - * A list of complete projection definitions: either a sole property name, or a function(property name). - */ - private final List projections; - - /** - * - * @param entityName Main entity which should be selected. - * @param alias Alias of the entity - */ - public QueryBuilder(String entityName, String alias) { - this(entityName, alias, new MutableInteger(), new MutableInteger()); - } - - private QueryBuilder(String entityName, String alias, MutableInteger aliasCounter, MutableInteger paramCounter) { - this.entityName = entityName; - this.alias = alias; - this.aliasCounter = aliasCounter; - this.paramCounter = paramCounter; - - rootParameters = new Parameters(alias, "and", paramCounter); - - froms = new ArrayList>(); - orders = new ArrayList>(); - projections = new ArrayList(); - - addFrom(entityName, alias); - } - - /** - * Add an entity from which to select. - * @param entityName Name of the entity from which to select. - * @param alias Alias of the entity. Should be different than all other aliases. - */ - public void addFrom(String entityName, String alias) { - froms.add(Pair.make(entityName, alias)); - } - - private String generateAlias() { - return "_e" + aliasCounter.getAndIncrease(); - } - - /** - * @return A sub-query builder for the same entity (with an auto-generated alias). The sub-query can - * be later used as a value of a parameter. - */ - public QueryBuilder newSubQueryBuilder() { - return newSubQueryBuilder(entityName, generateAlias()); - } - - /** - * @param entityName Entity name, which will be the main entity for the sub-query. - * @param alias Alias of the entity, which can later be used in parameters. - * @return A sub-query builder for the given entity, with the given alias. The sub-query can - * be later used as a value of a parameter. - */ - public QueryBuilder newSubQueryBuilder(String entityName, String alias) { - return new QueryBuilder(entityName, alias, aliasCounter, paramCounter); - } - - public Parameters getRootParameters() { - return rootParameters; - } - - public void addOrder(String propertyName, boolean ascending) { - orders.add(Pair.make(propertyName, ascending)); - } - - public void addProjection(String function, String propertyName, boolean distinct) { - if (function == null) { - projections.add((distinct ? "distinct " : "") + alias + "." + propertyName); - } else { - projections.add(function + "(" + (distinct ? "distinct " : "") + alias + "." + propertyName + ")"); - } - } - - /** - * Builds the given query, appending results to the given string buffer, and adding all query parameter values - * that are used to the map provided. - * @param sb String builder to which the query will be appended. - * @param queryParamValues Values of parameters used in the query. - */ - public void build(StringBuilder sb, Map queryParamValues) { - sb.append("select "); - if (projections.size() > 0) { - // all projections separated with commas - StringTools.append(sb, projections.iterator(), ", "); - } else { - // all aliases separated with commas - StringTools.append(sb, getAliasList().iterator(), ", "); - } - sb.append(" from "); - // all from entities with aliases, separated with commas - StringTools.append(sb, getFromList().iterator(), ", "); - // where part - rootParameters - if (!rootParameters.isEmpty()) { - sb.append(" where "); - rootParameters.build(sb, queryParamValues); - } - // orders - if (orders.size() > 0) { - sb.append(" order by "); - StringTools.append(sb, getOrderList().iterator(), ", "); - } - } - - private List getAliasList() { - List aliasList = new ArrayList(); - for (Pair from : froms) { - aliasList.add(from.getSecond()); - } - - return aliasList; - } - - private List getFromList() { - List fromList = new ArrayList(); - for (Pair from : froms) { - fromList.add(from.getFirst() + " " + from.getSecond()); - } - - return fromList; - } - - private List getOrderList() { - List orderList = new ArrayList(); - for (Pair order : orders) { - orderList.add(alias + "." + order.getFirst() + " " + (order.getSecond() ? "asc" : "desc")); - } - - return orderList; - } -} Deleted: trunk/src/test/org/jboss/envers/test/integration/reventity/CustomRevEntity.java =================================================================== --- trunk/src/test/org/jboss/envers/test/integration/reventity/CustomRevEntity.java 2008-09-03 08:12:08 UTC (rev 144) +++ trunk/src/test/org/jboss/envers/test/integration/reventity/CustomRevEntity.java 2008-09-03 08:13:16 UTC (rev 145) @@ -1,59 +0,0 @@ -package org.jboss.envers.test.integration.reventity; - -import org.jboss.envers.RevisionNumber; -import org.jboss.envers.RevisionTimestamp; -import org.jboss.envers.RevisionEntity; - -import javax.persistence.Id; -import javax.persistence.GeneratedValue; -import javax.persistence.Entity; - -/** - * @author Adam Warski (adam at warski dot org) - */ - at Entity - at RevisionEntity -public class CustomRevEntity { - @Id - @GeneratedValue - @RevisionNumber - private int customId; - - @RevisionTimestamp - private long customTimestamp; - - public int getCustomId() { - return customId; - } - - public void setCustomId(int customId) { - this.customId = customId; - } - - public long getCustomTimestamp() { - return customTimestamp; - } - - public void setCustomTimestamp(long customTimestamp) { - this.customTimestamp = customTimestamp; - } - - public boolean equals(Object o) { - if (this == o) return true; - if (!(o instanceof CustomRevEntity)) return false; - - CustomRevEntity that = (CustomRevEntity) o; - - if (customId != that.customId) return false; - if (customTimestamp != that.customTimestamp) return false; - - return true; - } - - public int hashCode() { - int result; - result = customId; - result = 31 * result + (int) (customTimestamp ^ (customTimestamp >>> 32)); - return result; - } -} From jboss-envers-commits at lists.jboss.org Wed Sep 3 04:41:02 2008 From: jboss-envers-commits at lists.jboss.org (jboss-envers-commits at lists.jboss.org) Date: Wed, 03 Sep 2008 04:41:02 -0400 Subject: [jboss-envers-commits] JBoss Envers SVN: r146 - tags. Message-ID: Author: adamw Date: 2008-09-03 04:41:02 -0400 (Wed, 03 Sep 2008) New Revision: 146 Added: tags/1.1.0-beta1/ Log: Tagging the 1.1.0.beta1 release Copied: tags/1.1.0-beta1 (from rev 145, trunk) From jboss-envers-commits at lists.jboss.org Wed Sep 3 05:48:56 2008 From: jboss-envers-commits at lists.jboss.org (jboss-envers-commits at lists.jboss.org) Date: Wed, 03 Sep 2008 05:48:56 -0400 Subject: [jboss-envers-commits] JBoss Envers SVN: r147 - in demos/seam: lib and 1 other directories. Message-ID: Author: adamw Date: 2008-09-03 05:48:55 -0400 (Wed, 03 Sep 2008) New Revision: 147 Added: demos/seam/lib/envers-1.1.0.beta1.jar demos/seam/lib/hibernate3.jar demos/seam/lib/lucene-core-2.3.0.jar Removed: demos/seam/lib/envers-1.0.0.ga.jar demos/seam/lib/hibernate.jar demos/seam/lib/lucene-core.jar Modified: demos/seam/build.xml demos/seam/lib/hibernate-annotations.jar demos/seam/lib/hibernate-entitymanager.jar demos/seam/lib/hibernate-search.jar demos/seam/src/action/org/jboss/envers/demo/seam/persons/PersonsAddresses.java demos/seam/src/action/org/jboss/envers/demo/seam/persons/Register.java Log: Updated demo Modified: demos/seam/build.xml =================================================================== --- demos/seam/build.xml 2008-09-03 08:41:02 UTC (rev 146) +++ demos/seam/build.xml 2008-09-03 09:48:55 UTC (rev 147) @@ -153,8 +153,8 @@ - + + Deleted: demos/seam/lib/envers-1.0.0.ga.jar =================================================================== (Binary files differ) Added: demos/seam/lib/envers-1.1.0.beta1.jar =================================================================== (Binary files differ) Property changes on: demos/seam/lib/envers-1.1.0.beta1.jar ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Modified: demos/seam/lib/hibernate-annotations.jar =================================================================== (Binary files differ) Modified: demos/seam/lib/hibernate-entitymanager.jar =================================================================== (Binary files differ) Modified: demos/seam/lib/hibernate-search.jar =================================================================== (Binary files differ) Deleted: demos/seam/lib/hibernate.jar =================================================================== (Binary files differ) Added: demos/seam/lib/hibernate3.jar =================================================================== (Binary files differ) Property changes on: demos/seam/lib/hibernate3.jar ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Added: demos/seam/lib/lucene-core-2.3.0.jar =================================================================== (Binary files differ) Property changes on: demos/seam/lib/lucene-core-2.3.0.jar ___________________________________________________________________ Name: svn:mime-type + application/octet-stream Deleted: demos/seam/lib/lucene-core.jar =================================================================== (Binary files differ) Modified: demos/seam/src/action/org/jboss/envers/demo/seam/persons/PersonsAddresses.java =================================================================== --- demos/seam/src/action/org/jboss/envers/demo/seam/persons/PersonsAddresses.java 2008-09-03 08:41:02 UTC (rev 146) +++ demos/seam/src/action/org/jboss/envers/demo/seam/persons/PersonsAddresses.java 2008-09-03 09:48:55 UTC (rev 147) @@ -4,7 +4,6 @@ import org.jboss.seam.annotations.Name; import org.jboss.envers.VersionsReader; import org.jboss.envers.query.VersionsRestrictions; -import org.hibernate.criterion.Order; import javax.persistence.NoResultException; import java.util.List; @@ -37,9 +36,9 @@ public List getRegistrations() { if (registrations == null) { registrations = versionsReader.createQuery() - .forRevisionsOfEntity(Registration.class, true) + .forRevisionsOfEntity(Registration.class, true, false) .add(VersionsRestrictions.relatedIdEq("person", person.getId())) - .addOrder(Order.asc("registrationDate")) + .addOrder("registrationDate", true) .getResultList(); } @@ -49,7 +48,7 @@ public void check() { try { regOnCheckDate = (Registration) versionsReader.createQuery() - .forRevisionsOfEntity(Registration.class, true) + .forRevisionsOfEntity(Registration.class, true, false) .add(VersionsRestrictions.maximizeProperty("actualDate") .add(VersionsRestrictions.relatedIdEq("person", person.getId())) .add(VersionsRestrictions.le("registrationDate", addressCheckDate)) @@ -61,7 +60,7 @@ try { regNow = (Registration) versionsReader.createQuery() - .forRevisionsOfEntity(Registration.class, true) + .forRevisionsOfEntity(Registration.class, true, false) .add(VersionsRestrictions.maximizeProperty("actualDate") .add(VersionsRestrictions.relatedIdEq("person", person.getId())) .add(VersionsRestrictions.le("actualDate", addressCheckDate))) Modified: demos/seam/src/action/org/jboss/envers/demo/seam/persons/Register.java =================================================================== --- demos/seam/src/action/org/jboss/envers/demo/seam/persons/Register.java 2008-09-03 08:41:02 UTC (rev 146) +++ demos/seam/src/action/org/jboss/envers/demo/seam/persons/Register.java 2008-09-03 09:48:55 UTC (rev 147) @@ -59,9 +59,9 @@ public boolean register() { // Checking if there is no registration with the same registration date already - int registrationsWithSameRegDate = (Integer) versionsReader.createQuery() - .forRevisionsOfEntity(Registration.class, false) - .setProjection(RevisionProperty.count()) + long registrationsWithSameRegDate = (Long) versionsReader.createQuery() + .forRevisionsOfEntity(Registration.class, false, false) + .addProjection(RevisionProperty.count()) .add(VersionsRestrictions.relatedIdEq("person", person.getId())) .add(VersionsRestrictions.eq("registrationDate", registrationDate)) .getSingleResult(); @@ -73,9 +73,9 @@ } // Checking if there is no registration with the same actual date already - int registrationsWithSameActDate = (Integer) versionsReader.createQuery() - .forRevisionsOfEntity(Registration.class, false) - .setProjection(RevisionProperty.count()) + long registrationsWithSameActDate = (Long) versionsReader.createQuery() + .forRevisionsOfEntity(Registration.class, false, false) + .addProjection(RevisionProperty.count()) .add(VersionsRestrictions.relatedIdEq("person", person.getId())) .add(VersionsRestrictions.eq("actualDate", actualDate)) .getSingleResult(); From jboss-envers-commits at lists.jboss.org Wed Sep 3 05:54:06 2008 From: jboss-envers-commits at lists.jboss.org (jboss-envers-commits at lists.jboss.org) Date: Wed, 03 Sep 2008 05:54:06 -0400 Subject: [jboss-envers-commits] JBoss Envers SVN: r148 - trunk/src/main/org/jboss/envers/query. Message-ID: Author: adamw Date: 2008-09-03 05:54:06 -0400 (Wed, 03 Sep 2008) New Revision: 148 Modified: trunk/src/main/org/jboss/envers/query/VersionsQueryCreator.java Log: Javadoc fix Modified: trunk/src/main/org/jboss/envers/query/VersionsQueryCreator.java =================================================================== --- trunk/src/main/org/jboss/envers/query/VersionsQueryCreator.java 2008-09-03 09:48:55 UTC (rev 147) +++ trunk/src/main/org/jboss/envers/query/VersionsQueryCreator.java 2008-09-03 09:54:06 UTC (rev 148) @@ -62,6 +62,7 @@ *
  7. revision entity, corresponding to the revision at which the entity was modified. If no custom * revision entity is used, this will be an instance of {@link org.jboss.envers.DefaultRevisionEntity}
  8. *
  9. type of the revision (an enum instance of class {@link org.jboss.envers.RevisionType})
  10. . + * * Additional conditions that the results must satisfy may be specified. * @param c Class of the entities for which to query. * @param selectEntitiesOnly If true, instead of a list of three-element arrays, a list of entites will be From jboss-envers-commits at lists.jboss.org Mon Sep 15 11:32:03 2008 From: jboss-envers-commits at lists.jboss.org (jboss-envers-commits at lists.jboss.org) Date: Mon, 15 Sep 2008 11:32:03 -0400 Subject: [jboss-envers-commits] JBoss Envers SVN: r149 - in trunk/src/main/org/jboss/envers: configuration/metadata and 6 other directories. Message-ID: Author: adamw Date: 2008-09-15 11:32:02 -0400 (Mon, 15 Sep 2008) New Revision: 149 Added: trunk/src/main/org/jboss/envers/configuration/metadata/BasicMetadataGenerator.java trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java trunk/src/main/org/jboss/envers/configuration/metadata/EntityXmlMappingData.java trunk/src/main/org/jboss/envers/configuration/metadata/IdMetadataGenerator.java trunk/src/main/org/jboss/envers/configuration/metadata/ToOneRelationMetadataGenerator.java trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/MapProxy.java Removed: trunk/src/main/org/jboss/envers/entities/mapper/CompositePropertyMapperBuilder.java Modified: trunk/src/main/org/jboss/envers/configuration/EntitiesConfigurator.java trunk/src/main/org/jboss/envers/configuration/GlobalConfiguration.java trunk/src/main/org/jboss/envers/configuration/RevisionInfoConfiguration.java trunk/src/main/org/jboss/envers/configuration/VersionsConfiguration.java trunk/src/main/org/jboss/envers/configuration/VersionsEntitiesConfiguration.java trunk/src/main/org/jboss/envers/configuration/metadata/MetadataTools.java trunk/src/main/org/jboss/envers/configuration/metadata/VersionsMetadataGenerator.java trunk/src/main/org/jboss/envers/entities/mapper/MapPropertyMapper.java trunk/src/main/org/jboss/envers/entities/mapper/MultiPropertyMapper.java trunk/src/main/org/jboss/envers/entities/mapper/PersistentCollectionChangeData.java trunk/src/main/org/jboss/envers/entities/mapper/PropertyMapper.java trunk/src/main/org/jboss/envers/entities/mapper/relation/AbstractOneToManyMapper.java trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyDetachedMapper.java trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/DetachedRelationInitializor.java trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/Initializor.java trunk/src/main/org/jboss/envers/query/criteria/CriteriaTools.java trunk/src/main/org/jboss/envers/tools/reflection/ReflectionTools.java Log: ENVERS-41: breaking down VersionsMetadaGenerator to smaller classes Modified: trunk/src/main/org/jboss/envers/configuration/EntitiesConfigurator.java =================================================================== --- trunk/src/main/org/jboss/envers/configuration/EntitiesConfigurator.java 2008-09-03 09:54:06 UTC (rev 148) +++ trunk/src/main/org/jboss/envers/configuration/EntitiesConfigurator.java 2008-09-15 15:32:02 UTC (rev 149) @@ -25,7 +25,7 @@ import org.jboss.envers.configuration.metadata.VersionsMetadataGenerator; import org.jboss.envers.configuration.metadata.PersistentClassVersioningData; import org.jboss.envers.configuration.metadata.AnnotationsMetadataReader; -import org.jboss.envers.configuration.metadata.EntityMappingData; +import org.jboss.envers.configuration.metadata.EntityXmlMappingData; import org.jboss.envers.tools.graph.GraphTopologicalSort; import org.jboss.envers.tools.reflection.YReflectionManager; import org.dom4j.io.DOMWriter; @@ -62,7 +62,7 @@ Map pcDatas = new HashMap(); - Map xmlMappings = new HashMap(); + Map xmlMappings = new HashMap(); // First pass AnnotationsMetadataReader annotationsMetadataReader = new AnnotationsMetadataReader(); @@ -79,25 +79,24 @@ verEntCfg.addCustomVersionsTableName(pc.getEntityName(), versioningData.versionsTableName); } - EntityMappingData mappingData = new EntityMappingData(); - versionsMetaGen.generateFirstPass(pc, versioningData, mappingData); - xmlMappings.put(pc, mappingData); + EntityXmlMappingData xmlMappingData = new EntityXmlMappingData(); + versionsMetaGen.generateFirstPass(pc, versioningData, xmlMappingData); + xmlMappings.put(pc, xmlMappingData); } } // Second pass for (PersistentClass pc : pcDatas.keySet()) { - EntityMappingData mappingData = xmlMappings.get(pc); + EntityXmlMappingData xmlMappingData = xmlMappings.get(pc); - versionsMetaGen.generateSecondPass(pc, pcDatas.get(pc), mappingData); + versionsMetaGen.generateSecondPass(pc, pcDatas.get(pc), xmlMappingData); - try { - cfg.addDocument(writer.write(mappingData.getMainMapping())); + cfg.addDocument(writer.write(xmlMappingData.getMainXmlMapping())); // TODO - //writeDocument(mappingData.getMainMapping()); + //writeDocument(xmlMappingData.getMainXmlMapping()); - for (Document additionalMapping : mappingData.getAdditionalMappings()) { + for (Document additionalMapping : xmlMappingData.getAdditionalXmlMappings()) { cfg.addDocument(writer.write(additionalMapping)); // TODO //writeDocument(additionalMapping); Modified: trunk/src/main/org/jboss/envers/configuration/GlobalConfiguration.java =================================================================== --- trunk/src/main/org/jboss/envers/configuration/GlobalConfiguration.java 2008-09-03 09:54:06 UTC (rev 148) +++ trunk/src/main/org/jboss/envers/configuration/GlobalConfiguration.java 2008-09-15 15:32:02 UTC (rev 149) @@ -64,5 +64,4 @@ public boolean isUnversionedOptimisticLockingField() { return unversionedOptimisticLockingField; } - } Modified: trunk/src/main/org/jboss/envers/configuration/RevisionInfoConfiguration.java =================================================================== --- trunk/src/main/org/jboss/envers/configuration/RevisionInfoConfiguration.java 2008-09-03 09:54:06 UTC (rev 148) +++ trunk/src/main/org/jboss/envers/configuration/RevisionInfoConfiguration.java 2008-09-15 15:32:02 UTC (rev 149) @@ -203,7 +203,7 @@ } return new RevisionInfoConfigurationResult( - revisionInfoGenerator, revisionPropType, revisionInfoXmlMapping, + revisionInfoGenerator, revisionInfoXmlMapping, new RevisionInfoQueryCreator(revisionInfoEntityName, revisionInfoIdName, revisionInfoTimestampName), generateRevisionInfoRelationMapping(), new RevisionInfoNumberReader(revisionInfoClass, revisionInfoIdName), revisionInfoEntityName); @@ -212,19 +212,17 @@ class RevisionInfoConfigurationResult { private final RevisionInfoGenerator revisionInfoGenerator; - private final String revisionPropType; private final Document revisionInfoXmlMapping; private final RevisionInfoQueryCreator revisionInfoQueryCreator; private final Element revisionInfoRelationMapping; private final RevisionInfoNumberReader revisionInfoNumberReader; private final String revisionInfoEntityName; - RevisionInfoConfigurationResult(RevisionInfoGenerator revisionInfoGenerator, String revisionPropType, + RevisionInfoConfigurationResult(RevisionInfoGenerator revisionInfoGenerator, Document revisionInfoXmlMapping, RevisionInfoQueryCreator revisionInfoQueryCreator, Element revisionInfoRelationMapping, RevisionInfoNumberReader revisionInfoNumberReader, String revisionInfoEntityName) { this.revisionInfoGenerator = revisionInfoGenerator; - this.revisionPropType = revisionPropType; this.revisionInfoXmlMapping = revisionInfoXmlMapping; this.revisionInfoQueryCreator = revisionInfoQueryCreator; this.revisionInfoRelationMapping = revisionInfoRelationMapping; @@ -236,10 +234,6 @@ return revisionInfoGenerator; } - public String getRevisionPropType() { - return revisionPropType; - } - public Document getRevisionInfoXmlMapping() { return revisionInfoXmlMapping; } Modified: trunk/src/main/org/jboss/envers/configuration/VersionsConfiguration.java =================================================================== --- trunk/src/main/org/jboss/envers/configuration/VersionsConfiguration.java 2008-09-03 09:54:06 UTC (rev 148) +++ trunk/src/main/org/jboss/envers/configuration/VersionsConfiguration.java 2008-09-15 15:32:02 UTC (rev 149) @@ -72,8 +72,7 @@ YReflectionManager reflectionManager = YReflectionManager.get(cfg); RevisionInfoConfiguration revInfoCfg = new RevisionInfoConfiguration(); RevisionInfoConfigurationResult revInfoCfgResult = revInfoCfg.configure(cfg, reflectionManager); - verEntCfg = new VersionsEntitiesConfiguration(properties, revInfoCfgResult.getRevisionPropType(), - revInfoCfgResult.getRevisionInfoEntityName()); + verEntCfg = new VersionsEntitiesConfiguration(properties, revInfoCfgResult.getRevisionInfoEntityName()); globalCfg = new GlobalConfiguration(properties); versionsSyncManager = new VersionsSyncManager(revInfoCfgResult.getRevisionInfoGenerator()); revisionInfoQueryCreator = revInfoCfgResult.getRevisionInfoQueryCreator(); Modified: trunk/src/main/org/jboss/envers/configuration/VersionsEntitiesConfiguration.java =================================================================== --- trunk/src/main/org/jboss/envers/configuration/VersionsEntitiesConfiguration.java 2008-09-03 09:54:06 UTC (rev 148) +++ trunk/src/main/org/jboss/envers/configuration/VersionsEntitiesConfiguration.java 2008-09-15 15:32:02 UTC (rev 149) @@ -34,11 +34,8 @@ private final String originalIdPropName; private final String revisionPropName; - private final String revisionPropType; private final String revisionPropPath; - private final String revisionEntityPath; - private final String revisionTypePropName; private final String revisionTypePropType; @@ -46,7 +43,7 @@ private final Map customVersionsTablesNames; - public VersionsEntitiesConfiguration(Properties properties, String revisionPropType, String revisionInfoEntityName) { + public VersionsEntitiesConfiguration(Properties properties, String revisionInfoEntityName) { this.revisionInfoEntityName = revisionInfoEntityName; versionsTablePrefix = properties.getProperty("org.jboss.envers.versionsTablePrefix", ""); @@ -55,15 +52,13 @@ originalIdPropName = "originalId"; revisionPropName = properties.getProperty("org.jboss.envers.revisionFieldName", "_revision"); - this.revisionPropType = revisionPropType; revisionTypePropName = properties.getProperty("org.jboss.envers.revisionTypeFieldName", "_revision_type"); revisionTypePropType = "byte"; customVersionsTablesNames = new HashMap(); - revisionEntityPath = originalIdPropName + "." + revisionPropName; - revisionPropPath = revisionEntityPath + ".id"; + revisionPropPath = originalIdPropName + "." + revisionPropName + ".id"; } public String getOriginalIdPropName() { @@ -78,10 +73,6 @@ return revisionPropPath; } - public String getRevisionPropType() { - return revisionPropType; - } - public String getRevisionTypePropName() { return revisionTypePropName; } @@ -90,10 +81,6 @@ return revisionTypePropType; } - public String getRevisionEntityPath() { - return revisionEntityPath; - } - public String getRevisionInfoEntityName() { return revisionInfoEntityName; } Added: trunk/src/main/org/jboss/envers/configuration/metadata/BasicMetadataGenerator.java =================================================================== --- trunk/src/main/org/jboss/envers/configuration/metadata/BasicMetadataGenerator.java (rev 0) +++ trunk/src/main/org/jboss/envers/configuration/metadata/BasicMetadataGenerator.java 2008-09-15 15:32:02 UTC (rev 149) @@ -0,0 +1,108 @@ +package org.jboss.envers.configuration.metadata; + +import org.dom4j.Element; +import org.hibernate.mapping.Value; +import org.hibernate.mapping.Column; +import org.hibernate.mapping.Component; +import org.hibernate.mapping.Property; +import org.hibernate.type.CustomType; +import org.hibernate.util.StringHelper; +import org.jboss.envers.entities.mapper.SimpleMapperBuilder; +import org.jboss.envers.entities.mapper.CompositeMapperBuilder; +import org.jboss.envers.ModificationStore; +import org.jboss.envers.exception.VersionsException; + +import java.util.Iterator; + +/** + * Generates metadata for basic properties: immutable types (including enums) and components + * @author Adam Warski (adam at warski dot org) + */ +public class BasicMetadataGenerator { + private final VersionsMetadataGenerator mainGenerator; + + BasicMetadataGenerator(VersionsMetadataGenerator versionsMetadataGenerator) { + mainGenerator = versionsMetadataGenerator; + } + + @SuppressWarnings({"unchecked"}) + void addSimpleValue(Element parent, String name, Value value, SimpleMapperBuilder mapper, + ModificationStore store, boolean key) { + Element prop_mapping = MetadataTools.addProperty(parent, name, + value.getType().getName(), key); + MetadataTools.addColumns(prop_mapping, (Iterator) value.getColumnIterator()); + + // A null mapper means that we only want to add xml mappings (while building the id mapping) + if (mapper != null) { + mapper.add(name, store); + } + } + + @SuppressWarnings({"unchecked"}) + void addEnumValue(Element parent, String name, Value value, SimpleMapperBuilder mapper, + ModificationStore store) { + Element prop_mapping = parent.addElement("property"); + prop_mapping.addAttribute("name", name); + + CustomType propertyType = (CustomType) value.getType(); + + Element type_mapping = prop_mapping.addElement("type"); + type_mapping.addAttribute("name", propertyType.getName()); + + Element type_param1 = type_mapping.addElement("param"); + type_param1.addAttribute("name", "enumClass"); + type_param1.setText(propertyType.getReturnedClass().getName()); + + Element type_param2 = type_mapping.addElement("param"); + type_param2.addAttribute("name", "type"); + type_param2.setText(Integer.toString(propertyType.sqlTypes(null)[0])); + + MetadataTools.addColumns(prop_mapping, (Iterator) value.getColumnIterator()); + + mapper.add(name, store); + } + + private void addComponentClassName(Element any_mapping, Component comp) { + if (StringHelper.isNotEmpty(comp.getComponentClassName())) { + any_mapping.addAttribute("class", comp.getComponentClassName()); + } + } + + @SuppressWarnings({"unchecked"}) + void addComponent(Element parent, String name, Value value, CompositeMapperBuilder mapper, + String entityName, EntityXmlMappingData xmlMappingData, + boolean firstPass) { + Element component_mapping = null; + Component prop_component = (Component) value; + + if (!firstPass) { + // The required element already exists. + Iterator iter = parent.elementIterator("component"); + while (iter.hasNext()) { + Element child = iter.next(); + if (child.attribute("name").getText().equals(name)) { + component_mapping = child; + break; + } + } + + if (component_mapping == null) { + throw new VersionsException("Element for component not found during second pass!"); + } + } else { + component_mapping = parent.addElement("component"); + component_mapping.addAttribute("name", name); + + addComponentClassName(component_mapping, prop_component); + } + + CompositeMapperBuilder componentMapper = mapper.addComposite(name); + + Iterator properties = (Iterator) prop_component.getPropertyIterator(); + while (properties.hasNext()) { + Property property = properties.next(); + mainGenerator.addValue(component_mapping, property.getName(), property.getValue(), + componentMapper, ModificationStore.FULL, entityName, xmlMappingData, firstPass); + } + } +} Added: trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java =================================================================== --- trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java (rev 0) +++ trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java 2008-09-15 15:32:02 UTC (rev 149) @@ -0,0 +1,204 @@ +package org.jboss.envers.configuration.metadata; + +import org.hibernate.mapping.*; +import org.hibernate.mapping.Collection; +import org.hibernate.MappingException; +import org.jboss.envers.entities.mapper.CompositeMapperBuilder; +import org.jboss.envers.entities.mapper.relation.*; +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; +import org.jboss.envers.tools.StringTools; +import org.dom4j.Element; + +import java.util.*; + +/** + * Generates metadata for collection-valued properties. + * @author Adam Warski (adam at warski dot org) + */ +public class CollectionMetadataGenerator { + private final VersionsMetadataGenerator mainGenerator; + + CollectionMetadataGenerator(VersionsMetadataGenerator versionsMetadataGenerator) { + mainGenerator = versionsMetadataGenerator; + } + + @SuppressWarnings({"unchecked"}) + private String getMappedBy(Collection collectionValue) { + Iterator assocClassProps = + ((OneToMany) collectionValue.getElement()).getAssociatedClass().getPropertyIterator(); + + while (assocClassProps.hasNext()) { + Property property = assocClassProps.next(); + + if (Tools.iteratorsContentEqual(property.getValue().getColumnIterator(), + collectionValue.getKey().getColumnIterator())) { + return property.getName(); + } + } + + return null; + } + + @SuppressWarnings({"unchecked"}) + void addOneToManyAttached(String name, Value value, CompositeMapperBuilder mapper, String entityName) { + Collection propertyValue = (Collection) value; + + String owningReferencePropertyName = getMappedBy(propertyValue); + if (owningReferencePropertyName == null) { + throw new MappingException("Unable to read the mapped by attribute for " + name); + } + + EntityConfiguration configuration = mainGenerator.getEntitiesConfigurations().get(entityName); + if (configuration == null) { + throw new MappingException("A versioned relation to a non-versioned entity " + entityName + "!"); + } + + IdMappingData referencingIdMapping = configuration.getIdMappingData(); + + String owningEntityName = ((OneToMany) propertyValue.getElement()).getReferencedEntityName(); + String lastPropertyPrefix = owningReferencePropertyName + "_"; + + // Generating the id mapper for the relation + IdMapper ownedIdMapper = referencingIdMapping.getIdMapper().prefixMappedProperties(lastPropertyPrefix); + + // Storing information about this relation + mainGenerator.getEntitiesConfigurations().get(entityName).addOneToManyAttachedRelation(name, owningReferencePropertyName, + owningEntityName, ownedIdMapper); + + // Adding mapper for the id + mapper.addComposite(name, new OneToManyAttachedMapper(owningReferencePropertyName, owningEntityName, + name)); + } + + private String getMiddleEntityName(String entityName, String referencedEntityName, ManyToOne mto) { + return entityName + "_" + referencedEntityName + "_" + mto.getTable().getName(); + } + + @SuppressWarnings({"unchecked"}) + void addOneToManyDetached(String name, Value value, CompositeMapperBuilder mapper, String entityName, + EntityXmlMappingData xmlMappingData) { + Collection propertyValue = (Collection) value; + ManyToOne mto = (ManyToOne) propertyValue.getElement(); + + String referencedEntityName = mto.getReferencedEntityName(); + + EntityConfiguration configuration = mainGenerator.getEntitiesConfigurations().get(entityName); + if (configuration == null) { + throw new MappingException("A versioned relation to a non-versioned entity " + entityName + "!"); + } + + EntityConfiguration referencedConfiguration = mainGenerator.getEntitiesConfigurations().get(referencedEntityName); + if (referencedConfiguration == null) { + throw new MappingException("A versioned relation to a non-versioned entity " + referencedEntityName + "!"); + } + + IdMappingData referencingIdMapping = configuration.getIdMappingData(); + IdMappingData referencedIdMapping = referencedConfiguration.getIdMappingData(); + + String referencingPrefix = StringTools.getLastComponent(entityName) + "_"; + String referencedPrefix = name + "_"; + + // Name of the entity that will be used to store the relation between the two entities. + String middleEntityName = getMiddleEntityName(entityName, referencedEntityName, mto); + String versionsMiddleEntityName = mainGenerator.getVerEntCfg().getVersionsEntityName(middleEntityName); + String versionsMiddleTableName = mainGenerator.getVerEntCfg().getVersionsTableName(middleEntityName, mto.getTable().getName()); + + Element middleEntity = MetadataTools.createEntity(xmlMappingData.newAdditionalMapping(), versionsMiddleEntityName, + versionsMiddleTableName, mto.getTable().getSchema(), mto.getTable().getCatalog(), null); + + Element middleEntityId = middleEntity.addElement("composite-id"); + middleEntityId.addAttribute("name", mainGenerator.getVerEntCfg().getOriginalIdPropName()); + + Iterator columnIterator = mto.getTable().getColumnIterator(); + + // Adding elements to the mapping corresponding to the referencing entity id's + Element properties = (Element) referencingIdMapping.getXmlRelationMapping().clone(); + MetadataTools.prefixNamesInPropertyElement(properties, referencingPrefix, columnIterator, true); + for (Element idProperty : (java.util.List) properties.elements()) { + middleEntityId.add((Element) idProperty.clone()); + } + + // Adding elements to the mapping corresponding to the referenced entity id's + properties = (Element) referencedIdMapping.getXmlRelationMapping().clone(); + MetadataTools.prefixNamesInPropertyElement(properties, referencedPrefix, columnIterator, true); + for (Element idProperty : (java.util.List) properties.elements()) { + middleEntityId.add((Element) idProperty.clone()); + } + + mainGenerator.addRevisionInfoRelation(middleEntityId); + mainGenerator.addRevisionType(middleEntity); + + mainGenerator.getEntitiesConfigurations().get(entityName).addOneToManyDetachedRelation(name, referencedEntityName); + + // Adding the property mapper + mapper.addComposite(name, new OneToManyDetachedMapper(mainGenerator.getVerEntCfg(), entityName, + referencedEntityName, name, mainGenerator.getVerEntCfg().getVersionsEntityName(referencedEntityName), + versionsMiddleEntityName, referencingIdMapping.getIdMapper().prefixMappedProperties(referencingPrefix), + referencedIdMapping.getIdMapper().prefixMappedProperties(referencedPrefix), + mainGenerator.getEntitiesConfigurations().get(referencedEntityName).getIdMapper())); + } + + @SuppressWarnings({"unchecked"}) + private String getMappedBy(Table collectionTable, PersistentClass referencedClass) { + Iterator properties = referencedClass.getPropertyIterator(); + while (properties.hasNext()) { + Property property = properties.next(); + if (property.getValue() instanceof Collection) { + // The equality is intentional. We want to find a collection property with the same collection table. + //noinspection ObjectEquality + if (((Collection) property.getValue()).getCollectionTable() == collectionTable) { + return property.getName(); + } + } + } + + return null; + } + + void addManyToManyNotOwning(String name, Value value, CompositeMapperBuilder mapper, String entityName) { + Collection propertyValue = (Collection) value; + ManyToOne mto = (ManyToOne) propertyValue.getElement(); + + String referencedEntityName = mto.getReferencedEntityName(); + + EntityConfiguration configuration = mainGenerator.getEntitiesConfigurations().get(entityName); + if (configuration == null) { + throw new MappingException("A versioned relation to a non-versioned entity " + entityName + "!"); + } + + EntityConfiguration referencedConfiguration = mainGenerator.getEntitiesConfigurations().get(referencedEntityName); + if (referencedConfiguration == null) { + throw new MappingException("A versioned relation to a non-versioned entity " + referencedEntityName + "!"); + } + + String mappedBy = getMappedBy(propertyValue.getCollectionTable(), mainGenerator.getCfg().getClassMapping(referencedEntityName)); + if (mappedBy == null) { + throw new MappingException("Unable to read the mapped by attribute for " + name); + } + + IdMappingData referencingIdMapping = configuration.getIdMappingData(); + IdMappingData referencedIdMapping = referencedConfiguration.getIdMappingData(); + + String referencingPrefix = mappedBy + "_"; + String referencedPrefix = StringTools.getLastComponent(referencedEntityName) + "_"; + + // Name of the entity that will be used to store the relation between the two entities. + // The order is inverse, as the referenced and referencing entity names are swapped with respect to the + // name of the entity that was created on the owning side in addOneToManyDetached. + String middleEntityName = getMiddleEntityName(referencedEntityName, entityName, mto); + String versionsMiddleEntityName = mainGenerator.getVerEntCfg().getVersionsEntityName(middleEntityName); + + mainGenerator.getEntitiesConfigurations().get(entityName).addManyToManyNotOwningRelation(name, mappedBy, + referencedEntityName); + + // Adding the property mapper + mapper.addComposite(name, new ManyToManyNotOwningMapper(mainGenerator.getVerEntCfg(), entityName, + referencedEntityName, name, mainGenerator.getVerEntCfg().getVersionsEntityName(referencedEntityName), + versionsMiddleEntityName, referencingIdMapping.getIdMapper().prefixMappedProperties(referencingPrefix), + referencedIdMapping.getIdMapper().prefixMappedProperties(referencedPrefix), + mainGenerator.getEntitiesConfigurations().get(referencedEntityName).getIdMapper())); + } +} Copied: trunk/src/main/org/jboss/envers/configuration/metadata/EntityXmlMappingData.java (from rev 148, trunk/src/main/org/jboss/envers/configuration/metadata/EntityMappingData.java) =================================================================== --- trunk/src/main/org/jboss/envers/configuration/metadata/EntityXmlMappingData.java (rev 0) +++ trunk/src/main/org/jboss/envers/configuration/metadata/EntityXmlMappingData.java 2008-09-15 15:32:02 UTC (rev 149) @@ -0,0 +1,58 @@ +/* + * JBoss, Home of Professional Open Source + * + * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated + * by the @authors tag. All rights reserved. + * + * See the copyright.txt in the distribution for a full listing of individual + * contributors. 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.configuration.metadata; + +import org.dom4j.Document; +import org.dom4j.DocumentHelper; + +import java.util.List; +import java.util.ArrayList; + +/** + * @author Adam Warski (adam at warski dot org) + */ +public class EntityXmlMappingData { + private Document mainXmlMapping; + private List additionalXmlMappings; + + public EntityXmlMappingData() { + mainXmlMapping = DocumentHelper.createDocument(); + additionalXmlMappings = new ArrayList(); + } + + public Document getMainXmlMapping() { + return mainXmlMapping; + } + + public List getAdditionalXmlMappings() { + return additionalXmlMappings; + } + + public Document newAdditionalMapping() { + Document additionalMapping = DocumentHelper.createDocument(); + additionalXmlMappings.add(additionalMapping); + + return additionalMapping; + } +} Added: trunk/src/main/org/jboss/envers/configuration/metadata/IdMetadataGenerator.java =================================================================== --- trunk/src/main/org/jboss/envers/configuration/metadata/IdMetadataGenerator.java (rev 0) +++ trunk/src/main/org/jboss/envers/configuration/metadata/IdMetadataGenerator.java 2008-09-15 15:32:02 UTC (rev 149) @@ -0,0 +1,91 @@ +package org.jboss.envers.configuration.metadata; + +import org.dom4j.Element; +import org.dom4j.tree.DefaultElement; +import org.hibernate.mapping.Property; +import org.hibernate.mapping.PersistentClass; +import org.hibernate.mapping.Component; +import org.hibernate.type.Type; +import org.hibernate.type.ImmutableType; +import org.hibernate.MappingException; +import org.jboss.envers.entities.mapper.SimpleMapperBuilder; +import org.jboss.envers.entities.mapper.id.SimpleIdMapperBuilder; +import org.jboss.envers.entities.mapper.id.MultipleIdMapper; +import org.jboss.envers.entities.mapper.id.EmbeddedIdMapper; +import org.jboss.envers.entities.mapper.id.SingleIdMapper; +import org.jboss.envers.entities.IdMappingData; +import org.jboss.envers.ModificationStore; + +import java.util.Iterator; + +/** + * Generates metadata for primary identifiers (ids) of versions entities. + * @author Adam Warski (adam at warski dot org) + */ +public class IdMetadataGenerator { + private final VersionsMetadataGenerator mainGenerator; + + IdMetadataGenerator(VersionsMetadataGenerator versionsMetadataGenerator) { + mainGenerator = versionsMetadataGenerator; + } + + @SuppressWarnings({"unchecked"}) + private void addIdProperties(Element parent, Iterator properties, SimpleMapperBuilder mapper, boolean key) { + while (properties.hasNext()) { + Property property = properties.next(); + Type propertyType = property.getType(); + if (!"_identifierMapper".equals(property.getName())) { + if (propertyType instanceof ImmutableType) { + mainGenerator.getBasicMetadataGenerator().addSimpleValue(parent, property.getName(), + property.getValue(), mapper, ModificationStore.FULL, key); + } else { + throw new MappingException("Type not supported: " + propertyType.getClass().getName()); + } + } + } + } + + @SuppressWarnings({"unchecked"}) + IdMappingData addId(PersistentClass pc) { + // Xml mapping which will be used for relations + Element rel_id_mapping = new DefaultElement("properties"); + // Xml mapping which will be used for the primary key of the versions table + Element orig_id_mapping = new DefaultElement("composite-id"); + + Property id_prop = pc.getIdentifierProperty(); + Component id_mapper = pc.getIdentifierMapper(); + + SimpleIdMapperBuilder mapper; + if (id_mapper != null) { + mapper = new MultipleIdMapper(((Component) pc.getIdentifier()).getComponentClassName()); + addIdProperties(rel_id_mapping, (Iterator) id_mapper.getPropertyIterator(), mapper, false); + + // null mapper - the mapping where already added the first time, now we only want to generate the xml + addIdProperties(orig_id_mapping, (Iterator) id_mapper.getPropertyIterator(), null, true); + } else if (id_prop.isComposite()) { + Component id_component = (Component) id_prop.getValue(); + + mapper = new EmbeddedIdMapper(id_prop.getName(), id_component.getComponentClassName()); + addIdProperties(rel_id_mapping, (Iterator) id_component.getPropertyIterator(), mapper, false); + + // null mapper - the mapping where already added the first time, now we only want to generate the xml + addIdProperties(orig_id_mapping, (Iterator) id_component.getPropertyIterator(), null, true); + } else { + mapper = new SingleIdMapper(); + + mainGenerator.getBasicMetadataGenerator().addSimpleValue(rel_id_mapping, id_prop.getName(), + id_prop.getValue(), mapper, ModificationStore.FULL, false); + + // null mapper - the mapping where already added the first time, now we only want to generate the xml + mainGenerator.getBasicMetadataGenerator().addSimpleValue(orig_id_mapping, id_prop.getName(), + id_prop.getValue(), null, ModificationStore.FULL, true); + } + + orig_id_mapping.addAttribute("name", mainGenerator.getVerEntCfg().getOriginalIdPropName()); + + // Adding a relation to the revision entity (effectively: the "revision number" property) + mainGenerator.addRevisionInfoRelation(orig_id_mapping); + + return new IdMappingData(mapper, orig_id_mapping, rel_id_mapping); + } +} Modified: trunk/src/main/org/jboss/envers/configuration/metadata/MetadataTools.java =================================================================== --- trunk/src/main/org/jboss/envers/configuration/metadata/MetadataTools.java 2008-09-03 09:54:06 UTC (rev 148) +++ trunk/src/main/org/jboss/envers/configuration/metadata/MetadataTools.java 2008-09-15 15:32:02 UTC (rev 149) @@ -23,8 +23,12 @@ import org.dom4j.Element; import org.dom4j.Document; +import org.dom4j.Attribute; import org.jboss.envers.tools.StringTools; +import org.hibernate.mapping.Column; +import java.util.Iterator; + /** * @author Adam Warski (adam at warski dot org) */ @@ -126,4 +130,48 @@ return join_mapping; } + + public static void addColumns(Element any_mapping, Iterator columns) { + while (columns.hasNext()) { + Column column = columns.next(); + addColumn(any_mapping, column.getName(), column.getLength()); + } + } + + @SuppressWarnings({"unchecked"}) + private static void changeNamesInColumnElement(Element element, Iterator columnIterator) { + Iterator properties = element.elementIterator(); + while (properties.hasNext()) { + Element property = properties.next(); + + if ("column".equals(property.getName())) { + Attribute nameAttr = property.attribute("name"); + if (nameAttr != null) { + nameAttr.setText(columnIterator.next().getName()); + } + } + } + } + + @SuppressWarnings({"unchecked"}) + public static void prefixNamesInPropertyElement(Element element, String prefix, Iterator columnIterator, + boolean changeToKey) { + Iterator properties = element.elementIterator(); + while (properties.hasNext()) { + Element property = properties.next(); + + if ("property".equals(property.getName())) { + Attribute nameAttr = property.attribute("name"); + if (nameAttr != null) { + nameAttr.setText(prefix + nameAttr.getText()); + } + + changeNamesInColumnElement(property, columnIterator); + + if (changeToKey) { + property.setName("key-property"); + } + } + } + } } Added: trunk/src/main/org/jboss/envers/configuration/metadata/ToOneRelationMetadataGenerator.java =================================================================== --- trunk/src/main/org/jboss/envers/configuration/metadata/ToOneRelationMetadataGenerator.java (rev 0) +++ trunk/src/main/org/jboss/envers/configuration/metadata/ToOneRelationMetadataGenerator.java 2008-09-15 15:32:02 UTC (rev 149) @@ -0,0 +1,87 @@ +package org.jboss.envers.configuration.metadata; + +import org.dom4j.Element; +import org.hibernate.mapping.Value; +import org.hibernate.mapping.ToOne; +import org.hibernate.mapping.OneToOne; +import org.hibernate.MappingException; +import org.jboss.envers.entities.mapper.CompositeMapperBuilder; +import org.jboss.envers.entities.mapper.relation.ToOneIdMapper; +import org.jboss.envers.entities.mapper.relation.OneToOneNotOwningIdMapper; +import org.jboss.envers.entities.mapper.id.IdMapper; +import org.jboss.envers.entities.EntityConfiguration; +import org.jboss.envers.entities.IdMappingData; + +/** + * Generates metadata for to-one relations (reference-valued properties). + * @author Adam Warski (adam at warski dot org) + */ +public class ToOneRelationMetadataGenerator { + private final VersionsMetadataGenerator mainGenerator; + + ToOneRelationMetadataGenerator(VersionsMetadataGenerator versionsMetadataGenerator) { + mainGenerator = versionsMetadataGenerator; + } + + @SuppressWarnings({"unchecked"}) + void addToOne(Element parent, String name, Value value, CompositeMapperBuilder mapper, String entityName) { + String referencedEntityName = ((ToOne) value).getReferencedEntityName(); + + EntityConfiguration configuration = mainGenerator.getEntitiesConfigurations().get(referencedEntityName); + if (configuration == null) { + throw new MappingException("A versioned relation to a non-versioned entity " + referencedEntityName + "!"); + } + + IdMappingData idMapping = configuration.getIdMappingData(); + + String lastPropertyPrefix = name + "_"; + + // Generating the id mapper for the relation + IdMapper relMapper = idMapping.getIdMapper().prefixMappedProperties(lastPropertyPrefix); + + // Storing information about this relation + mainGenerator.getEntitiesConfigurations().get(entityName).addToOneRelation(name, referencedEntityName, relMapper); + + // Adding an element to the mapping corresponding to the references entity id's + Element properties = (Element) idMapping.getXmlRelationMapping().clone(); + properties.addAttribute("name", name); + + MetadataTools.prefixNamesInPropertyElement(properties, lastPropertyPrefix, value.getColumnIterator(), false); + parent.add(properties); + + // Adding mapper for the id + mapper.addComposite(name, new ToOneIdMapper(relMapper, name, referencedEntityName)); + } + + @SuppressWarnings({"unchecked"}) + void addOneToOneNotOwning(String name, Value value, CompositeMapperBuilder mapper, String entityName) { + OneToOne propertyValue = (OneToOne) value; + + String owningReferencePropertyName = propertyValue.getReferencedPropertyName(); // mappedBy + + EntityConfiguration configuration = mainGenerator.getEntitiesConfigurations().get(entityName); + if (configuration == null) { + throw new MappingException("A versioned relation to a non-versioned entity " + entityName + "!"); + } + + IdMappingData ownedIdMapping = configuration.getIdMappingData(); + + if (ownedIdMapping == null) { + throw new MappingException("A versioned relation to a non-versioned entity " + entityName + "!"); + } + + String lastPropertyPrefix = owningReferencePropertyName + "_"; + String referencedEntityName = propertyValue.getReferencedEntityName(); + + // Generating the id mapper for the relation + IdMapper ownedIdMapper = ownedIdMapping.getIdMapper().prefixMappedProperties(lastPropertyPrefix); + + // Storing information about this relation + mainGenerator.getEntitiesConfigurations().get(entityName).addOneToOneNotOwningRelation(name, owningReferencePropertyName, + referencedEntityName, ownedIdMapper); + + // Adding mapper for the id + mapper.addComposite(name, new OneToOneNotOwningIdMapper(owningReferencePropertyName, + referencedEntityName, name)); + } +} Modified: trunk/src/main/org/jboss/envers/configuration/metadata/VersionsMetadataGenerator.java =================================================================== --- trunk/src/main/org/jboss/envers/configuration/metadata/VersionsMetadataGenerator.java 2008-09-03 09:54:06 UTC (rev 148) +++ trunk/src/main/org/jboss/envers/configuration/metadata/VersionsMetadataGenerator.java 2008-09-15 15:32:02 UTC (rev 149) @@ -22,28 +22,21 @@ package org.jboss.envers.configuration.metadata; import org.hibernate.type.*; -import org.hibernate.util.StringHelper; import org.hibernate.MappingException; import org.hibernate.cfg.Configuration; import org.hibernate.mapping.*; import org.hibernate.mapping.Collection; import org.dom4j.Element; -import org.dom4j.Attribute; -import org.dom4j.tree.DefaultElement; import org.jboss.envers.entities.EntityConfiguration; import org.jboss.envers.entities.IdMappingData; import org.jboss.envers.configuration.GlobalConfiguration; import org.jboss.envers.configuration.VersionsEntitiesConfiguration; import org.jboss.envers.entities.mapper.*; -import org.jboss.envers.entities.mapper.id.*; -import org.jboss.envers.entities.mapper.relation.*; import org.jboss.envers.tools.StringTools; -import org.jboss.envers.tools.Tools; import org.jboss.envers.tools.HibernateVersion; import org.jboss.envers.ModificationStore; import org.jboss.envers.tools.log.YLog; import org.jboss.envers.tools.log.YLogManager; -import org.jboss.envers.exception.VersionsException; import java.util.*; import java.util.List; @@ -54,13 +47,16 @@ * @author Sebastian Komander */ public class VersionsMetadataGenerator { - private final static Map EMPTY_STORE = Collections.emptyMap(); - private final Configuration cfg; private final GlobalConfiguration globalCfg; private final VersionsEntitiesConfiguration verEntCfg; private final Element revisionInfoRelationMapping; + private final BasicMetadataGenerator basicMetadataGenerator; + private final IdMetadataGenerator idMetadataGenerator; + private final CollectionMetadataGenerator collectionMetadataGenerator; + private final ToOneRelationMetadataGenerator toOneRelationMetadataGenerator; + private Map entitiesConfigurations; // Map entity name -> (join descriptor -> element describing the "versioned" join) @@ -76,24 +72,16 @@ this.verEntCfg = verEntCfg; this.revisionInfoRelationMapping = revisionInfoRelationMapping; + this.basicMetadataGenerator = new BasicMetadataGenerator(this); + this.idMetadataGenerator = new IdMetadataGenerator(this); + this.collectionMetadataGenerator = new CollectionMetadataGenerator(this); + this.toOneRelationMetadataGenerator = new ToOneRelationMetadataGenerator(this); + entitiesConfigurations = new HashMap(); entitiesJoins = new HashMap>(); } - private void addComponentClassName(Element any_mapping, Component comp) { - if (StringHelper.isNotEmpty(comp.getComponentClassName())) { - any_mapping.addAttribute("class", comp.getComponentClassName()); - } - } - - private void addColumns(Element any_mapping, Iterator columns) { - while (columns.hasNext()) { - Column column = columns.next(); - MetadataTools.addColumn(any_mapping, column.getName(), column.getLength()); - } - } - - private void addRevisionInfoRelation(Element any_mapping) { + void addRevisionInfoRelation(Element any_mapping) { Element rev_mapping = (Element) revisionInfoRelationMapping.clone(); rev_mapping.addAttribute("name", verEntCfg.getRevisionPropName()); MetadataTools.addColumn(rev_mapping, verEntCfg.getRevisionPropName(), null); @@ -101,361 +89,12 @@ any_mapping.add(rev_mapping); } - private void addRevisionType(Element any_mapping) { + void addRevisionType(Element any_mapping) { Element revTypeProperty = MetadataTools.addProperty(any_mapping, verEntCfg.getRevisionTypePropName(), verEntCfg.getRevisionTypePropType(), false); revTypeProperty.addAttribute("type", "org.jboss.envers.entities.RevisionTypeType"); } - @SuppressWarnings({"unchecked"}) - private void addSimpleProperty(Element parent, Property property, SimpleMapperBuilder mapper, - ModificationStore store, boolean key) { - Element prop_mapping = MetadataTools.addProperty(parent, property.getName(), - property.getType().getName(), key); - addColumns(prop_mapping, (Iterator) property.getColumnIterator()); - - // A null mapper means that we only want to add xml mappings (while building the id mapping) - if (mapper != null) { - mapper.add(property.getName(), store); - } - } - - @SuppressWarnings({"unchecked"}) - private void addEnumProperty(Element parent, Property property, SimpleMapperBuilder mapper, - ModificationStore store) { - Element prop_mapping = parent.addElement("property"); - prop_mapping.addAttribute("name", property.getName()); - - CustomType propertyType = (CustomType) property.getType(); - - Element type_mapping = prop_mapping.addElement("type"); - type_mapping.addAttribute("name", propertyType.getName()); - - Element type_param1 = type_mapping.addElement("param"); - type_param1.addAttribute("name", "enumClass"); - type_param1.setText(propertyType.getReturnedClass().getName()); - - Element type_param2 = type_mapping.addElement("param"); - type_param2.addAttribute("name", "type"); - type_param2.setText(Integer.toString(propertyType.sqlTypes(null)[0])); - - addColumns(prop_mapping, (Iterator) property.getColumnIterator()); - - mapper.add(property.getName(), store); - } - - @SuppressWarnings({"unchecked"}) - private void addComponent(Element parent, Property property, CompositeMapperBuilder mapper, ModificationStore store, - String entityName, EntityMappingData mappingData, List unversionedProperties, - boolean firstPass) { - Element component_mapping = null; - Component prop_component = (Component) property.getValue(); - - if (!firstPass) { - // The required element already exists. - Iterator iter = parent.elementIterator("component"); - while (iter.hasNext()) { - Element child = iter.next(); - if (child.attribute("name").getText().equals(property.getName())) { - component_mapping = child; - break; - } - } - - if (component_mapping == null) { - throw new VersionsException("Element for component not found during second pass!"); - } - } else { - component_mapping = parent.addElement("component"); - component_mapping.addAttribute("name", property.getName()); - - addComponentClassName(component_mapping, prop_component); - } - - addProperties(component_mapping, (Iterator) prop_component.getPropertyIterator(), - mapper.addComposite(property.getName()), new PropertyStoreInfo(store, EMPTY_STORE), entityName, - mappingData, unversionedProperties, firstPass); - } - - @SuppressWarnings({"unchecked"}) - private void changeNamesInColumnElement(Element element, Iterator columnIterator) { - Iterator properties = element.elementIterator(); - while (properties.hasNext()) { - Element property = properties.next(); - - if ("column".equals(property.getName())) { - Attribute nameAttr = property.attribute("name"); - if (nameAttr != null) { - nameAttr.setText(columnIterator.next().getName()); - } - } - } - } - - @SuppressWarnings({"unchecked"}) - private void prefixNamesInPropertyElement(Element element, String prefix, Iterator columnIterator, - boolean changeToKey) { - Iterator properties = element.elementIterator(); - while (properties.hasNext()) { - Element property = properties.next(); - - if ("property".equals(property.getName())) { - Attribute nameAttr = property.attribute("name"); - if (nameAttr != null) { - nameAttr.setText(prefix + nameAttr.getText()); - } - - changeNamesInColumnElement(property, columnIterator); - - if (changeToKey) { - property.setName("key-property"); - } - } - } - } - - @SuppressWarnings({"unchecked"}) - private void addToOne(Element parent, Property property, CompositeMapperBuilder mapper, String entityName) { - String referencedEntityName = ((ToOne) property.getValue()).getReferencedEntityName(); - - EntityConfiguration configuration = entitiesConfigurations.get(referencedEntityName); - if (configuration == null) { - throw new MappingException("A versioned relation to a non-versioned entity " + referencedEntityName + "!"); - } - - IdMappingData idMapping = configuration.getIdMappingData(); - - String propertyName = property.getName(); - String lastPropertyPrefix = propertyName + "_"; - - // Generating the id mapper for the relation - IdMapper relMapper = idMapping.getIdMapper().prefixMappedProperties(lastPropertyPrefix); - - // Storing information about this relation - entitiesConfigurations.get(entityName).addToOneRelation(propertyName, referencedEntityName, relMapper); - - // Adding an element to the mapping corresponding to the references entity id's - Element properties = (Element) idMapping.getXmlRelationMapping().clone(); - properties.addAttribute("name", propertyName); - - prefixNamesInPropertyElement(properties, lastPropertyPrefix, property.getValue().getColumnIterator(), false); - parent.add(properties); - - // Adding mapper for the id - mapper.addComposite(propertyName, new ToOneIdMapper(relMapper, propertyName, referencedEntityName)); - } - - @SuppressWarnings({"unchecked"}) - private void addOneToOneNotOwning(Property property, CompositeMapperBuilder mapper, String entityName) { - OneToOne propertyValue = (OneToOne) property.getValue(); - - String owningReferencePropertyName = propertyValue.getReferencedPropertyName(); // mappedBy - - EntityConfiguration configuration = entitiesConfigurations.get(entityName); - if (configuration == null) { - throw new MappingException("A versioned relation to a non-versioned entity " + entityName + "!"); - } - - IdMappingData ownedIdMapping = configuration.getIdMappingData(); - - if (ownedIdMapping == null) { - throw new MappingException("A versioned relation to a non-versioned entity " + entityName + "!"); - } - - String propertyName = property.getName(); - String lastPropertyPrefix = owningReferencePropertyName + "_"; - String referencedEntityName = propertyValue.getReferencedEntityName(); - - // Generating the id mapper for the relation - IdMapper ownedIdMapper = ownedIdMapping.getIdMapper().prefixMappedProperties(lastPropertyPrefix); - - // Storing information about this relation - entitiesConfigurations.get(entityName).addOneToOneNotOwningRelation(propertyName, owningReferencePropertyName, - referencedEntityName, ownedIdMapper); - - // Adding mapper for the id - mapper.addComposite(propertyName, new OneToOneNotOwningIdMapper(owningReferencePropertyName, - referencedEntityName, propertyName)); - } - - @SuppressWarnings({"unchecked"}) - private String getMappedBy(Collection collectionValue) { - Iterator assocClassProps = - ((OneToMany) collectionValue.getElement()).getAssociatedClass().getPropertyIterator(); - - while (assocClassProps.hasNext()) { - Property property = assocClassProps.next(); - - if (Tools.iteratorsContentEqual(property.getValue().getColumnIterator(), - collectionValue.getKey().getColumnIterator())) { - return property.getName(); - } - } - - return null; - } - - @SuppressWarnings({"unchecked"}) - private void addOneToManyAttached(Property property, CompositeMapperBuilder mapper, String entityName) { - Collection propertyValue = (Collection) property.getValue(); - - String owningReferencePropertyName = getMappedBy(propertyValue); - if (owningReferencePropertyName == null) { - throw new MappingException("Unable to read the mapped by attribute for " + property.getName()); - } - - EntityConfiguration configuration = entitiesConfigurations.get(entityName); - if (configuration == null) { - throw new MappingException("A versioned relation to a non-versioned entity " + entityName + "!"); - } - - IdMappingData referencingIdMapping = configuration.getIdMappingData(); - - String owningEntityName = ((OneToMany) propertyValue.getElement()).getReferencedEntityName(); - String propertyName = property.getName(); - String lastPropertyPrefix = owningReferencePropertyName + "_"; - - // Generating the id mapper for the relation - IdMapper ownedIdMapper = referencingIdMapping.getIdMapper().prefixMappedProperties(lastPropertyPrefix); - - // Storing information about this relation - entitiesConfigurations.get(entityName).addOneToManyAttachedRelation(propertyName, owningReferencePropertyName, - owningEntityName, ownedIdMapper); - - // Adding mapper for the id - mapper.addComposite(propertyName, new OneToManyAttachedMapper(owningReferencePropertyName, owningEntityName, - propertyName)); - } - - private String getMiddleEntityName(String entityName, String referencedEntityName, ManyToOne mto) { - return entityName + "_" + referencedEntityName + "_" + mto.getTable().getName(); - } - - @SuppressWarnings({"unchecked"}) - private void addOneToManyDetached(Property property, CompositeMapperBuilder mapper, String entityName, - EntityMappingData mappingData) { - Collection propertyValue = (Collection) property.getValue(); - ManyToOne mto = (ManyToOne) propertyValue.getElement(); - - String referencedEntityName = mto.getReferencedEntityName(); - - EntityConfiguration configuration = entitiesConfigurations.get(entityName); - if (configuration == null) { - throw new MappingException("A versioned relation to a non-versioned entity " + entityName + "!"); - } - - EntityConfiguration referencedConfiguration = entitiesConfigurations.get(referencedEntityName); - if (referencedConfiguration == null) { - throw new MappingException("A versioned relation to a non-versioned entity " + referencedEntityName + "!"); - } - - IdMappingData referencingIdMapping = configuration.getIdMappingData(); - IdMappingData referencedIdMapping = referencedConfiguration.getIdMappingData(); - - String referencingPrefix = StringTools.getLastComponent(entityName) + "_"; - String referencedPrefix = property.getName() + "_"; - - // Name of the entity that will be used to store the relation between the two entities. - String middleEntityName = getMiddleEntityName(entityName, referencedEntityName, mto); - String versionsMiddleEntityName = verEntCfg.getVersionsEntityName(middleEntityName); - String versionsMiddleTableName = verEntCfg.getVersionsTableName(middleEntityName, mto.getTable().getName()); - - Element middleEntity = MetadataTools.createEntity(mappingData.newAdditionalMapping(), versionsMiddleEntityName, - versionsMiddleTableName, mto.getTable().getSchema(), mto.getTable().getCatalog(), null); - - Element middleEntityId = middleEntity.addElement("composite-id"); - middleEntityId.addAttribute("name", verEntCfg.getOriginalIdPropName()); - - Iterator columnIterator = mto.getTable().getColumnIterator(); - - // Adding elements to the mapping corresponding to the referencing entity id's - Element properties = (Element) referencingIdMapping.getXmlRelationMapping().clone(); - prefixNamesInPropertyElement(properties, referencingPrefix, columnIterator, true); - for (Element idProperty : (List) properties.elements()) { - middleEntityId.add((Element) idProperty.clone()); - } - - // Adding elements to the mapping corresponding to the referenced entity id's - properties = (Element) referencedIdMapping.getXmlRelationMapping().clone(); - prefixNamesInPropertyElement(properties, referencedPrefix, columnIterator, true); - for (Element idProperty : (List) properties.elements()) { - middleEntityId.add((Element) idProperty.clone()); - } - - addRevisionInfoRelation(middleEntityId); - addRevisionType(middleEntity); - - entitiesConfigurations.get(entityName).addOneToManyDetachedRelation(property.getName(), referencedEntityName); - - // Adding the property mapper - mapper.addComposite(property.getName(), new OneToManyDetachedMapper(verEntCfg, entityName, - referencedEntityName, property.getName(), verEntCfg.getVersionsEntityName(referencedEntityName), - versionsMiddleEntityName, referencingIdMapping.getIdMapper().prefixMappedProperties(referencingPrefix), - referencedIdMapping.getIdMapper().prefixMappedProperties(referencedPrefix), - entitiesConfigurations.get(referencedEntityName).getIdMapper())); - } - - @SuppressWarnings({"unchecked"}) - private String getMappedBy(Table collectionTable, PersistentClass referencedClass) { - Iterator properties = referencedClass.getPropertyIterator(); - while (properties.hasNext()) { - Property property = properties.next(); - if (property.getValue() instanceof Collection) { - // The equality is intentional. We want to find a collection property with the same collection table. - //noinspection ObjectEquality - if (((Collection) property.getValue()).getCollectionTable() == collectionTable) { - return property.getName(); - } - } - } - - return null; - } - - private void addManyToManyNotOwning(Property property, CompositeMapperBuilder mapper, String entityName) { - Collection propertyValue = (Collection) property.getValue(); - ManyToOne mto = (ManyToOne) propertyValue.getElement(); - - String referencedEntityName = mto.getReferencedEntityName(); - - EntityConfiguration configuration = entitiesConfigurations.get(entityName); - if (configuration == null) { - throw new MappingException("A versioned relation to a non-versioned entity " + entityName + "!"); - } - - EntityConfiguration referencedConfiguration = entitiesConfigurations.get(referencedEntityName); - if (referencedConfiguration == null) { - throw new MappingException("A versioned relation to a non-versioned entity " + referencedEntityName + "!"); - } - - String mappedBy = getMappedBy(propertyValue.getCollectionTable(), cfg.getClassMapping(referencedEntityName)); - if (mappedBy == null) { - throw new MappingException("Unable to read the mapped by attribute for " + property.getName()); - } - - IdMappingData referencingIdMapping = configuration.getIdMappingData(); - IdMappingData referencedIdMapping = referencedConfiguration.getIdMappingData(); - - String referencingPrefix = mappedBy + "_"; - String referencedPrefix = StringTools.getLastComponent(referencedEntityName) + "_"; - - // Name of the entity that will be used to store the relation between the two entities. - // The order is inverse, as the referenced and referencing entity names are swapped with respect to the - // name of the entity that was created on the owning side in addOneToManyDetached. - String middleEntityName = getMiddleEntityName(referencedEntityName, entityName, mto); - String versionsMiddleEntityName = verEntCfg.getVersionsEntityName(middleEntityName); - - entitiesConfigurations.get(entityName).addManyToManyNotOwningRelation(property.getName(), mappedBy, - referencedEntityName); - - // Adding the property mapper - mapper.addComposite(property.getName(), new ManyToManyNotOwningMapper(verEntCfg, entityName, - referencedEntityName, property.getName(), verEntCfg.getVersionsEntityName(referencedEntityName), - versionsMiddleEntityName, referencingIdMapping.getIdMapper().prefixMappedProperties(referencingPrefix), - referencedIdMapping.getIdMapper().prefixMappedProperties(referencedPrefix), - entitiesConfigurations.get(referencedEntityName).getIdMapper())); - } - private ModificationStore getStoreForProperty(Property property, PropertyStoreInfo propertyStoreInfo, List unversionedProperties) { /* @@ -479,100 +118,93 @@ } @SuppressWarnings({"unchecked"}) - private void addIdProperties(Element parent, Iterator properties, SimpleMapperBuilder mapper, boolean key) { - while (properties.hasNext()) { - Property property = properties.next(); - Type propertyType = property.getType(); - if (!"_identifierMapper".equals(property.getName())) { - if (propertyType instanceof ImmutableType) { - addSimpleProperty(parent, property, mapper, ModificationStore.FULL, key); - } else { - throw new MappingException("Type not supported: " + propertyType.getClass().getName()); - } + void addValue(Element parent, String name, Value value, CompositeMapperBuilder currentMapper, + ModificationStore store, String entityName, EntityXmlMappingData xmlMappingData, + boolean firstPass) { + Type type = value.getType(); + + if (type instanceof ComponentType) { + // only first pass + if (firstPass) { + basicMetadataGenerator.addComponent(parent, name, value, currentMapper, entityName, xmlMappingData, firstPass); } + } else if (type instanceof ImmutableType || type instanceof MutableType) { + // only first pass + if (firstPass) { + basicMetadataGenerator.addSimpleValue(parent, name, value, currentMapper, store, false); + } + } else if (type instanceof CustomType && + "org.hibernate.type.EnumType".equals(type.getName())) { + // only first pass + if (firstPass) { + basicMetadataGenerator.addEnumValue(parent, name, value, currentMapper, store); + } + } else if (type instanceof ManyToOneType) { + // only second pass + if (!firstPass) { + toOneRelationMetadataGenerator.addToOne(parent, name, value, currentMapper, entityName); + } + } else if (type instanceof OneToOneType) { + // only second pass + if (!firstPass) { + toOneRelationMetadataGenerator.addOneToOneNotOwning(name, value, currentMapper, entityName); + } + } else if ("org.hibernate.type.PrimitiveByteArrayBlobType".equals( + type.getClass().getName())) { + // only first pass + if (firstPass) { + basicMetadataGenerator.addSimpleValue(parent, name, value, currentMapper, store, false); + } + } else if (type instanceof CustomType && + ("org.hibernate.type.PrimitiveCharacterArrayClobType".equals(type.getName()) || + "org.hibernate.type.StringClobType".equals(type.getName()))) { + // only first pass + if (firstPass) { + basicMetadataGenerator.addSimpleValue(parent, name, value, currentMapper, store, false); + } + } else if ((type instanceof BagType || type instanceof SetType) && + ((((Collection) value).getElement() instanceof OneToMany))) { + // only second pass + if (!firstPass) { + collectionMetadataGenerator.addOneToManyAttached(name, value, currentMapper, entityName); + } + } else if ((type instanceof BagType || type instanceof SetType) && + ((((Collection) value).getElement() instanceof ManyToOne)) && + !((Collection) value).isInverse()) { + // only second pass + if (!firstPass) { + collectionMetadataGenerator.addOneToManyDetached(name, value, currentMapper, entityName, xmlMappingData); + } + } else if ((type instanceof BagType || type instanceof SetType) && + ((((Collection) value).getElement() instanceof ManyToOne)) && + ((Collection) value).isInverse()) { + // only second pass + if (!firstPass) { + collectionMetadataGenerator.addManyToManyNotOwning(name, value, currentMapper, entityName); + } + } else { + String message = "Type not supported for versioning: " + type.getClass().getName() + + ", on entity " + entityName + ", property '" + name + "'."; + if (globalCfg.isWarnOnUnsupportedTypes()) { + log.warn(message); + } else { + throw new MappingException(message); + } } } @SuppressWarnings({"unchecked"}) private void addProperties(Element parent, Iterator properties, CompositeMapperBuilder currentMapper, - PropertyStoreInfo propertyStoreInfo, String entityName, EntityMappingData mappingData, + PropertyStoreInfo propertyStoreInfo, String entityName, EntityXmlMappingData xmlMappingData, List unversionedProperties, boolean firstPass) { while (properties.hasNext()) { Property property = properties.next(); - Type propertyType = property.getType(); if (!"_identifierMapper".equals(property.getName())) { ModificationStore store = getStoreForProperty(property, propertyStoreInfo, unversionedProperties); - if (store != null) { - if (propertyType instanceof ComponentType) { - // only first pass - if (firstPass) { - addComponent(parent, property, currentMapper, store, entityName, mappingData, - unversionedProperties, firstPass); - } - } else if (propertyType instanceof ImmutableType || propertyType instanceof MutableType) { - // only first pass - if (firstPass) { - addSimpleProperty(parent, property, currentMapper, store, false); - } - } else if (propertyType instanceof CustomType && - "org.hibernate.type.EnumType".equals(propertyType.getName())) { - // only first pass - if (firstPass) { - addEnumProperty(parent, property, currentMapper, store); - } - } else if (propertyType instanceof ManyToOneType) { - // only second pass - if (!firstPass) { - addToOne(parent, property, currentMapper, entityName); - } - } else if (propertyType instanceof OneToOneType) { - // only second pass - if (!firstPass) { - addOneToOneNotOwning(property, currentMapper, entityName); - } - } else if ("org.hibernate.type.PrimitiveByteArrayBlobType".equals( - propertyType.getClass().getName())) { - // only first pass - if (firstPass) { - addSimpleProperty(parent, property, currentMapper, store, false); - } - } else if (propertyType instanceof CustomType && - ("org.hibernate.type.PrimitiveCharacterArrayClobType".equals(propertyType.getName()) || - "org.hibernate.type.StringClobType".equals(propertyType.getName()))) { - // only first pass - if (firstPass) { - addSimpleProperty(parent, property, currentMapper, store, false); - } - } else if ((propertyType instanceof BagType || propertyType instanceof SetType) && - ((((Collection) property.getValue()).getElement() instanceof OneToMany))) { - // only second pass - if (!firstPass) { - addOneToManyAttached(property, currentMapper, entityName); - } - } else if ((propertyType instanceof BagType || propertyType instanceof SetType) && - ((((Collection) property.getValue()).getElement() instanceof ManyToOne)) && - !((Collection) property.getValue()).isInverse()) { - // only second pass - if (!firstPass) { - addOneToManyDetached(property, currentMapper, entityName, mappingData); - } - } else if ((propertyType instanceof BagType || propertyType instanceof SetType) && - ((((Collection) property.getValue()).getElement() instanceof ManyToOne)) && - ((Collection) property.getValue()).isInverse()) { - // only second pass - if (!firstPass) { - addManyToManyNotOwning(property, currentMapper, entityName); - } - } else { - String message = "Type not supported for versioning: " + propertyType.getClass().getName() + - ", on entity " + entityName + ", property '" + property.getName() + "'."; - if (globalCfg.isWarnOnUnsupportedTypes()) { - log.warn(message); - } else { - throw new MappingException(message); - } - } + if (store != null) { + addValue(parent, property.getName(), property.getValue(), currentMapper, store, entityName, + xmlMappingData, firstPass); } } } @@ -610,14 +242,14 @@ joinElements.put(join, joinElement); Element joinKey = joinElement.addElement("key"); - addColumns(joinKey, join.getKey().getColumnIterator()); + MetadataTools.addColumns(joinKey, join.getKey().getColumnIterator()); MetadataTools.addColumn(joinKey, verEntCfg.getRevisionPropName(), null); } } @SuppressWarnings({"unchecked"}) private void addJoins(PersistentClass pc, CompositeMapperBuilder currentMapper, PropertyStoreInfo propertyStoreInfo, - String entityName, EntityMappingData mappingData, List unversionedProperties, + String entityName, EntityXmlMappingData xmlMappingData, List unversionedProperties, boolean firstPass) { Iterator joins = pc.getJoinIterator(); @@ -626,52 +258,12 @@ Element joinElement = entitiesJoins.get(entityName).get(join); addProperties(joinElement, join.getPropertyIterator(), currentMapper, propertyStoreInfo, entityName, - mappingData, unversionedProperties, firstPass); + xmlMappingData, unversionedProperties, firstPass); } } - @SuppressWarnings({"unchecked"}) - private IdMappingData addId(PersistentClass pc) { - // Xml mapping which will be used for relations - Element rel_id_mapping = new DefaultElement("properties"); - // Xml mapping which will be used for the primary key of the versions table - Element orig_id_mapping = new DefaultElement("composite-id"); - Property id_prop = pc.getIdentifierProperty(); - Component id_mapper = pc.getIdentifierMapper(); - SimpleIdMapperBuilder mapper; - if (id_mapper != null) { - mapper = new MultipleIdMapper(((Component) pc.getIdentifier()).getComponentClassName()); - addIdProperties(rel_id_mapping, (Iterator) id_mapper.getPropertyIterator(), mapper, false); - - // null mapper - the mapping where already added the first time, now we only want to generate the xml - addIdProperties(orig_id_mapping, (Iterator) id_mapper.getPropertyIterator(), null, true); - } else if (id_prop.isComposite()) { - Component id_component = (Component) id_prop.getValue(); - - mapper = new EmbeddedIdMapper(id_prop.getName(), id_component.getComponentClassName()); - addIdProperties(rel_id_mapping, (Iterator) id_component.getPropertyIterator(), mapper, false); - - // null mapper - the mapping where already added the first time, now we only want to generate the xml - addIdProperties(orig_id_mapping, (Iterator) id_component.getPropertyIterator(), null, true); - } else { - mapper = new SingleIdMapper(); - - addSimpleProperty(rel_id_mapping, id_prop, mapper, ModificationStore.FULL, false); - - // null mapper - the mapping where already added the first time, now we only want to generate the xml - addSimpleProperty(orig_id_mapping, id_prop, null, ModificationStore.FULL, true); - } - - orig_id_mapping.addAttribute("name", verEntCfg.getOriginalIdPropName()); - - // Adding a relation to the revision entity (effectively: the "revision number" property) - addRevisionInfoRelation(orig_id_mapping); - - return new IdMappingData(mapper, orig_id_mapping, rel_id_mapping); - } - private void addPersisterHack(Element class_mapping) { String persisterClassName; if (HibernateVersion.get().startsWith("3.3")) { @@ -685,7 +277,7 @@ @SuppressWarnings({"unchecked"}) public void generateFirstPass(PersistentClass pc, PersistentClassVersioningData versioningData, - EntityMappingData mappingData) { + EntityXmlMappingData xmlMappingData) { String schema = versioningData.schema; if (StringTools.isEmpty(schema)) { schema = pc.getTable().getSchema(); @@ -701,7 +293,7 @@ String versionsTableName = verEntCfg.getVersionsTableName(entityName, pc.getTable().getName()); // Generating a mapping for the id - IdMappingData idMapper = addId(pc); + IdMappingData idMapper = idMetadataGenerator.addId(pc); Element class_mapping; ExtendedPropertyMapper propertyMapper; @@ -711,14 +303,14 @@ switch (inheritanceType) { case NONE: - class_mapping = MetadataTools.createEntity(mappingData.getMainMapping(), versionsEntityName, versionsTableName, + class_mapping = MetadataTools.createEntity(xmlMappingData.getMainXmlMapping(), versionsEntityName, versionsTableName, schema, catalog, pc.getDiscriminatorValue()); propertyMapper = new MultiPropertyMapper(); // Checking if there is a discriminator column if (pc.getDiscriminator() != null) { Element discriminator_element = class_mapping.addElement("discriminator"); - addColumns(discriminator_element, pc.getDiscriminator().getColumnIterator()); + MetadataTools.addColumns(discriminator_element, pc.getDiscriminator().getColumnIterator()); discriminator_element.addAttribute("type", pc.getDiscriminator().getType().getName()); // If so, there is some inheritance scheme -> using the persister hack. @@ -734,7 +326,7 @@ break; case SINGLE: String extendsEntityName = verEntCfg.getVersionsEntityName(pc.getSuperclass().getEntityName()); - class_mapping = MetadataTools.createSubclassEntity(mappingData.getMainMapping(), versionsEntityName, + class_mapping = MetadataTools.createSubclassEntity(xmlMappingData.getMainXmlMapping(), versionsEntityName, versionsTableName, schema, catalog, extendsEntityName, pc.getDiscriminatorValue()); addPersisterHack(class_mapping); @@ -757,12 +349,12 @@ // Mapping unjoined properties addProperties(class_mapping, (Iterator) pc.getUnjoinedPropertyIterator(), propertyMapper, - versioningData.propertyStoreInfo, pc.getEntityName(), mappingData, versioningData.unversionedProperties, + versioningData.propertyStoreInfo, pc.getEntityName(), xmlMappingData, versioningData.unversionedProperties, true); // Creating and mapping joins (first pass) createJoins(pc, class_mapping, versioningData); - addJoins(pc, propertyMapper, versioningData.propertyStoreInfo, pc.getEntityName(), mappingData, + addJoins(pc, propertyMapper, versioningData.propertyStoreInfo, pc.getEntityName(), xmlMappingData, versioningData.unversionedProperties, true); // Storing the generated configuration @@ -773,27 +365,57 @@ @SuppressWarnings({"unchecked"}) public void generateSecondPass(PersistentClass pc, PersistentClassVersioningData versioningData, - EntityMappingData mappingData) { + EntityXmlMappingData xmlMappingData) { String entityName = pc.getEntityName(); CompositeMapperBuilder propertyMapper = entitiesConfigurations.get(entityName).getPropertyMapper(); // Mapping unjoined properties - Element parent = mappingData.getMainMapping().getRootElement().element("class"); + Element parent = xmlMappingData.getMainXmlMapping().getRootElement().element("class"); if (parent == null) { - parent = mappingData.getMainMapping().getRootElement().element("subclass"); + parent = xmlMappingData.getMainXmlMapping().getRootElement().element("subclass"); } addProperties(parent, (Iterator) pc.getUnjoinedPropertyIterator(), - propertyMapper, versioningData.propertyStoreInfo, entityName, mappingData, + propertyMapper, versioningData.propertyStoreInfo, entityName, xmlMappingData, versioningData.unversionedProperties, false); // Mapping joins (second pass) - addJoins(pc, propertyMapper, versioningData.propertyStoreInfo, entityName, mappingData, + addJoins(pc, propertyMapper, versioningData.propertyStoreInfo, entityName, xmlMappingData, versioningData.unversionedProperties, false); } public Map getEntitiesConfigurations() { return entitiesConfigurations; } + + // Getters for generators and configuration + + BasicMetadataGenerator getBasicMetadataGenerator() { + return basicMetadataGenerator; + } + + IdMetadataGenerator getIdMetadataGenerator() { + return idMetadataGenerator; + } + + CollectionMetadataGenerator getCollectionMetadataGenerator() { + return collectionMetadataGenerator; + } + + ToOneRelationMetadataGenerator getToOneRelationMetadataGenerator() { + return toOneRelationMetadataGenerator; + } + + Configuration getCfg() { + return cfg; + } + + GlobalConfiguration getGlobalCfg() { + return globalCfg; + } + + VersionsEntitiesConfiguration getVerEntCfg() { + return verEntCfg; + } } Deleted: trunk/src/main/org/jboss/envers/entities/mapper/CompositePropertyMapperBuilder.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/CompositePropertyMapperBuilder.java 2008-09-03 09:54:06 UTC (rev 148) +++ trunk/src/main/org/jboss/envers/entities/mapper/CompositePropertyMapperBuilder.java 2008-09-15 15:32:02 UTC (rev 149) @@ -1,28 +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; - -/** - * @author Adam Warski (adam at warski dot org) - */ -public interface CompositePropertyMapperBuilder extends PropertyMapper, CompositeMapperBuilder { -} Modified: trunk/src/main/org/jboss/envers/entities/mapper/MapPropertyMapper.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/MapPropertyMapper.java 2008-09-03 09:54:06 UTC (rev 148) +++ trunk/src/main/org/jboss/envers/entities/mapper/MapPropertyMapper.java 2008-09-15 15:32:02 UTC (rev 149) @@ -39,7 +39,7 @@ /** * @author Adam Warski (adam at warski dot org) */ -public class MapPropertyMapper implements CompositePropertyMapperBuilder { +public class MapPropertyMapper implements PropertyMapper, CompositeMapperBuilder { private String propertyName; private ExtendedPropertyMapper delegate; Modified: trunk/src/main/org/jboss/envers/entities/mapper/MultiPropertyMapper.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/MultiPropertyMapper.java 2008-09-03 09:54:06 UTC (rev 148) +++ trunk/src/main/org/jboss/envers/entities/mapper/MultiPropertyMapper.java 2008-09-15 15:32:02 UTC (rev 149) @@ -55,7 +55,7 @@ throw new MappingException("Mapping for " + propertyName + " already added!"); } - CompositePropertyMapperBuilder mapperBuilder = new MapPropertyMapper(propertyName); + MapPropertyMapper mapperBuilder = new MapPropertyMapper(propertyName); properties.put(propertyName, mapperBuilder); return mapperBuilder; Modified: trunk/src/main/org/jboss/envers/entities/mapper/PersistentCollectionChangeData.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/PersistentCollectionChangeData.java 2008-09-03 09:54:06 UTC (rev 148) +++ trunk/src/main/org/jboss/envers/entities/mapper/PersistentCollectionChangeData.java 2008-09-15 15:32:02 UTC (rev 149) @@ -26,6 +26,8 @@ import java.util.Map; /** + * Data describing the change of a single object in a persisten collection (when the object was added, removed or + * modified in the collection). * @author Adam Warski (adam at warski dot org) */ public class PersistentCollectionChangeData { @@ -39,6 +41,10 @@ this.obj = obj; } + /** + * + * @return Name of the (middle) entity that holds the collection data. + */ public String getEntityName() { return entityName; } @@ -47,6 +53,10 @@ return data; } + /** + * + * @return The affected object, which was changed (added, removed, modified) in the collection. + */ public Object getObj() { return obj; } Modified: trunk/src/main/org/jboss/envers/entities/mapper/PropertyMapper.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/PropertyMapper.java 2008-09-03 09:54:06 UTC (rev 148) +++ trunk/src/main/org/jboss/envers/entities/mapper/PropertyMapper.java 2008-09-15 15:32:02 UTC (rev 149) @@ -36,9 +36,8 @@ /** * Maps properties to the given map, basing on differences between properties of new and old objects. * @param data Data to map to. - * @param newObj If the modification store for a property is DIFF, diff-s are created from differences between - * newObj and oldObj. - * @param oldObj Data to map from. + * @param newObj New state of the entity. + * @param oldObj Old state of the entity. * @return True if there are any differences between the states represented by newObj and oldObj. */ public boolean mapToMapFromEntity(Map data, Object newObj, Object oldObj); @@ -50,7 +49,7 @@ * @param data Data to map from. * @param primaryKey Primary key of the object to which we map (for relations) * @param versionsReader VersionsReader for reading relations - * @param revision Revision at which the obejct is read, for reading relations + * @param revision Revision at which the object is read, for reading relations */ public void mapToEntityFromMap(VersionsConfiguration verCfg, Object obj, Map data, Object primaryKey, VersionsReaderImplementor versionsReader, Number revision); Modified: trunk/src/main/org/jboss/envers/entities/mapper/relation/AbstractOneToManyMapper.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/AbstractOneToManyMapper.java 2008-09-03 09:54:06 UTC (rev 148) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/AbstractOneToManyMapper.java 2008-09-15 15:32:02 UTC (rev 149) @@ -28,6 +28,7 @@ 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.Initializor; +import org.jboss.envers.entities.mapper.relation.lazy.proxy.MapProxy; import org.jboss.envers.entities.mapper.PropertyMapper; import org.jboss.envers.exception.VersionsException; import org.jboss.envers.configuration.VersionsConfiguration; @@ -48,10 +49,10 @@ this.propertyName = propertyName; } - protected abstract Initializor getInitializator(VersionsConfiguration verCfg, - VersionsReaderImplementor versionsReader, - Class entityClass, Object primaryKey, - Number revision, Class collectionClass); + protected abstract Initializor getInitializator(VersionsConfiguration verCfg, + VersionsReaderImplementor versionsReader, + Class entityClass, Object primaryKey, + Number revision, Class collectionClass); @SuppressWarnings({"unchecked"}) public void mapToEntityFromMap(VersionsConfiguration verCfg, Object obj, Map data, Object primaryKey, @@ -71,6 +72,8 @@ value = new ListProxy(getInitializator(verCfg, versionsReader, entityClass, primaryKey, revision, ArrayList.class)); } else if (Set.class.isAssignableFrom(collectionClass) || Collection.class.isAssignableFrom(collectionClass)) { value = new SetProxy(getInitializator(verCfg, versionsReader, entityClass, primaryKey, revision, HashSet.class)); + } else if (Map.class.isAssignableFrom(collectionClass)) { + value = new MapProxy(getInitializator(verCfg, versionsReader, entityClass, primaryKey, revision, HashMap.class)); } else { throw new VersionsException("Unsupported versioned collection type: " + collectionClass.getName()); } Modified: trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyDetachedMapper.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyDetachedMapper.java 2008-09-03 09:54:06 UTC (rev 148) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyDetachedMapper.java 2008-09-15 15:32:02 UTC (rev 149) @@ -134,7 +134,7 @@ return false; } - protected Initializor getInitializator(VersionsConfiguration verCfg, + protected Initializor getInitializator(VersionsConfiguration verCfg, VersionsReaderImplementor versionsReader, Class entityClass, Object primaryKey, Number revision, Class collectionClass) { Modified: trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/DetachedRelationInitializor.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/DetachedRelationInitializor.java 2008-09-03 09:54:06 UTC (rev 148) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/DetachedRelationInitializor.java 2008-09-15 15:32:02 UTC (rev 149) @@ -36,7 +36,7 @@ /** * @author Adam Warski (adam at warski dot org) */ -public class DetachedRelationInitializor implements Initializor { +public class DetachedRelationInitializor implements Initializor { private final VersionsConfiguration verCfg; private final String entityName; private final DetachedRelationQueryGenerator queryGenerator; @@ -71,7 +71,8 @@ throw new VersionsException(e); } - entityInstantiator.addInstancesFromVersionsEntities(entityName, result, queryResult, revision); + // TODO + entityInstantiator.addInstancesFromVersionsEntities(entityName, (Collection) result, queryResult, revision); return result; } Modified: trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/Initializor.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/Initializor.java 2008-09-03 09:54:06 UTC (rev 148) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/Initializor.java 2008-09-15 15:32:02 UTC (rev 149) @@ -26,6 +26,6 @@ /** * @author Adam Warski (adam at warski dot org) */ -public interface Initializor { +public interface Initializor { T initialize(); } Added: trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/MapProxy.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/MapProxy.java (rev 0) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/MapProxy.java 2008-09-15 15:32:02 UTC (rev 149) @@ -0,0 +1,117 @@ +/* + * JBoss, Home of Professional Open Source + * + * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated + * by the @authors tag. All rights reserved. + * + * See the copyright.txt in the distribution for a full listing of individual + * contributors. 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.proxy; + +import java.util.Map; +import java.util.Set; +import java.util.Collection; + +/** + * @author Adam Warski (adam at warski dot org) + */ +public class MapProxy implements Map { + private Initializor> initializor; + protected Map delegate; + + public MapProxy(Initializor> initializor) { + this.initializor = initializor; + } + + private void checkInit() { + if (delegate == null) { + delegate = initializor.initialize(); + } + } + + public int size() { + checkInit(); + return delegate.size(); + } + + public boolean isEmpty() { + checkInit(); + return delegate.isEmpty(); + } + + public boolean containsKey(Object o) { + checkInit(); + return delegate.containsKey(o); + } + + public boolean containsValue(Object o) { + checkInit(); + return delegate.containsValue(o); + } + + public V get(Object o) { + checkInit(); + return delegate.get(o); + } + + public V put(K k, V v) { + checkInit(); + return delegate.put(k, v); + } + + public V remove(Object o) { + checkInit(); + return delegate.remove(o); + } + + public void putAll(Map map) { + checkInit(); + delegate.putAll(map); + } + + public void clear() { + checkInit(); + delegate.clear(); + } + + public Set keySet() { + checkInit(); + return delegate.keySet(); + } + + public Collection values() { + checkInit(); + return delegate.values(); + } + + public Set> entrySet() { + checkInit(); + return delegate.entrySet(); + } + + @SuppressWarnings({"EqualsWhichDoesntCheckParameterClass"}) + public boolean equals(Object o) { + checkInit(); + return delegate.equals(o); + } + + public int hashCode() { + checkInit(); + return delegate.hashCode(); + } +} Modified: trunk/src/main/org/jboss/envers/query/criteria/CriteriaTools.java =================================================================== --- trunk/src/main/org/jboss/envers/query/criteria/CriteriaTools.java 2008-09-03 09:54:06 UTC (rev 148) +++ trunk/src/main/org/jboss/envers/query/criteria/CriteriaTools.java 2008-09-15 15:32:02 UTC (rev 149) @@ -42,7 +42,7 @@ public static RelationDescription getRelatedEntity(VersionsConfiguration verCfg, String entityName, String propertyName) throws VersionsException { - RelationDescription relationDesc = verCfg.getEntCfg().get(entityName).getRelationDescription(propertyName); + RelationDescription relationDesc = verCfg.getEntCfg().getRelationDescription(entityName, propertyName); if (relationDesc == null) { return null; Modified: trunk/src/main/org/jboss/envers/tools/reflection/ReflectionTools.java =================================================================== --- trunk/src/main/org/jboss/envers/tools/reflection/ReflectionTools.java 2008-09-03 09:54:06 UTC (rev 148) +++ trunk/src/main/org/jboss/envers/tools/reflection/ReflectionTools.java 2008-09-15 15:32:02 UTC (rev 149) @@ -62,7 +62,7 @@ Getter value = getterCache.get(key); if (value == null) { value = ReflectHelper.getGetter(cls, propertyName); - // It's ok if two getter are generated concurrently + // It's ok if two getters are generated concurrently getterCache.put(key, value); } From jboss-envers-commits at lists.jboss.org Fri Sep 19 10:51:23 2008 From: jboss-envers-commits at lists.jboss.org (jboss-envers-commits at lists.jboss.org) Date: Fri, 19 Sep 2008 10:51:23 -0400 Subject: [jboss-envers-commits] JBoss Envers SVN: r151 - in trunk/src/main/org/jboss/envers: configuration and 11 other directories. Message-ID: Author: adamw Date: 2008-09-19 10:51:22 -0400 (Fri, 19 Sep 2008) New Revision: 151 Added: trunk/src/main/org/jboss/envers/configuration/metadata/QueryGeneratorBuilder.java trunk/src/main/org/jboss/envers/entities/mapper/relation/AbstractCollectionMapper.java trunk/src/main/org/jboss/envers/entities/mapper/relation/BasicCollectionMapper.java trunk/src/main/org/jboss/envers/entities/mapper/relation/MiddleComponentData.java trunk/src/main/org/jboss/envers/entities/mapper/relation/MiddleIdData.java trunk/src/main/org/jboss/envers/entities/mapper/relation/component/ trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleComponentMapper.java trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleDummyComponentMapper.java trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleMapKeyComponentMapper.java trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleRelatedComponentMapper.java trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleSimpleComponentMapper.java trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/ 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/Initializor.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/lazy/proxy/SortedMapProxy.java trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/SortedSetProxy.java trunk/src/main/org/jboss/envers/entities/mapper/relation/query/ trunk/src/main/org/jboss/envers/entities/mapper/relation/query/MiddleTableQueryGenerator.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 Removed: trunk/src/main/org/jboss/envers/configuration/metadata/EntityMappingData.java trunk/src/main/org/jboss/envers/entities/mapper/relation/DetachedRelationQueryGenerator.java trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/Initializor.java Modified: trunk/src/main/org/jboss/envers/VersionsReader.java trunk/src/main/org/jboss/envers/configuration/EntitiesConfigurator.java trunk/src/main/org/jboss/envers/configuration/metadata/BasicMetadataGenerator.java trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java trunk/src/main/org/jboss/envers/configuration/metadata/IdMetadataGenerator.java trunk/src/main/org/jboss/envers/configuration/metadata/ToOneRelationMetadataGenerator.java trunk/src/main/org/jboss/envers/configuration/metadata/VersionsMetadataGenerator.java trunk/src/main/org/jboss/envers/entities/mapper/PropertyMapper.java trunk/src/main/org/jboss/envers/entities/mapper/relation/AbstractOneToManyMapper.java trunk/src/main/org/jboss/envers/entities/mapper/relation/ManyToManyNotOwningMapper.java trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyAttachedMapper.java trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyDetachedMapper.java trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/DetachedRelationInitializor.java trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/OneToManyAttachedInitializor.java trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/CollectionProxy.java trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/ListProxy.java trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/MapProxy.java trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/SetProxy.java trunk/src/main/org/jboss/envers/event/VersionsEventListener.java trunk/src/main/org/jboss/envers/exception/NotVersionedException.java trunk/src/main/org/jboss/envers/exception/RevisionDoesNotExistException.java trunk/src/main/org/jboss/envers/query/VersionsQueryCreator.java Log: ENVERS-42, ENVERS-44: initial support of redesigned collection handling, ENVERS-50: merging patch Modified: trunk/src/main/org/jboss/envers/VersionsReader.java =================================================================== --- trunk/src/main/org/jboss/envers/VersionsReader.java 2008-09-17 15:38:55 UTC (rev 150) +++ trunk/src/main/org/jboss/envers/VersionsReader.java 2008-09-19 14:51:22 UTC (rev 151) @@ -40,7 +40,7 @@ * @return The found entity instance at the given revision (its properties may be partially filled * if not all properties are versioned) or null, if an entity with that id didn't exist at that * revision. - * @throws IllegalArgumentException If cls, primaryKey is null or revision is less or equal to 0. + * @throws IllegalArgumentException If cls or primaryKey is null or revision is less or equal to 0. * @throws NotVersionedException When entities of the given class are not versioned. * @throws IllegalStateException If the associated entity manager is closed. */ @@ -48,18 +48,13 @@ IllegalArgumentException, NotVersionedException, IllegalStateException; /** - * Get revisions, at which an entity was modified. - * Warning! Only revisions, at which properties that the entity "owns" changed will be returned. - * Properties not owned by an entity are: - *
      - *
    • @OneToOne(mappedBy="...")
    • - *
    • @OneToMany
    • - *
    + * Get a list of revision numbers, at which an entity was modified. * @param cls Class of the entity. * @param primaryKey Primary key of the entity. - * @return A list of revision numbers, at which the entity was modified, sorted in ascending order. + * @return A list of revision numbers, at which the entity was modified, sorted in ascending order (so older + * revisions come first). * @throws NotVersionedException When entities of the given class are not versioned. - * @throws IllegalArgumentException If cls, primaryKey is null. + * @throws IllegalArgumentException If cls or primaryKey is null. * @throws IllegalStateException If the associated entity manager is closed. */ List getRevisions(Class cls, Object primaryKey) @@ -78,8 +73,8 @@ /** * Gets the revision number, that corresponds to the given date. More precisely, returns - * the number of the highest revision, which was created before the given date. So: - * getRevisionDate(getRevisionNumberForDate(date)) < date and + * the number of the highest revision, which was created on or before the given date. So: + * getRevisionDate(getRevisionNumberForDate(date)) <= date and * getRevisionDate(getRevisionNumberForDate(date)+1) > date. * @param date Date for which to get the revision. * @return Revision number corresponding to the given date. Modified: trunk/src/main/org/jboss/envers/configuration/EntitiesConfigurator.java =================================================================== --- trunk/src/main/org/jboss/envers/configuration/EntitiesConfigurator.java 2008-09-17 15:38:55 UTC (rev 150) +++ trunk/src/main/org/jboss/envers/configuration/EntitiesConfigurator.java 2008-09-19 14:51:22 UTC (rev 151) @@ -94,12 +94,12 @@ try { cfg.addDocument(writer.write(xmlMappingData.getMainXmlMapping())); // TODO - //writeDocument(xmlMappingData.getMainXmlMapping()); + writeDocument(xmlMappingData.getMainXmlMapping()); for (Document additionalMapping : xmlMappingData.getAdditionalXmlMappings()) { cfg.addDocument(writer.write(additionalMapping)); // TODO - //writeDocument(additionalMapping); + writeDocument(additionalMapping); } } catch (DocumentException e) { throw new MappingException(e); Modified: trunk/src/main/org/jboss/envers/configuration/metadata/BasicMetadataGenerator.java =================================================================== --- trunk/src/main/org/jboss/envers/configuration/metadata/BasicMetadataGenerator.java 2008-09-17 15:38:55 UTC (rev 150) +++ trunk/src/main/org/jboss/envers/configuration/metadata/BasicMetadataGenerator.java 2008-09-19 14:51:22 UTC (rev 151) @@ -18,7 +18,7 @@ * Generates metadata for basic properties: immutable types (including enums) and components * @author Adam Warski (adam at warski dot org) */ -public class BasicMetadataGenerator { +public final class BasicMetadataGenerator { private final VersionsMetadataGenerator mainGenerator; BasicMetadataGenerator(VersionsMetadataGenerator versionsMetadataGenerator) { @@ -28,9 +28,11 @@ @SuppressWarnings({"unchecked"}) void addSimpleValue(Element parent, String name, Value value, SimpleMapperBuilder mapper, ModificationStore store, boolean key) { - Element prop_mapping = MetadataTools.addProperty(parent, name, - value.getType().getName(), key); - MetadataTools.addColumns(prop_mapping, (Iterator) value.getColumnIterator()); + if (parent != null) { + Element prop_mapping = MetadataTools.addProperty(parent, name, + value.getType().getName(), key); + MetadataTools.addColumns(prop_mapping, (Iterator) value.getColumnIterator()); + } // A null mapper means that we only want to add xml mappings (while building the id mapping) if (mapper != null) { @@ -41,23 +43,25 @@ @SuppressWarnings({"unchecked"}) void addEnumValue(Element parent, String name, Value value, SimpleMapperBuilder mapper, ModificationStore store) { - Element prop_mapping = parent.addElement("property"); - prop_mapping.addAttribute("name", name); + if (parent != null) { + Element prop_mapping = parent.addElement("property"); + prop_mapping.addAttribute("name", name); - CustomType propertyType = (CustomType) value.getType(); + CustomType propertyType = (CustomType) value.getType(); - Element type_mapping = prop_mapping.addElement("type"); - type_mapping.addAttribute("name", propertyType.getName()); + Element type_mapping = prop_mapping.addElement("type"); + type_mapping.addAttribute("name", propertyType.getName()); - Element type_param1 = type_mapping.addElement("param"); - type_param1.addAttribute("name", "enumClass"); - type_param1.setText(propertyType.getReturnedClass().getName()); + Element type_param1 = type_mapping.addElement("param"); + type_param1.addAttribute("name", "enumClass"); + type_param1.setText(propertyType.getReturnedClass().getName()); - Element type_param2 = type_mapping.addElement("param"); - type_param2.addAttribute("name", "type"); - type_param2.setText(Integer.toString(propertyType.sqlTypes(null)[0])); + Element type_param2 = type_mapping.addElement("param"); + type_param2.addAttribute("name", "type"); + type_param2.setText(Integer.toString(propertyType.sqlTypes(null)[0])); - MetadataTools.addColumns(prop_mapping, (Iterator) value.getColumnIterator()); + MetadataTools.addColumns(prop_mapping, (Iterator) value.getColumnIterator()); + } mapper.add(name, store); } @@ -75,25 +79,27 @@ Element component_mapping = null; Component prop_component = (Component) value; - if (!firstPass) { - // The required element already exists. - Iterator iter = parent.elementIterator("component"); - while (iter.hasNext()) { - Element child = iter.next(); - if (child.attribute("name").getText().equals(name)) { - component_mapping = child; - break; + if (parent != null) { + if (!firstPass) { + // The required element already exists. + Iterator iter = parent.elementIterator("component"); + while (iter.hasNext()) { + Element child = iter.next(); + if (child.attribute("name").getText().equals(name)) { + component_mapping = child; + break; + } } - } - if (component_mapping == null) { - throw new VersionsException("Element for component not found during second pass!"); + if (component_mapping == null) { + throw new VersionsException("Element for component not found during second pass!"); + } + } else { + component_mapping = parent.addElement("component"); + component_mapping.addAttribute("name", name); + + addComponentClassName(component_mapping, prop_component); } - } else { - component_mapping = parent.addElement("component"); - component_mapping.addAttribute("name", name); - - addComponentClassName(component_mapping, prop_component); } CompositeMapperBuilder componentMapper = mapper.addComposite(name); Modified: trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java =================================================================== --- trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java 2008-09-17 15:38:55 UTC (rev 150) +++ trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java 2008-09-19 14:51:22 UTC (rev 151) @@ -3,28 +3,50 @@ import org.hibernate.mapping.*; import org.hibernate.mapping.Collection; import org.hibernate.MappingException; +import org.hibernate.type.*; import org.jboss.envers.entities.mapper.CompositeMapperBuilder; import org.jboss.envers.entities.mapper.relation.*; +import org.jboss.envers.entities.mapper.relation.lazy.proxy.SetProxy; +import org.jboss.envers.entities.mapper.relation.lazy.proxy.ListProxy; +import org.jboss.envers.entities.mapper.relation.query.MiddleTableQueryGenerator; +import org.jboss.envers.entities.mapper.relation.component.MiddleDummyComponentMapper; +import org.jboss.envers.entities.mapper.relation.component.MiddleRelatedComponentMapper; +import org.jboss.envers.entities.mapper.relation.component.MiddleSimpleComponentMapper; 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; import org.jboss.envers.tools.StringTools; +import org.jboss.envers.ModificationStore; import org.dom4j.Element; import java.util.*; +import java.util.Set; +import java.util.List; /** * Generates metadata for collection-valued properties. * @author Adam Warski (adam at warski dot org) */ -public class CollectionMetadataGenerator { +public final class CollectionMetadataGenerator { private final VersionsMetadataGenerator mainGenerator; CollectionMetadataGenerator(VersionsMetadataGenerator versionsMetadataGenerator) { mainGenerator = versionsMetadataGenerator; } + void addCollection(String name, Collection value, CompositeMapperBuilder currentMapper, + String entityName, EntityXmlMappingData xmlMappingData) { + Type type = value.getType(); + + if ((type instanceof BagType || type instanceof SetType) && + (value.getElement() instanceof OneToMany)) { + addOneToManyAttached(name, value, currentMapper, entityName); + } else { + addWithMiddleTable(name, value, currentMapper, entityName, xmlMappingData); + } + } + @SuppressWarnings({"unchecked"}) private String getMappedBy(Collection collectionValue) { Iterator assocClassProps = @@ -43,22 +65,37 @@ } @SuppressWarnings({"unchecked"}) - void addOneToManyAttached(String name, Value value, CompositeMapperBuilder mapper, String entityName) { - Collection propertyValue = (Collection) value; + private String getMappedBy(Table collectionTable, PersistentClass referencedClass) { + Iterator properties = referencedClass.getPropertyIterator(); + while (properties.hasNext()) { + Property property = properties.next(); + if (property.getValue() instanceof Collection) { + // The equality is intentional. We want to find a collection property with the same collection table. + //noinspection ObjectEquality + if (((Collection) property.getValue()).getCollectionTable() == collectionTable) { + return property.getName(); + } + } + } - String owningReferencePropertyName = getMappedBy(propertyValue); + return null; + } + + @SuppressWarnings({"unchecked"}) + private void addOneToManyAttached(String name, Collection value, CompositeMapperBuilder mapper, String entityName) { + String owningReferencePropertyName = getMappedBy(value); if (owningReferencePropertyName == null) { - throw new MappingException("Unable to read the mapped by attribute for " + name); + throw new MappingException("Unable to read the mapped by attribute for " + name + " in " + entityName + "!"); } EntityConfiguration configuration = mainGenerator.getEntitiesConfigurations().get(entityName); if (configuration == null) { - throw new MappingException("A versioned relation to a non-versioned entity " + entityName + "!"); + throw new MappingException("Unable to read versioning configuration for " + entityName + "!"); } IdMappingData referencingIdMapping = configuration.getIdMappingData(); - String owningEntityName = ((OneToMany) propertyValue.getElement()).getReferencedEntityName(); + String owningEntityName = ((OneToMany) value.getElement()).getReferencedEntityName(); String lastPropertyPrefix = owningReferencePropertyName + "_"; // Generating the id mapper for the relation @@ -73,15 +110,207 @@ name)); } + private String getReferencedEntityName(Collection value) { + if (value.getElement() instanceof ToOne) { + return ((ToOne) value.getElement()).getReferencedEntityName(); + } else { + return null; + } + } + + @SuppressWarnings({"unchecked"}) + private void addWithMiddleTable(String name, Collection value, CompositeMapperBuilder currentMapper, + String entityName, EntityXmlMappingData xmlMappingData) { + // Generating the name of the middle table, its schema and catalog + // TODO: add support for @VersionsJoinTable + String middleTableName = value.getCollectionTable().getName(); + String middleEntityName = middleTableName; + String versionsMiddleTableName = mainGenerator.getVerEntCfg().getVersionsTableName(null, middleTableName); + String versionsMiddleEntityName = mainGenerator.getVerEntCfg().getVersionsEntityName(middleEntityName); + + String schema = value.getCollectionTable().getSchema(); + String catalog = value.getCollectionTable().getCatalog(); + + // Generating the XML mapping for the middle entity, only if the relation isn't inverse. + // If the relation is inverse, will be later checked by comparing middleEntityXml with null. + Element middleEntityXml; + Element middleEntityXmlId; + Iterator middleEntityOriginalColumns; + if (!value.isInverse()) { + middleEntityXml = MetadataTools.createEntity(xmlMappingData.newAdditionalMapping(), + versionsMiddleEntityName, versionsMiddleTableName, schema, catalog, null); + middleEntityXmlId = middleEntityXml.addElement("composite-id"); + middleEntityOriginalColumns = value.getCollectionTable().getColumnIterator(); + } else { + middleEntityXml = null; + middleEntityXmlId = null; + middleEntityOriginalColumns = null; + } + + // ****** + // Generating the mapping for the referencing entity (it must be an entity). + // ****** + EntityConfiguration referencingConfiguration = mainGenerator.getEntitiesConfigurations().get(entityName); + if (referencingConfiguration == null) { + throw new MappingException("Unable to read versioning configuration for " + entityName + "!"); + } + + // Getting the id-mapping data of the referencing entity (the entity that "owns" this collection). + IdMappingData referencingIdMapping = referencingConfiguration.getIdMappingData(); + + // Null if this collection doesn't reference an entity. + String referencedEntityName = getReferencedEntityName(value); + // Only valid for an inverse relation; null otherwise. + String mappedBy; + + String referencingPrefix; + // Only valid if referencedEntityName isn't null. + String referencedPrefix; + + if (value.isInverse()) { + // If the relation is inverse, then referencedEntityName is not null. + mappedBy = getMappedBy(value.getCollectionTable(), mainGenerator.getCfg().getClassMapping(referencedEntityName)); + if (mappedBy == null) { + throw new MappingException("Unable to read the mapped by attribute for " + name); + } + + referencingPrefix = mappedBy + "_"; + referencedPrefix = StringTools.getLastComponent(referencedEntityName) + "_"; + } else { + mappedBy = null; + + referencingPrefix = StringTools.getLastComponent(entityName) + "_"; + referencedPrefix = name + "_"; + } + + // Storing the id data of the referencing entity: original mapper, prefixed mapper and entity name. + MiddleIdData referencingIdData = new MiddleIdData( + referencingIdMapping.getIdMapper(), + referencingIdMapping.getIdMapper().prefixMappedProperties(referencingPrefix), + entityName, + mainGenerator.getVerEntCfg().getVersionsEntityName(entityName)); + + // 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 + // a query generator to read the raw data collection from the middle table. + QueryGeneratorBuilder queryGeneratorBuilder = new QueryGeneratorBuilder(mainGenerator.getVerEntCfg(), + referencingIdData, versionsMiddleEntityName); + + // Adding the XML mapping for the referencing entity, if the relation isn't inverse. + if (middleEntityXml != null) { + middleEntityXmlId.addAttribute("name", mainGenerator.getVerEntCfg().getOriginalIdPropName()); + + Element properties = (Element) referencingIdMapping.getXmlRelationMapping().clone(); + MetadataTools.prefixNamesInPropertyElement(properties, referencingPrefix, middleEntityOriginalColumns, true); + for (Element idProperty : (java.util.List) properties.elements()) { + middleEntityXmlId.add((Element) idProperty.clone()); + } + + // Adding the revision number as a foreign key to the revision info entity to the composite id of the + // middle table. + mainGenerator.addRevisionInfoRelation(middleEntityXmlId); + + // Adding the revision type property to the entity xml. + mainGenerator.addRevisionType(middleEntityXml); + } + + // ****** + // Generating the element mapping. + // ****** + MiddleComponentData elementComponentData = addValueToMiddleTable(value.getElement(), middleEntityXmlId, + middleEntityOriginalColumns, queryGeneratorBuilder, referencedPrefix); + + // ****** + // Optionally, generating the index mapping. + // ****** + MiddleComponentData indexComponentData; + if (value instanceof IndexedCollection) { + IndexedCollection indexedValue = (IndexedCollection) value; + indexComponentData = null; + // TODO + } else { + // No index - creating a dummy mapper. + indexComponentData = new MiddleComponentData(new MiddleDummyComponentMapper(), + queryGeneratorBuilder.getCurrentIndex()); + } + + // ****** + // Generating the property mapper. + // ****** + + // Building the query generator. + MiddleTableQueryGenerator queryGenerator = queryGeneratorBuilder.build(); + + // Checking the type of the collection and adding an appropriate mapper. + Type type = value.getType(); + if (type instanceof SetType) { + currentMapper.addComposite(name, new BasicCollectionMapper(mainGenerator.getVerEntCfg(), + versionsMiddleEntityName, referencingIdData, HashSet.class, SetProxy.class, queryGenerator, + name, elementComponentData)); + } else if (type instanceof BagType) { + currentMapper.addComposite(name, new BasicCollectionMapper(mainGenerator.getVerEntCfg(), + versionsMiddleEntityName, referencingIdData, ArrayList.class, ListProxy.class, queryGenerator, + name, elementComponentData)); + } else { + throw new RuntimeException(); + } + } + + /** + * + * @param value Value, which should be mapped to the middle-table, either as a relation to another entity, + * or as a simple value. + * @param middleEntityXml If not null, xml mapping for this value is added to this element. + * @param middleEntityOriginalColumns If middleEntityXml is not null, this iterator is used + * to read column names for the generated xml mapping. + * @param queryGeneratorBuilder In case value is a relation to another entity, information about it + * should be added to the given. + * @param prefix Prefix for proeprty names of related entities identifiers. + * @return Data for mapping this component. + */ + private MiddleComponentData addValueToMiddleTable(Value value, Element middleEntityXml, + Iterator middleEntityOriginalColumns, + QueryGeneratorBuilder queryGeneratorBuilder, + String prefix) { + Type type = value.getType(); + if (type instanceof ManyToOneType) { + ToOne toOneValue = (ToOne) value; + String referencedEntityName = toOneValue.getReferencedEntityName(); + IdMappingData referencedIdMapping = mainGenerator.getEntitiesConfigurations().get( + toOneValue.getReferencedEntityName()).getIdMappingData(); + + // Storing the id data of the referenced entity: original mapper, prefixed mapper and entity name. + IdMapper referencedPrefixedIdMapper = referencedIdMapping.getIdMapper().prefixMappedProperties(prefix); + MiddleIdData referencedIdData = new MiddleIdData( + referencedIdMapping.getIdMapper(), + referencedPrefixedIdMapper, + referencedEntityName, + mainGenerator.getVerEntCfg().getVersionsEntityName(referencedEntityName)); + // And adding it to the generator builder. + queryGeneratorBuilder.addRelation(referencedIdData); + + return new MiddleComponentData(new MiddleRelatedComponentMapper(referencedIdData), + queryGeneratorBuilder.getCurrentIndex()); + } else if (type instanceof ImmutableType || type instanceof MutableType) { + // TODO: add support for enums, components, custom types + mainGenerator.getBasicMetadataGenerator().addSimpleValue(middleEntityXml, "element", value, null, ModificationStore.FULL, true); + + return new MiddleComponentData(new MiddleSimpleComponentMapper(mainGenerator.getVerEntCfg(), "element"), + queryGeneratorBuilder.getCurrentIndex()); + } else { + // TODO: throw an exception + throw new RuntimeException(); + } + } + private String getMiddleEntityName(String entityName, String referencedEntityName, ManyToOne mto) { return entityName + "_" + referencedEntityName + "_" + mto.getTable().getName(); } @SuppressWarnings({"unchecked"}) - void addOneToManyDetached(String name, Value value, CompositeMapperBuilder mapper, String entityName, + private void addOneToManyDetached(String name, Collection value, CompositeMapperBuilder mapper, String entityName, EntityXmlMappingData xmlMappingData) { - Collection propertyValue = (Collection) value; - ManyToOne mto = (ManyToOne) propertyValue.getElement(); + ManyToOne mto = (ManyToOne) value.getElement(); String referencedEntityName = mto.getReferencedEntityName(); @@ -141,27 +370,9 @@ mainGenerator.getEntitiesConfigurations().get(referencedEntityName).getIdMapper())); } - @SuppressWarnings({"unchecked"}) - private String getMappedBy(Table collectionTable, PersistentClass referencedClass) { - Iterator properties = referencedClass.getPropertyIterator(); - while (properties.hasNext()) { - Property property = properties.next(); - if (property.getValue() instanceof Collection) { - // The equality is intentional. We want to find a collection property with the same collection table. - //noinspection ObjectEquality - if (((Collection) property.getValue()).getCollectionTable() == collectionTable) { - return property.getName(); - } - } - } + private void addManyToManyNotOwning(String name, Collection value, CompositeMapperBuilder mapper, String entityName) { + ManyToOne mto = (ManyToOne) value.getElement(); - return null; - } - - void addManyToManyNotOwning(String name, Value value, CompositeMapperBuilder mapper, String entityName) { - Collection propertyValue = (Collection) value; - ManyToOne mto = (ManyToOne) propertyValue.getElement(); - String referencedEntityName = mto.getReferencedEntityName(); EntityConfiguration configuration = mainGenerator.getEntitiesConfigurations().get(entityName); @@ -174,7 +385,7 @@ throw new MappingException("A versioned relation to a non-versioned entity " + referencedEntityName + "!"); } - String mappedBy = getMappedBy(propertyValue.getCollectionTable(), mainGenerator.getCfg().getClassMapping(referencedEntityName)); + String mappedBy = getMappedBy(value.getCollectionTable(), mainGenerator.getCfg().getClassMapping(referencedEntityName)); if (mappedBy == null) { throw new MappingException("Unable to read the mapped by attribute for " + name); } Deleted: trunk/src/main/org/jboss/envers/configuration/metadata/EntityMappingData.java =================================================================== --- trunk/src/main/org/jboss/envers/configuration/metadata/EntityMappingData.java 2008-09-17 15:38:55 UTC (rev 150) +++ trunk/src/main/org/jboss/envers/configuration/metadata/EntityMappingData.java 2008-09-19 14:51:22 UTC (rev 151) @@ -1,58 +0,0 @@ -/* - * JBoss, Home of Professional Open Source - * - * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated - * by the @authors tag. All rights reserved. - * - * See the copyright.txt in the distribution for a full listing of individual - * contributors. 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.configuration.metadata; - -import org.dom4j.Document; -import org.dom4j.DocumentHelper; - -import java.util.List; -import java.util.ArrayList; - -/** - * @author Adam Warski (adam at warski dot org) - */ -public class EntityMappingData { - private Document mainMapping; - private List additionalMappings; - - public EntityMappingData() { - mainMapping = DocumentHelper.createDocument(); - additionalMappings = new ArrayList(); - } - - public Document getMainMapping() { - return mainMapping; - } - - public List getAdditionalMappings() { - return additionalMappings; - } - - public Document newAdditionalMapping() { - Document additionalMapping = DocumentHelper.createDocument(); - additionalMappings.add(additionalMapping); - - return additionalMapping; - } -} Modified: trunk/src/main/org/jboss/envers/configuration/metadata/IdMetadataGenerator.java =================================================================== --- trunk/src/main/org/jboss/envers/configuration/metadata/IdMetadataGenerator.java 2008-09-17 15:38:55 UTC (rev 150) +++ trunk/src/main/org/jboss/envers/configuration/metadata/IdMetadataGenerator.java 2008-09-19 14:51:22 UTC (rev 151) @@ -22,7 +22,7 @@ * Generates metadata for primary identifiers (ids) of versions entities. * @author Adam Warski (adam at warski dot org) */ -public class IdMetadataGenerator { +public final class IdMetadataGenerator { private final VersionsMetadataGenerator mainGenerator; IdMetadataGenerator(VersionsMetadataGenerator versionsMetadataGenerator) { Added: trunk/src/main/org/jboss/envers/configuration/metadata/QueryGeneratorBuilder.java =================================================================== --- trunk/src/main/org/jboss/envers/configuration/metadata/QueryGeneratorBuilder.java (rev 0) +++ trunk/src/main/org/jboss/envers/configuration/metadata/QueryGeneratorBuilder.java 2008-09-19 14:51:22 UTC (rev 151) @@ -0,0 +1,58 @@ +package org.jboss.envers.configuration.metadata; + +import org.jboss.envers.entities.mapper.relation.MiddleIdData; +import org.jboss.envers.entities.mapper.relation.query.MiddleTableQueryGenerator; +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; +import org.jboss.envers.configuration.VersionsEntitiesConfiguration; + +import java.util.List; +import java.util.ArrayList; + +/** + * Builds query generators, for reading collection middle tables, along with any related entities. + * The related entities information can be added gradually, and when complete, the query generator can be built. + * @author Adam Warski (adam at warski dot org) + */ +public final class QueryGeneratorBuilder { + private final VersionsEntitiesConfiguration verEntCfg; + private final MiddleIdData referencingIdData; + private final String versionsMiddleEntityName; + private final List idDatas; + + QueryGeneratorBuilder(VersionsEntitiesConfiguration verEntCfg, MiddleIdData referencingIdData, + String versionsMiddleEntityName) { + this.verEntCfg = verEntCfg; + this.referencingIdData = referencingIdData; + this.versionsMiddleEntityName = versionsMiddleEntityName; + + idDatas = new ArrayList(); + } + + void addRelation(MiddleIdData idData) { + idDatas.add(idData); + } + + MiddleTableQueryGenerator build() { + if (idDatas.size() == 0) { + return new OneEntityQueryGenerator(verEntCfg, versionsMiddleEntityName, referencingIdData); + } else if (idDatas.size() == 1) { + return new TwoEntityQueryGenerator(verEntCfg, versionsMiddleEntityName, referencingIdData, + idDatas.get(0)); + } else if (idDatas.size() == 1) { + return new ThreeEntityQueryGenerator(verEntCfg, versionsMiddleEntityName, referencingIdData, + idDatas.get(0), idDatas.get(1)); + } else { + throw new IllegalStateException("Illegal number of related entities."); + } + } + + /** + * @return Current index of data in the array, which will be the element of a list, returned when executing a query + * generated by the built query generator. + */ + int getCurrentIndex() { + return idDatas.size(); + } +} Modified: trunk/src/main/org/jboss/envers/configuration/metadata/ToOneRelationMetadataGenerator.java =================================================================== --- trunk/src/main/org/jboss/envers/configuration/metadata/ToOneRelationMetadataGenerator.java 2008-09-17 15:38:55 UTC (rev 150) +++ trunk/src/main/org/jboss/envers/configuration/metadata/ToOneRelationMetadataGenerator.java 2008-09-19 14:51:22 UTC (rev 151) @@ -16,7 +16,7 @@ * Generates metadata for to-one relations (reference-valued properties). * @author Adam Warski (adam at warski dot org) */ -public class ToOneRelationMetadataGenerator { +public final class ToOneRelationMetadataGenerator { private final VersionsMetadataGenerator mainGenerator; ToOneRelationMetadataGenerator(VersionsMetadataGenerator versionsMetadataGenerator) { Modified: trunk/src/main/org/jboss/envers/configuration/metadata/VersionsMetadataGenerator.java =================================================================== --- trunk/src/main/org/jboss/envers/configuration/metadata/VersionsMetadataGenerator.java 2008-09-17 15:38:55 UTC (rev 150) +++ trunk/src/main/org/jboss/envers/configuration/metadata/VersionsMetadataGenerator.java 2008-09-19 14:51:22 UTC (rev 151) @@ -46,7 +46,7 @@ * @author Adam Warski (adam at warski dot org) * @author Sebastian Komander */ -public class VersionsMetadataGenerator { +public final class VersionsMetadataGenerator { private final Configuration cfg; private final GlobalConfiguration globalCfg; private final VersionsEntitiesConfiguration verEntCfg; @@ -57,10 +57,10 @@ private final CollectionMetadataGenerator collectionMetadataGenerator; private final ToOneRelationMetadataGenerator toOneRelationMetadataGenerator; - private Map entitiesConfigurations; + private final Map entitiesConfigurations; // Map entity name -> (join descriptor -> element describing the "versioned" join) - private Map> entitiesJoins; + private final Map> entitiesJoins; private YLog log = YLogManager.getLogManager().getLog(VersionsMetadataGenerator.class); @@ -119,8 +119,8 @@ @SuppressWarnings({"unchecked"}) void addValue(Element parent, String name, Value value, CompositeMapperBuilder currentMapper, - ModificationStore store, String entityName, EntityXmlMappingData xmlMappingData, - boolean firstPass) { + ModificationStore store, String entityName, EntityXmlMappingData xmlMappingData, + boolean firstPass) { Type type = value.getType(); if (type instanceof ComponentType) { @@ -139,48 +139,34 @@ if (firstPass) { basicMetadataGenerator.addEnumValue(parent, name, value, currentMapper, store); } - } else if (type instanceof ManyToOneType) { - // only second pass - if (!firstPass) { - toOneRelationMetadataGenerator.addToOne(parent, name, value, currentMapper, entityName); - } - } else if (type instanceof OneToOneType) { - // only second pass - if (!firstPass) { - toOneRelationMetadataGenerator.addOneToOneNotOwning(name, value, currentMapper, entityName); - } - } else if ("org.hibernate.type.PrimitiveByteArrayBlobType".equals( - type.getClass().getName())) { + } else if (type instanceof CustomType && + ("org.hibernate.type.PrimitiveCharacterArrayClobType".equals(type.getName()) || + "org.hibernate.type.StringClobType".equals(type.getName()))) { // only first pass if (firstPass) { basicMetadataGenerator.addSimpleValue(parent, name, value, currentMapper, store, false); } - } else if (type instanceof CustomType && - ("org.hibernate.type.PrimitiveCharacterArrayClobType".equals(type.getName()) || - "org.hibernate.type.StringClobType".equals(type.getName()))) { + } else if ("org.hibernate.type.PrimitiveByteArrayBlobType".equals( + type.getClass().getName())) { // only first pass if (firstPass) { basicMetadataGenerator.addSimpleValue(parent, name, value, currentMapper, store, false); } - } else if ((type instanceof BagType || type instanceof SetType) && - ((((Collection) value).getElement() instanceof OneToMany))) { + } else if (type instanceof ManyToOneType) { // only second pass if (!firstPass) { - collectionMetadataGenerator.addOneToManyAttached(name, value, currentMapper, entityName); + toOneRelationMetadataGenerator.addToOne(parent, name, value, currentMapper, entityName); } - } else if ((type instanceof BagType || type instanceof SetType) && - ((((Collection) value).getElement() instanceof ManyToOne)) && - !((Collection) value).isInverse()) { + } else if (type instanceof OneToOneType) { // only second pass if (!firstPass) { - collectionMetadataGenerator.addOneToManyDetached(name, value, currentMapper, entityName, xmlMappingData); + toOneRelationMetadataGenerator.addOneToOneNotOwning(name, value, currentMapper, entityName); } - } else if ((type instanceof BagType || type instanceof SetType) && - ((((Collection) value).getElement() instanceof ManyToOne)) && - ((Collection) value).isInverse()) { + } else if (type instanceof CollectionType) { // only second pass if (!firstPass) { - collectionMetadataGenerator.addManyToManyNotOwning(name, value, currentMapper, entityName); + collectionMetadataGenerator.addCollection(name, (Collection) value, currentMapper, entityName, + xmlMappingData); } } else { String message = "Type not supported for versioning: " + type.getClass().getName() + @@ -395,26 +381,10 @@ return basicMetadataGenerator; } - IdMetadataGenerator getIdMetadataGenerator() { - return idMetadataGenerator; - } - - CollectionMetadataGenerator getCollectionMetadataGenerator() { - return collectionMetadataGenerator; - } - - ToOneRelationMetadataGenerator getToOneRelationMetadataGenerator() { - return toOneRelationMetadataGenerator; - } - Configuration getCfg() { return cfg; } - GlobalConfiguration getGlobalCfg() { - return globalCfg; - } - VersionsEntitiesConfiguration getVerEntCfg() { return verEntCfg; } Modified: trunk/src/main/org/jboss/envers/entities/mapper/PropertyMapper.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/PropertyMapper.java 2008-09-17 15:38:55 UTC (rev 150) +++ trunk/src/main/org/jboss/envers/entities/mapper/PropertyMapper.java 2008-09-19 14:51:22 UTC (rev 151) @@ -40,7 +40,7 @@ * @param oldObj Old state of the entity. * @return True if there are any differences between the states represented by newObj and oldObj. */ - public boolean mapToMapFromEntity(Map data, Object newObj, Object oldObj); + boolean mapToMapFromEntity(Map data, Object newObj, Object oldObj); /** * Maps properties from the given map to the given object. @@ -51,8 +51,8 @@ * @param versionsReader VersionsReader for reading relations * @param revision Revision at which the object is read, for reading relations */ - public void mapToEntityFromMap(VersionsConfiguration verCfg, Object obj, Map data, Object primaryKey, - VersionsReaderImplementor versionsReader, Number revision); + void mapToEntityFromMap(VersionsConfiguration verCfg, Object obj, Map data, Object primaryKey, + VersionsReaderImplementor versionsReader, Number revision); /** * Maps collection changes @@ -62,7 +62,7 @@ * @param id Id of the object owning the collection. * @return List of changes that need to be performed on the persistent store. */ - public List mapCollectionChanges(String referencingPropertyName, - PersistentCollection newColl, - Serializable oldColl, Serializable id); + List mapCollectionChanges(String referencingPropertyName, + PersistentCollection newColl, + Serializable oldColl, Serializable id); } Added: trunk/src/main/org/jboss/envers/entities/mapper/relation/AbstractCollectionMapper.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/AbstractCollectionMapper.java (rev 0) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/AbstractCollectionMapper.java 2008-09-19 14:51:22 UTC (rev 151) @@ -0,0 +1,90 @@ +package org.jboss.envers.entities.mapper.relation; + +import org.jboss.envers.entities.mapper.PersistentCollectionChangeData; +import org.jboss.envers.entities.mapper.PropertyMapper; +import org.jboss.envers.RevisionType; +import org.jboss.envers.configuration.VersionsEntitiesConfiguration; +import org.hibernate.collection.PersistentCollection; + +import java.util.*; +import java.io.Serializable; + +/** + * @author Adam Warski (adam at warski dot org) + */ +public abstract class AbstractCollectionMapper implements PropertyMapper { + private final VersionsEntitiesConfiguration verEntCfg; + private final String versionsMiddleEntityName; + private final String collectionReferencingPropertyName; + private final MiddleIdData referencingIdData; + + protected AbstractCollectionMapper(VersionsEntitiesConfiguration verEntCfg, String versionsMiddleEntityName, + String collectionReferencingPropertyName, MiddleIdData referencingIdData) { + this.verEntCfg = verEntCfg; + this.versionsMiddleEntityName = versionsMiddleEntityName; + this.collectionReferencingPropertyName = collectionReferencingPropertyName; + this.referencingIdData = referencingIdData; + } + + protected abstract Collection getNewCollectionContent(PersistentCollection newCollection); + protected abstract Collection getOldCollectionContent(Serializable oldCollection); + + /** + * Maps the changed collection element to the given map. + * @param data Where to map the data. + * @param changed The changed collection element to map. + */ + protected abstract void mapToMapFromObject(Map data, Object changed); + + private void addCollectionChanges(List collectionChanges, Set changed, + RevisionType revisionType, Serializable id) { + for (Object changedObj : changed) { + Map entityData = new HashMap(); + Map originalId = new HashMap(); + entityData.put(verEntCfg.getOriginalIdPropName(), originalId); + + collectionChanges.add(new PersistentCollectionChangeData(versionsMiddleEntityName, entityData, changedObj)); + // Mapping the collection owner's id. + referencingIdData.getPrefixedMapper().mapToMapFromId(originalId, id); + + // Mapping collection element and index (if present). + mapToMapFromObject(originalId, changedObj); + + entityData.put(verEntCfg.getRevisionTypePropName(), revisionType); + } + } + + @SuppressWarnings({"unchecked"}) + public List mapCollectionChanges(String referencingPropertyName, + PersistentCollection newColl, + Serializable oldColl, Serializable id) { + if (!collectionReferencingPropertyName.equals(referencingPropertyName)) { + return null; + } + + List collectionChanges = new ArrayList(); + + // Comparing new and old collection content. + Collection newCollection = getNewCollectionContent(newColl); + Collection oldCollection = getOldCollectionContent(oldColl); + + Set added = new HashSet(); + if (newColl != null) { added.addAll(newCollection); } + if (oldColl != null) { added.removeAll(oldCollection); } + + addCollectionChanges(collectionChanges, added, RevisionType.ADD, id); + + Set deleted = new HashSet(); + if (oldColl != null) { deleted.addAll(oldCollection); } + if (newColl != null) { deleted.removeAll(newCollection); } + + addCollectionChanges(collectionChanges, deleted, RevisionType.DEL, id); + + return collectionChanges; + } + + public boolean mapToMapFromEntity(Map data, Object newObj, Object oldObj) { + // Changes are mapped in the "mapCollectionChanges" method. + return false; + } +} Modified: trunk/src/main/org/jboss/envers/entities/mapper/relation/AbstractOneToManyMapper.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/AbstractOneToManyMapper.java 2008-09-17 15:38:55 UTC (rev 150) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/AbstractOneToManyMapper.java 2008-09-19 14:51:22 UTC (rev 151) @@ -27,7 +27,7 @@ import org.jboss.envers.tools.reflection.ReflectionTools; 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.Initializor; +import org.jboss.envers.entities.mapper.relation.lazy.initializor.Initializor; import org.jboss.envers.entities.mapper.relation.lazy.proxy.MapProxy; import org.jboss.envers.entities.mapper.PropertyMapper; import org.jboss.envers.exception.VersionsException; Added: trunk/src/main/org/jboss/envers/entities/mapper/relation/BasicCollectionMapper.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/BasicCollectionMapper.java (rev 0) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/BasicCollectionMapper.java 2008-09-19 14:51:22 UTC (rev 151) @@ -0,0 +1,86 @@ +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.entities.mapper.relation.query.MiddleTableQueryGenerator; +import org.jboss.envers.entities.mapper.relation.lazy.initializor.Initializor; +import org.jboss.envers.entities.mapper.relation.lazy.initializor.BasicCollectionInitializor; +import org.jboss.envers.configuration.VersionsConfiguration; +import org.jboss.envers.configuration.VersionsEntitiesConfiguration; +import org.jboss.envers.reader.VersionsReaderImplementor; +import org.jboss.envers.tools.reflection.ReflectionTools; +import org.jboss.envers.exception.VersionsException; +import org.hibernate.collection.PersistentCollection; +import org.hibernate.property.Setter; + +import java.util.Map; +import java.util.List; +import java.util.Collection; +import java.io.Serializable; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; + +/** + * @author Adam Warski (adam at warski dot org) + */ +public final class BasicCollectionMapper extends AbstractCollectionMapper implements PropertyMapper { + private final Class collectionClass; + private final MiddleTableQueryGenerator queryGenerator; + private final String propertyName; + private final MiddleComponentData elementComponentData; + + private final Constructor proxyConstructor; + + public BasicCollectionMapper(VersionsEntitiesConfiguration verEntCfg, String versionsMiddleEntityName, + MiddleIdData referencingIdData, + Class collectionClass, Class proxyClass, + MiddleTableQueryGenerator queryGenerator, + String propertyName, MiddleComponentData elementComponentData) { + super(verEntCfg, versionsMiddleEntityName, propertyName, referencingIdData); + this.collectionClass = collectionClass; + this.queryGenerator = queryGenerator; + this.propertyName = propertyName; + this.elementComponentData = elementComponentData; + + try { + proxyConstructor = proxyClass.getConstructor(Initializor.class); + } catch (NoSuchMethodException e) { + throw new VersionsException(e); + } + } + + public void mapToEntityFromMap(VersionsConfiguration verCfg, Object obj, Map data, Object primaryKey, + VersionsReaderImplementor versionsReader, Number revision) { + // Creating the initializator and passing to the proxy, which will use it to initialize the collection + // when and if it is accessed. + Initializor collectionInitializor = new BasicCollectionInitializor(verCfg, + versionsReader, queryGenerator, primaryKey, revision, collectionClass, elementComponentData); + + Setter setter = ReflectionTools.getSetter(obj.getClass(), propertyName); + try { + setter.set(obj, proxyConstructor.newInstance(collectionInitializor), null); + } catch (InstantiationException e) { + throw new VersionsException(e); + } catch (IllegalAccessException e) { + throw new VersionsException(e); + } catch (InvocationTargetException e) { + throw new VersionsException(e); + } + } + + protected Collection getNewCollectionContent(PersistentCollection newCollection) { + return (Collection) newCollection; + } + + protected Collection getOldCollectionContent(Serializable oldCollection) { + if (oldCollection instanceof Map) { + return ((Map) oldCollection).keySet(); + } else { + return (Collection) oldCollection; + } + } + + protected void mapToMapFromObject(Map data, Object changed) { + elementComponentData.getComponentMapper().mapToMapFromObject(data, changed); + } +} Deleted: trunk/src/main/org/jboss/envers/entities/mapper/relation/DetachedRelationQueryGenerator.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/DetachedRelationQueryGenerator.java 2008-09-17 15:38:55 UTC (rev 150) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/DetachedRelationQueryGenerator.java 2008-09-19 14:51:22 UTC (rev 151) @@ -1,116 +0,0 @@ -/* - * JBoss, Home of Professional Open Source - * - * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated - * by the @authors tag. All rights reserved. - * - * See the copyright.txt in the distribution for a full listing of individual - * contributors. 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.id.QueryParameterData; -import org.jboss.envers.entities.mapper.id.IdMapper; -import org.jboss.envers.configuration.VersionsEntitiesConfiguration; -import org.jboss.envers.RevisionType; -import org.jboss.envers.reader.VersionsReaderImplementor; -import org.hibernate.Query; - -/** - * @author Adam Warski (adam at warski dot org) - */ -public class DetachedRelationQueryGenerator { - private final String queryString; - private final IdMapper referencingMiddleIdMapper; - - public DetachedRelationQueryGenerator(VersionsEntitiesConfiguration verEntCfg, - String versionsReferencedEntityName, String versionsMiddleEntityName, - IdMapper referencingMiddleIdMapper, IdMapper referencedMiddleIdMapper, - IdMapper referencedIdMapper) { - this.referencingMiddleIdMapper = referencingMiddleIdMapper; - - /* - * The query that we need to create: - * SELECT e FROM versionsReferencedEntity e, middleEntity ee - * WHERE - * (selecting e entities at revision :revision) - * e.revision = (SELECT max(e2.revision) FROM referencedEntity e2 - * WHERE e2.revision <= :revision AND e2.id1 = e.id1) AND - * (only entities referenced by the association) - * ee.id1 = e.id1 AND - * ee.id2 = :id2 AND - * (the association at revision :revision) - * ee.revision = (SELECT max(ee2.revision) FROM middleEntity ee2 - * WHERE ee2.revision <= :revision AND ee2.id2 = :id2 AND ee2.id1 = ee.id1) AND - * (only non-deleted entities and associations) - * ee.revision_type != DEL AND - * e.revision_type != DEL - */ - String revisionPropertyPath = verEntCfg.getRevisionPropPath(); - String originalIdPropertyName = verEntCfg.getOriginalIdPropName(); - - StringBuilder querySB = new StringBuilder(); - querySB - .append("SELECT e FROM ").append(versionsReferencedEntityName).append(" e") - .append(", ").append(versionsMiddleEntityName).append(" ee ") - .append("WHERE ") - // e.revision = (SELECT max(e2.revision) ... - .append("e.").append(revisionPropertyPath).append(" = (SELECT max(e2.").append(revisionPropertyPath) - .append(") FROM ").append(versionsReferencedEntityName).append(" e2 ") - // e2.revision <= :revision - .append("WHERE e2.").append(revisionPropertyPath).append(" <= :revision AND ") - // e2.id1 = e.id1) - .append(referencedIdMapper.getIdsEqualQuery("e." + originalIdPropertyName, "e2." + originalIdPropertyName)) - .append(") AND ") - // ee.id1 = e.id1 - .append(referencedMiddleIdMapper.getIdsEqualQuery("ee." + originalIdPropertyName, - referencedIdMapper, "e." + originalIdPropertyName)) - .append(" AND ") - // ee.id2 = :id2 - .append(referencingMiddleIdMapper.getIdEqualsQuery("ee." + originalIdPropertyName, true)) - .append(" AND ") - // ee.revision = (SELECT max(ee2.revision) ... - .append("ee.").append(revisionPropertyPath).append(" = (SELECT max(ee2.").append(revisionPropertyPath) - .append(") FROM ").append(versionsMiddleEntityName).append(" ee2 ") - // ee2.revision <= :revision - .append("WHERE ee2.").append(revisionPropertyPath).append(" <= :revision AND ") - // ee2.id2 = :id2) - .append(referencingMiddleIdMapper.getIdEqualsQuery("ee2." + originalIdPropertyName, true)) - .append(" AND ") - // ee2.id1 = ee.id1) - .append(referencedMiddleIdMapper.getIdsEqualQuery("ee." + originalIdPropertyName, "ee2." + originalIdPropertyName)) - .append(") AND ") - // e.revision_type != DEL AND - .append("e.").append(verEntCfg.getRevisionTypePropName()).append(" != ").append(":delrevisiontype") - .append(" AND ") - // ee.revision_type != DEL - .append("ee.").append(verEntCfg.getRevisionTypePropName()).append(" != ").append(":delrevisiontype"); - - queryString = querySB.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: referencingMiddleIdMapper.mapToQueryParametersFromId(primaryKey)) { - paramData.setParameterValue(query); - } - - return query; - } -} Modified: trunk/src/main/org/jboss/envers/entities/mapper/relation/ManyToManyNotOwningMapper.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/ManyToManyNotOwningMapper.java 2008-09-17 15:38:55 UTC (rev 150) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/ManyToManyNotOwningMapper.java 2008-09-19 14:51:22 UTC (rev 151) @@ -25,8 +25,8 @@ import org.jboss.envers.entities.mapper.PropertyMapper; import org.jboss.envers.entities.mapper.PersistentCollectionChangeData; -import org.jboss.envers.entities.mapper.relation.lazy.proxy.Initializor; import org.jboss.envers.entities.mapper.relation.lazy.DetachedRelationInitializor; +import org.jboss.envers.entities.mapper.relation.lazy.initializor.Initializor; import org.jboss.envers.entities.mapper.id.IdMapper; import org.jboss.envers.reader.VersionsReaderImplementor; import org.jboss.envers.configuration.VersionsEntitiesConfiguration; @@ -45,7 +45,7 @@ */ private final String referencedEntityName; - private final DetachedRelationQueryGenerator queryGenerator; + // private final DetachedRelationQueryGenerator queryGenerator; public ManyToManyNotOwningMapper(VersionsEntitiesConfiguration verEntCfg, String referencingEntityName, String referencedEntityName, String collectionReferencingPropertyName, @@ -56,8 +56,8 @@ this.referencedEntityName = referencedEntityName; - queryGenerator = new DetachedRelationQueryGenerator(verEntCfg, versionsReferencedEntityName, - versionsMiddleEntityName, referencingMiddleIdMapper, referencedMiddleIdMapper, referencedIdMapper); + // queryGenerator = new DetachedRelationQueryGenerator(verEntCfg, versionsReferencedEntityName, + // versionsMiddleEntityName, referencingMiddleIdMapper, referencedMiddleIdMapper, referencedIdMapper); } public List mapCollectionChanges(String referencingPropertyName, @@ -72,11 +72,11 @@ return false; } - protected Initializor getInitializator(VersionsConfiguration verCfg, + protected Initializor getInitializator(VersionsConfiguration verCfg, VersionsReaderImplementor versionsReader, Class entityClass, Object primaryKey, Number revision, Class collectionClass) { - return new DetachedRelationInitializor(verCfg, referencedEntityName, queryGenerator, - versionsReader, primaryKey, revision, collectionClass); + return null; //new DetachedRelationInitializor(verCfg, referencedEntityName, queryGenerator, + // versionsReader, primaryKey, revision, collectionClass); } } \ No newline at end of file Added: trunk/src/main/org/jboss/envers/entities/mapper/relation/MiddleComponentData.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/MiddleComponentData.java (rev 0) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/MiddleComponentData.java 2008-09-19 14:51:22 UTC (rev 151) @@ -0,0 +1,28 @@ +package org.jboss.envers.entities.mapper.relation; + +import org.jboss.envers.entities.mapper.relation.component.MiddleComponentMapper; + +/** + * A data holder for a middle relation component (which is either the collection element or index): + * - component mapper used to map the component to and from versions entities + * - an index, which specifies in which element of the array returned by the query for reading the collection the data + * of the component is + * @author Adam Warski (adam at warski dot org) + */ +public final class MiddleComponentData { + private final MiddleComponentMapper componentMapper; + private final int componentIndex; + + public MiddleComponentData(MiddleComponentMapper componentMapper, int componentIndex) { + this.componentMapper = componentMapper; + this.componentIndex = componentIndex; + } + + public MiddleComponentMapper getComponentMapper() { + return componentMapper; + } + + public int getComponentIndex() { + return componentIndex; + } +} Added: trunk/src/main/org/jboss/envers/entities/mapper/relation/MiddleIdData.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/MiddleIdData.java (rev 0) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/MiddleIdData.java 2008-09-19 14:51:22 UTC (rev 151) @@ -0,0 +1,38 @@ +package org.jboss.envers.entities.mapper.relation; + +import org.jboss.envers.entities.mapper.id.IdMapper; + +/** + * A class holding information about ids, which form a virtual "relation" from a middle-table. Middle-tables are used + * when mapping collections. + * @author Adam Warski (adam at warski dot org) + */ +public final class MiddleIdData { + private final IdMapper originalMapper; + private final IdMapper prefixedMapper; + private final String entityName; + private final String versionsEntityName; + + public MiddleIdData(IdMapper originalMapper, IdMapper prefixedMapper, String entityName, String versionsEntityName) { + this.originalMapper = originalMapper; + this.prefixedMapper = prefixedMapper; + this.entityName = entityName; + this.versionsEntityName = versionsEntityName; + } + + public IdMapper getOriginalMapper() { + return originalMapper; + } + + public IdMapper getPrefixedMapper() { + return prefixedMapper; + } + + public String getEntityName() { + return entityName; + } + + public String getVersionsEntityName() { + return versionsEntityName; + } +} Modified: trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyAttachedMapper.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyAttachedMapper.java 2008-09-17 15:38:55 UTC (rev 150) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyAttachedMapper.java 2008-09-19 14:51:22 UTC (rev 151) @@ -25,7 +25,7 @@ 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.Initializor; +import org.jboss.envers.entities.mapper.relation.lazy.initializor.Initializor; import org.jboss.envers.configuration.VersionsConfiguration; import org.hibernate.collection.PersistentCollection; @@ -48,7 +48,7 @@ return false; } - protected Initializor getInitializator(VersionsConfiguration verCfg, + protected Initializor getInitializator(VersionsConfiguration verCfg, VersionsReaderImplementor versionsReader, Class entityClass, Object primaryKey, Number revision, Modified: trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyDetachedMapper.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyDetachedMapper.java 2008-09-17 15:38:55 UTC (rev 150) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyDetachedMapper.java 2008-09-19 14:51:22 UTC (rev 151) @@ -25,8 +25,8 @@ import org.jboss.envers.entities.mapper.PropertyMapper; import org.jboss.envers.entities.mapper.PersistentCollectionChangeData; -import org.jboss.envers.entities.mapper.relation.lazy.proxy.Initializor; import org.jboss.envers.entities.mapper.relation.lazy.DetachedRelationInitializor; +import org.jboss.envers.entities.mapper.relation.lazy.initializor.Initializor; import org.jboss.envers.entities.mapper.id.IdMapper; import org.jboss.envers.reader.VersionsReaderImplementor; import org.jboss.envers.configuration.VersionsEntitiesConfiguration; @@ -57,7 +57,7 @@ private final IdMapper referencingMiddleIdMapper; private final IdMapper referencedMiddleIdMapper; - private final DetachedRelationQueryGenerator queryGenerator; + //private final DetachedRelationQueryGenerator queryGenerator; public OneToManyDetachedMapper(VersionsEntitiesConfiguration verEntCfg, String referencingEntityName, String referencedEntityName, String collectionReferencingPropertyName, @@ -73,8 +73,8 @@ this.referencingMiddleIdMapper = referencingMiddleIdMapper; this.referencedMiddleIdMapper = referencedMiddleIdMapper; - queryGenerator = new DetachedRelationQueryGenerator(verEntCfg, versionsReferencedEntityName, - versionsMiddleEntityName, referencingMiddleIdMapper, referencedMiddleIdMapper, referencedIdMapper); + //queryGenerator = new DetachedRelationQueryGenerator(verEntCfg, versionsReferencedEntityName, + // versionsMiddleEntityName, referencingMiddleIdMapper, referencedMiddleIdMapper, referencedIdMapper); } private Collection getOldCollection(Serializable oldColl) { @@ -138,7 +138,7 @@ VersionsReaderImplementor versionsReader, Class entityClass, Object primaryKey, Number revision, Class collectionClass) { - return new DetachedRelationInitializor(verCfg, referencedEntityName, queryGenerator, - versionsReader, primaryKey, revision, collectionClass); + return null;// new DetachedRelationInitializor(verCfg, referencedEntityName, queryGenerator, + //versionsReader, primaryKey, revision, collectionClass); } } Added: trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleComponentMapper.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleComponentMapper.java (rev 0) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleComponentMapper.java 2008-09-19 14:51:22 UTC (rev 151) @@ -0,0 +1,27 @@ +package org.jboss.envers.entities.mapper.relation.component; + +import org.jboss.envers.entities.EntityInstantiator; + +import java.util.Map; + +/** + * @author Adam Warski (adam at warski dot org) + */ +public interface MiddleComponentMapper { + /** + * Maps from full object data, contained in the given map, to an object. + * @param entityInstantiator An entity instatiator bound with an open versions reader. + * @param data Full object data. + * @param revision Revision at which the data is read. + * @return An object with data corresponding to the one found in the given map. + */ + Object mapToObjectFromFullMap(EntityInstantiator entityInstantiator, Map data, + Number revision); + + /** + * Maps from an object to the object's representation (for an entity - only its id). + * @param data Map to which data should be added. + * @param obj Object to map from. + */ + void mapToMapFromObject(Map data, Object obj); +} Added: trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleDummyComponentMapper.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleDummyComponentMapper.java (rev 0) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleDummyComponentMapper.java 2008-09-19 14:51:22 UTC (rev 151) @@ -0,0 +1,18 @@ +package org.jboss.envers.entities.mapper.relation.component; + +import org.jboss.envers.entities.EntityInstantiator; +import org.jboss.envers.entities.mapper.relation.component.MiddleComponentMapper; + +import java.util.Map; + +/** + * @author Adam Warski (adam at warski dot org) + */ +public final class MiddleDummyComponentMapper implements MiddleComponentMapper { + public Object mapToObjectFromFullMap(EntityInstantiator entityInstantiator, Map data, Number revision) { + return null; + } + + public void mapToMapFromObject(Map data, Object obj) { + } +} \ No newline at end of file Added: trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleMapKeyComponentMapper.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleMapKeyComponentMapper.java (rev 0) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleMapKeyComponentMapper.java 2008-09-19 14:51:22 UTC (rev 151) @@ -0,0 +1,29 @@ +package org.jboss.envers.entities.mapper.relation.component; + +import org.jboss.envers.entities.EntityInstantiator; +import org.jboss.envers.entities.mapper.relation.MiddleIdData; + +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 mapToMapFromObject 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 data, + Number revision) { + return relatedIdData.getOriginalMapper().mapToIdFromMap(data); + } + + public void mapToMapFromObject(Map data, Object obj) { + // Doing nothing. + } +} \ No newline at end of file Added: trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleRelatedComponentMapper.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleRelatedComponentMapper.java (rev 0) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleRelatedComponentMapper.java 2008-09-19 14:51:22 UTC (rev 151) @@ -0,0 +1,27 @@ +package org.jboss.envers.entities.mapper.relation.component; + +import org.jboss.envers.entities.EntityInstantiator; +import org.jboss.envers.entities.mapper.relation.component.MiddleComponentMapper; +import org.jboss.envers.entities.mapper.relation.MiddleIdData; + +import java.util.Map; + +/** + * @author Adam Warski (adam at warski dot org) + */ +public final class MiddleRelatedComponentMapper implements MiddleComponentMapper { + private final MiddleIdData relatedIdData; + + public MiddleRelatedComponentMapper(MiddleIdData relatedIdData) { + this.relatedIdData = relatedIdData; + } + + public Object mapToObjectFromFullMap(EntityInstantiator entityInstantiator, Map data, + Number revision) { + return entityInstantiator.createInstanceFromVersionsEntity(relatedIdData.getEntityName(), data, revision); + } + + public void mapToMapFromObject(Map data, Object obj) { + relatedIdData.getOriginalMapper().mapToMapFromEntity(data, obj); + } +} Added: trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleSimpleComponentMapper.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleSimpleComponentMapper.java (rev 0) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleSimpleComponentMapper.java 2008-09-19 14:51:22 UTC (rev 151) @@ -0,0 +1,29 @@ +package org.jboss.envers.entities.mapper.relation.component; + +import org.jboss.envers.entities.EntityInstantiator; +import org.jboss.envers.entities.mapper.relation.component.MiddleComponentMapper; +import org.jboss.envers.configuration.VersionsEntitiesConfiguration; + +import java.util.Map; + +/** + * @author Adam Warski (adam at warski dot org) + */ +public final class MiddleSimpleComponentMapper implements MiddleComponentMapper { + private final String propertyName; + private final VersionsEntitiesConfiguration verEntCfg; + + public MiddleSimpleComponentMapper(VersionsEntitiesConfiguration verEntCfg, String propertyName) { + this.propertyName = propertyName; + this.verEntCfg = verEntCfg; + } + + @SuppressWarnings({"unchecked"}) + public Object mapToObjectFromFullMap(EntityInstantiator entityInstantiator, Map data, Number revision) { + return ((Map) data.get(verEntCfg.getOriginalIdPropName())).get(propertyName); + } + + public void mapToMapFromObject(Map data, Object obj) { + data.put(propertyName, obj); + } +} Modified: trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/DetachedRelationInitializor.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/DetachedRelationInitializor.java 2008-09-17 15:38:55 UTC (rev 150) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/DetachedRelationInitializor.java 2008-09-19 14:51:22 UTC (rev 151) @@ -23,8 +23,7 @@ */ package org.jboss.envers.entities.mapper.relation.lazy; -import org.jboss.envers.entities.mapper.relation.lazy.proxy.Initializor; -import org.jboss.envers.entities.mapper.relation.DetachedRelationQueryGenerator; +import org.jboss.envers.entities.mapper.relation.lazy.initializor.Initializor; import org.jboss.envers.entities.EntityInstantiator; import org.jboss.envers.reader.VersionsReaderImplementor; import org.jboss.envers.exception.VersionsException; @@ -39,19 +38,19 @@ public class DetachedRelationInitializor implements Initializor { private final VersionsConfiguration verCfg; private final String entityName; - private final DetachedRelationQueryGenerator queryGenerator; + //private final DetachedRelationQueryGenerator queryGenerator; private final VersionsReaderImplementor versionsReader; private final Object primaryKey; private final Number revision; private final Class collectionClass; public DetachedRelationInitializor(VersionsConfiguration verCfg, String entityName, - DetachedRelationQueryGenerator queryGenerator, + //DetachedRelationQueryGenerator queryGenerator, VersionsReaderImplementor versionsReader, Object primaryKey, Number revision, Class collectionClass) { this.verCfg = verCfg; this.entityName = entityName; - this.queryGenerator = queryGenerator; + //this.queryGenerator = queryGenerator; this.versionsReader = versionsReader; this.primaryKey = primaryKey; this.revision = revision; @@ -62,7 +61,7 @@ public T initialize() { EntityInstantiator entityInstantiator = new EntityInstantiator(verCfg, versionsReader); - List queryResult = queryGenerator.getQuery(versionsReader, primaryKey, revision).list(); + List queryResult = null;//queryGenerator.getQuery(versionsReader, primaryKey, revision).list(); T result; try { Modified: 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-17 15:38:55 UTC (rev 150) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/OneToManyAttachedInitializor.java 2008-09-19 14:51:22 UTC (rev 151) @@ -22,7 +22,7 @@ package org.jboss.envers.entities.mapper.relation.lazy; import org.jboss.envers.reader.VersionsReaderImplementor; -import org.jboss.envers.entities.mapper.relation.lazy.proxy.Initializor; +import org.jboss.envers.entities.mapper.relation.lazy.initializor.Initializor; import org.jboss.envers.query.VersionsRestrictions; import org.jboss.envers.exception.VersionsException; @@ -32,7 +32,7 @@ /** * @author Adam Warski (adam at warski dot org) */ -public class OneToManyAttachedInitializor implements Initializor { +public class OneToManyAttachedInitializor implements Initializor { private VersionsReaderImplementor versionsReader; private Class entityClass; private String owningReferencePropertyName; @@ -61,7 +61,8 @@ } else { Collection result; try { - result = collectionClass.newInstance(); + // TODO + result = (Collection) collectionClass.newInstance(); } catch (Exception e) { throw new VersionsException(e); } Added: 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 (rev 0) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/AbstractCollectionInitializor.java 2008-09-19 14:51:22 UTC (rev 151) @@ -0,0 +1,49 @@ +package org.jboss.envers.entities.mapper.relation.lazy.initializor; + +import org.jboss.envers.entities.mapper.relation.query.MiddleTableQueryGenerator; +import org.jboss.envers.entities.EntityInstantiator; +import org.jboss.envers.reader.VersionsReaderImplementor; +import org.jboss.envers.configuration.VersionsConfiguration; + +import java.util.List; + +/** + * Initializes a persistent collection. + * @author Adam Warski (adam at warski dot org) + */ +public abstract class AbstractCollectionInitializor implements Initializor { + private final VersionsReaderImplementor versionsReader; + private final MiddleTableQueryGenerator queryGenerator; + private final Object primaryKey; + + protected final Number revision; + protected final EntityInstantiator entityInstantiator; + + public AbstractCollectionInitializor(VersionsConfiguration verCfg, + VersionsReaderImplementor versionsReader, + MiddleTableQueryGenerator queryGenerator, + Object primaryKey, Number revision) { + this.versionsReader = versionsReader; + this.queryGenerator = queryGenerator; + this.primaryKey = primaryKey; + this.revision = revision; + + entityInstantiator = new EntityInstantiator(verCfg, versionsReader); + } + + protected abstract T initializeCollection(int size); + + protected abstract void addToCollection(T collection, Object collectionRow); + + public T initialize() { + List collectionContent = queryGenerator.getQuery(versionsReader, primaryKey, revision).list(); + + T collection = initializeCollection(collectionContent.size()); + + for (Object collectionRow : collectionContent) { + addToCollection(collection, collectionRow); + } + + return collection; + } +} Added: 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 (rev 0) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/ArrayCollectionInitializor.java 2008-09-19 14:51:22 UTC (rev 151) @@ -0,0 +1,47 @@ +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.MiddleComponentData; +import org.jboss.envers.reader.VersionsReaderImplementor; +import org.jboss.envers.configuration.VersionsConfiguration; + +import java.util.*; + +/** + * Initializes a map. + * @author Adam Warski (adam at warski dot org) + */ +public class ArrayCollectionInitializor extends AbstractCollectionInitializor { + private final MiddleComponentData elementComponentData; + private final MiddleComponentData indexComponentData; + + public ArrayCollectionInitializor(VersionsConfiguration verCfg, + VersionsReaderImplementor versionsReader, + MiddleTableQueryGenerator queryGenerator, + Object primaryKey, Number revision, + MiddleComponentData elementComponentData, + MiddleComponentData indexComponentData) { + super(verCfg, versionsReader, queryGenerator, primaryKey, revision); + + this.elementComponentData = elementComponentData; + this.indexComponentData = indexComponentData; + } + + protected Object[] initializeCollection(int size) { + return new Object[size]; + } + + @SuppressWarnings({"unchecked"}) + protected void addToCollection(Object[] collection, Object collectionRow) { + Object elementData = ((List) collectionRow).get(elementComponentData.getComponentIndex()); + Object element = elementComponentData.getComponentMapper().mapToObjectFromFullMap(entityInstantiator, + (Map) elementData, revision); + + Object indexData = ((List) collectionRow).get(indexComponentData.getComponentIndex()); + Object indexObj = indexComponentData.getComponentMapper().mapToObjectFromFullMap(entityInstantiator, + (Map) indexData, revision); + int index = ((Number) indexObj).intValue(); + + collection[index] = element; + } +} \ No newline at end of file Added: 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 (rev 0) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/BasicCollectionInitializor.java 2008-09-19 14:51:22 UTC (rev 151) @@ -0,0 +1,48 @@ +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.MiddleComponentData; +import org.jboss.envers.reader.VersionsReaderImplementor; +import org.jboss.envers.exception.VersionsException; +import org.jboss.envers.configuration.VersionsConfiguration; + +import java.util.*; + +/** + * Initializes a non-indexed java collection (set or list, eventually sorted). + * @author Adam Warski (adam at warski dot org) + */ +public class BasicCollectionInitializor extends AbstractCollectionInitializor { + private final Class collectionClass; + private final MiddleComponentData elementComponentData; + + public BasicCollectionInitializor(VersionsConfiguration verCfg, + VersionsReaderImplementor versionsReader, + MiddleTableQueryGenerator queryGenerator, + Object primaryKey, Number revision, + Class collectionClass, + MiddleComponentData elementComponentData) { + super(verCfg, versionsReader, queryGenerator, primaryKey, revision); + + this.collectionClass = collectionClass; + this.elementComponentData = elementComponentData; + } + + protected T initializeCollection(int size) { + try { + return collectionClass.newInstance(); + } catch (InstantiationException e) { + throw new VersionsException(e); + } catch (IllegalAccessException e) { + throw new VersionsException(e); + } + } + + @SuppressWarnings({"unchecked"}) + protected void addToCollection(T collection, Object collectionRow) { + Object elementData = ((List) collectionRow).get(elementComponentData.getComponentIndex()); + Object element = elementComponentData.getComponentMapper().mapToObjectFromFullMap(entityInstantiator, + (Map) elementData, revision); + collection.add(element); + } +} Copied: trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/Initializor.java (from rev 149, trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/Initializor.java) =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/Initializor.java (rev 0) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/Initializor.java 2008-09-19 14:51:22 UTC (rev 151) @@ -0,0 +1,29 @@ +/* + * 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.initializor; + +/** + * @author Adam Warski (adam at warski dot org) + */ +public interface Initializor { + T initialize(); +} Added: 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 (rev 0) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/ListCollectionInitializor.java 2008-09-19 14:51:22 UTC (rev 151) @@ -0,0 +1,47 @@ +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.MiddleComponentData; +import org.jboss.envers.reader.VersionsReaderImplementor; +import org.jboss.envers.configuration.VersionsConfiguration; + +import java.util.*; + +/** + * Initializes a map. + * @author Adam Warski (adam at warski dot org) + */ +public class ListCollectionInitializor extends AbstractCollectionInitializor { + private final MiddleComponentData elementComponentData; + private final MiddleComponentData indexComponentData; + + public ListCollectionInitializor(VersionsConfiguration verCfg, + VersionsReaderImplementor versionsReader, + MiddleTableQueryGenerator queryGenerator, + Object primaryKey, Number revision, + MiddleComponentData elementComponentData, + MiddleComponentData indexComponentData) { + super(verCfg, versionsReader, queryGenerator, primaryKey, revision); + + this.elementComponentData = elementComponentData; + this.indexComponentData = indexComponentData; + } + + protected List initializeCollection(int size) { + return new ArrayList(); + } + + @SuppressWarnings({"unchecked"}) + protected void addToCollection(List collection, Object collectionRow) { + Object elementData = ((List) collectionRow).get(elementComponentData.getComponentIndex()); + Object element = elementComponentData.getComponentMapper().mapToObjectFromFullMap(entityInstantiator, + (Map) elementData, revision); + + Object indexData = ((List) collectionRow).get(indexComponentData.getComponentIndex()); + Object indexObj = indexComponentData.getComponentMapper().mapToObjectFromFullMap(entityInstantiator, + (Map) indexData, revision); + int index = ((Number) indexObj).intValue(); + + collection.set(index, element); + } +} \ No newline at end of file Added: 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 (rev 0) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/MapCollectionInitializor.java 2008-09-19 14:51:22 UTC (rev 151) @@ -0,0 +1,56 @@ +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.MiddleComponentData; +import org.jboss.envers.reader.VersionsReaderImplementor; +import org.jboss.envers.exception.VersionsException; +import org.jboss.envers.configuration.VersionsConfiguration; + +import java.util.*; + +/** + * Initializes a map. + * @author Adam Warski (adam at warski dot org) + */ +public class MapCollectionInitializor> extends AbstractCollectionInitializor { + private final Class collectionClass; + private final MiddleComponentData elementComponentData; + private final MiddleComponentData indexComponentData; + + public MapCollectionInitializor(VersionsConfiguration verCfg, + VersionsReaderImplementor versionsReader, + MiddleTableQueryGenerator queryGenerator, + Object primaryKey, Number revision, + Class collectionClass, + MiddleComponentData elementComponentData, + MiddleComponentData indexComponentData) { + super(verCfg, versionsReader, queryGenerator, primaryKey, revision); + + this.collectionClass = collectionClass; + this.elementComponentData = elementComponentData; + this.indexComponentData = indexComponentData; + } + + protected T initializeCollection(int size) { + try { + return collectionClass.newInstance(); + } catch (InstantiationException e) { + throw new VersionsException(e); + } catch (IllegalAccessException e) { + throw new VersionsException(e); + } + } + + @SuppressWarnings({"unchecked"}) + protected void addToCollection(T collection, Object collectionRow) { + Object elementData = ((List) collectionRow).get(elementComponentData.getComponentIndex()); + Object element = elementComponentData.getComponentMapper().mapToObjectFromFullMap(entityInstantiator, + (Map) elementData, revision); + + Object indexData = ((List) collectionRow).get(indexComponentData.getComponentIndex()); + Object index = indexComponentData.getComponentMapper().mapToObjectFromFullMap(entityInstantiator, + (Map) indexData, revision); + + collection.put(index, element); + } +} \ No newline at end of file Modified: trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/CollectionProxy.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/CollectionProxy.java 2008-09-17 15:38:55 UTC (rev 150) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/CollectionProxy.java 2008-09-19 14:51:22 UTC (rev 151) @@ -21,6 +21,8 @@ */ package org.jboss.envers.entities.mapper.relation.lazy.proxy; +import org.jboss.envers.entities.mapper.relation.lazy.initializor.Initializor; + import java.util.Collection; import java.util.Iterator; @@ -28,7 +30,7 @@ * @author Adam Warski (adam at warski dot org) */ public abstract class CollectionProxy> implements Collection { - private Initializor initializor; + private org.jboss.envers.entities.mapper.relation.lazy.initializor.Initializor initializor; protected T delegate; public CollectionProxy(Initializor initializor) { Deleted: trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/Initializor.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/Initializor.java 2008-09-17 15:38:55 UTC (rev 150) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/Initializor.java 2008-09-19 14:51:22 UTC (rev 151) @@ -1,31 +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.proxy; - -import java.util.Collection; - -/** - * @author Adam Warski (adam at warski dot org) - */ -public interface Initializor { - T initialize(); -} Modified: trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/ListProxy.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/ListProxy.java 2008-09-17 15:38:55 UTC (rev 150) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/ListProxy.java 2008-09-19 14:51:22 UTC (rev 151) @@ -29,7 +29,7 @@ * @author Adam Warski (adam at warski dot org) */ public class ListProxy extends CollectionProxy> implements List { - public ListProxy(Initializor> initializor) { + public ListProxy(org.jboss.envers.entities.mapper.relation.lazy.initializor.Initializor> initializor) { super(initializor); } Modified: trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/MapProxy.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/MapProxy.java 2008-09-17 15:38:55 UTC (rev 150) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/MapProxy.java 2008-09-19 14:51:22 UTC (rev 151) @@ -23,6 +23,8 @@ */ package org.jboss.envers.entities.mapper.relation.lazy.proxy; +import org.jboss.envers.entities.mapper.relation.lazy.initializor.Initializor; + import java.util.Map; import java.util.Set; import java.util.Collection; @@ -34,7 +36,7 @@ private Initializor> initializor; protected Map delegate; - public MapProxy(Initializor> initializor) { + public MapProxy(org.jboss.envers.entities.mapper.relation.lazy.initializor.Initializor> initializor) { this.initializor = initializor; } Modified: trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/SetProxy.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/SetProxy.java 2008-09-17 15:38:55 UTC (rev 150) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/SetProxy.java 2008-09-19 14:51:22 UTC (rev 151) @@ -27,7 +27,7 @@ * @author Adam Warski (adam at warski dot org) */ public class SetProxy extends CollectionProxy> implements Set { - public SetProxy(Initializor> initializor) { + public SetProxy(org.jboss.envers.entities.mapper.relation.lazy.initializor.Initializor> initializor) { super(initializor); } } Copied: trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/SortedMapProxy.java (from rev 149, trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/MapProxy.java) =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/SortedMapProxy.java (rev 0) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/SortedMapProxy.java 2008-09-19 14:51:22 UTC (rev 151) @@ -0,0 +1,147 @@ +/* + * JBoss, Home of Professional Open Source + * + * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated + * by the @authors tag. All rights reserved. + * + * See the copyright.txt in the distribution for a full listing of individual + * contributors. 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.proxy; + +import org.jboss.envers.entities.mapper.relation.lazy.initializor.Initializor; + +import java.util.*; + +/** + * @author Adam Warski (adam at warski dot org) + */ +public class SortedMapProxy implements SortedMap { + private Initializor> initializor; + protected SortedMap delegate; + + public SortedMapProxy(org.jboss.envers.entities.mapper.relation.lazy.initializor.Initializor> initializor) { + this.initializor = initializor; + } + + private void checkInit() { + if (delegate == null) { + delegate = initializor.initialize(); + } + } + + public int size() { + checkInit(); + return delegate.size(); + } + + public boolean isEmpty() { + checkInit(); + return delegate.isEmpty(); + } + + public boolean containsKey(Object o) { + checkInit(); + return delegate.containsKey(o); + } + + public boolean containsValue(Object o) { + checkInit(); + return delegate.containsValue(o); + } + + public V get(Object o) { + checkInit(); + return delegate.get(o); + } + + public V put(K k, V v) { + checkInit(); + return delegate.put(k, v); + } + + public V remove(Object o) { + checkInit(); + return delegate.remove(o); + } + + public void putAll(Map map) { + checkInit(); + delegate.putAll(map); + } + + public void clear() { + checkInit(); + delegate.clear(); + } + + public Set keySet() { + checkInit(); + return delegate.keySet(); + } + + public Collection values() { + checkInit(); + return delegate.values(); + } + + public Set> entrySet() { + checkInit(); + return delegate.entrySet(); + } + + public Comparator comparator() { + checkInit(); + return delegate.comparator(); + } + + public SortedMap subMap(K k, K k1) { + checkInit(); + return delegate.subMap(k, k1); + } + + public SortedMap headMap(K k) { + checkInit(); + return delegate.headMap(k); + } + + public SortedMap tailMap(K k) { + checkInit(); + return delegate.tailMap(k); + } + + public K firstKey() { + checkInit(); + return delegate.firstKey(); + } + + public K lastKey() { + checkInit(); + return delegate.lastKey(); + } + + @SuppressWarnings({"EqualsWhichDoesntCheckParameterClass"}) + public boolean equals(Object o) { + checkInit(); + return delegate.equals(o); + } + + public int hashCode() { + checkInit(); + return delegate.hashCode(); + } +} \ No newline at end of file Property changes on: trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/SortedMapProxy.java ___________________________________________________________________ Name: svn:mergeinfo + Copied: trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/SortedSetProxy.java (from rev 148, trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/SetProxy.java) =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/SortedSetProxy.java (rev 0) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/SortedSetProxy.java 2008-09-19 14:51:22 UTC (rev 151) @@ -0,0 +1,66 @@ +/* + * JBoss, Home of Professional Open Source + * + * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated + * by the @authors tag. All rights reserved. + * + * See the copyright.txt in the distribution for a full listing of individual + * contributors. 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.proxy; + +import java.util.SortedSet; +import java.util.Comparator; + +/** + * @author Adam Warski (adam at warski dot org) + */ +public class SortedSetProxy extends CollectionProxy> implements SortedSet { + public SortedSetProxy(org.jboss.envers.entities.mapper.relation.lazy.initializor.Initializor> initializor) { + super(initializor); + } + + public Comparator comparator() { + checkInit(); + return delegate.comparator(); + } + + public SortedSet subSet(U u, U u1) { + checkInit(); + return delegate.subSet(u, u1); + } + + public SortedSet headSet(U u) { + checkInit(); + return delegate.headSet(u); + } + + public SortedSet tailSet(U u) { + checkInit(); + return delegate.tailSet(u); + } + + public U first() { + checkInit(); + return delegate.first(); + } + + public U last() { + checkInit(); + return delegate.last(); + } +} \ No newline at end of file Property changes on: trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/SortedSetProxy.java ___________________________________________________________________ Name: svn:mergeinfo + Added: trunk/src/main/org/jboss/envers/entities/mapper/relation/query/MiddleTableQueryGenerator.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/query/MiddleTableQueryGenerator.java (rev 0) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/query/MiddleTableQueryGenerator.java 2008-09-19 14:51:22 UTC (rev 151) @@ -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 middle-table (a table used + * for mapping relations). The query can select, apart from selecting the middle table, select also other "related" + * entities. + * @author Adam Warski (adam at warski dot org) + */ +public interface MiddleTableQueryGenerator { + Query getQuery(VersionsReaderImplementor versionsReader, Object primaryKey, Number revision); +} Added: trunk/src/main/org/jboss/envers/entities/mapper/relation/query/OneEntityQueryGenerator.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/query/OneEntityQueryGenerator.java (rev 0) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/query/OneEntityQueryGenerator.java 2008-09-19 14:51:22 UTC (rev 151) @@ -0,0 +1,67 @@ +package org.jboss.envers.entities.mapper.relation.query; + +import org.jboss.envers.entities.mapper.relation.MiddleIdData; +import org.jboss.envers.entities.mapper.id.QueryParameterData; +import org.jboss.envers.configuration.VersionsEntitiesConfiguration; +import org.jboss.envers.reader.VersionsReaderImplementor; +import org.jboss.envers.RevisionType; +import org.hibernate.Query; + +/** + * @author Adam Warski (adam at warski dot org) + */ +public final class OneEntityQueryGenerator implements MiddleTableQueryGenerator { + private final String queryString; + private final MiddleIdData referencingIdData; + + public OneEntityQueryGenerator(VersionsEntitiesConfiguration verEntCfg, + String versionsMiddleEntityName, + MiddleIdData referencingIdData) { + this.referencingIdData = referencingIdData; + + /* + * The query that we need to create: + * SELECT new list(ee) FROM middleEntity ee WHERE + * (only entities referenced by the association) + * ee.id2 = :id2 AND + * (the association at revision :revision) + * ee.revision = (SELECT max(ee2.revision) FROM middleEntity ee2 + * WHERE ee2.revision <= :revision AND ee2.id2 = :id2) AND + * (only non-deleted entities and associations) + * ee.revision_type != DEL + */ + String revisionPropertyPath = verEntCfg.getRevisionPropPath(); + String originalIdPropertyName = verEntCfg.getOriginalIdPropName(); + + StringBuilder querySB = new StringBuilder(); + querySB + .append("SELECT new list(ee) FROM ").append(versionsMiddleEntityName).append(" ee ") + .append("WHERE ") + // ee.id2 = :id2 + .append(referencingIdData.getPrefixedMapper().getIdEqualsQuery("ee." + originalIdPropertyName, true)) + .append(" AND ") + // ee.revision = (SELECT max(ee2.revision) ... + .append("ee.").append(revisionPropertyPath).append(" = (SELECT max(ee2.").append(revisionPropertyPath) + .append(") FROM ").append(versionsMiddleEntityName).append(" ee2 ") + // ee2.revision <= :revision + .append("WHERE ee2.").append(revisionPropertyPath).append(" <= :revision AND ") + // ee2.id2 = :id2) + .append(referencingIdData.getPrefixedMapper().getIdEqualsQuery("ee2." + originalIdPropertyName, true)) + .append(") AND ") + // ee.revision_type != DEL + .append("ee.").append(verEntCfg.getRevisionTypePropName()).append(" != ").append(":delrevisiontype"); + + queryString = querySB.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; + } +} Added: trunk/src/main/org/jboss/envers/entities/mapper/relation/query/ThreeEntityQueryGenerator.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/query/ThreeEntityQueryGenerator.java (rev 0) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/query/ThreeEntityQueryGenerator.java 2008-09-19 14:51:22 UTC (rev 151) @@ -0,0 +1,29 @@ +package org.jboss.envers.entities.mapper.relation.query; + +import org.hibernate.Query; +import org.jboss.envers.reader.VersionsReaderImplementor; +import org.jboss.envers.entities.mapper.relation.MiddleIdData; +import org.jboss.envers.configuration.VersionsEntitiesConfiguration; + +/** + * @author Adam Warski (adam at warski dot org) + */ +public final class ThreeEntityQueryGenerator implements MiddleTableQueryGenerator { + private final String queryString; + private final MiddleIdData referencingIdData; + + public ThreeEntityQueryGenerator(VersionsEntitiesConfiguration verEntCfg, + String versionsMiddleEntityName, + MiddleIdData referencingIdData, + MiddleIdData referencedIdData1, + MiddleIdData referencedIdData2) { + this.referencingIdData = referencingIdData; + + // TODO + queryString = ""; + } + + public Query getQuery(VersionsReaderImplementor versionsReader, Object primaryKey, Number revision) { + throw new RuntimeException(); + } +} Added: trunk/src/main/org/jboss/envers/entities/mapper/relation/query/TwoEntityQueryGenerator.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/query/TwoEntityQueryGenerator.java (rev 0) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/query/TwoEntityQueryGenerator.java 2008-09-19 14:51:22 UTC (rev 151) @@ -0,0 +1,93 @@ +package org.jboss.envers.entities.mapper.relation.query; + +import org.jboss.envers.entities.mapper.id.QueryParameterData; +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.hibernate.Query; + +/** + * @author Adam Warski (adam at warski dot org) + */ +public final class TwoEntityQueryGenerator implements MiddleTableQueryGenerator { + private final String queryString; + private final MiddleIdData referencingIdData; + + public TwoEntityQueryGenerator(VersionsEntitiesConfiguration verEntCfg, + String versionsMiddleEntityName, + MiddleIdData referencingIdData, + MiddleIdData referencedIdData) { + this.referencingIdData = referencingIdData; + + /* + * The query that we need to create: + * SELECT new list(ee, e) FROM versionsReferencedEntity e, middleEntity ee + * WHERE + * (selecting e entities at revision :revision) + * e.revision = (SELECT max(e2.revision) FROM versionsReferencedEntity e2 + * WHERE e2.revision <= :revision AND e2.id1 = e.id1) AND + * (only entities referenced by the association) + * ee.id1 = e.id1 AND + * ee.id2 = :id2 AND + * (the association at revision :revision) + * ee.revision = (SELECT max(ee2.revision) FROM middleEntity ee2 + * WHERE ee2.revision <= :revision AND ee2.id2 = :id2 AND ee2.id1 = ee.id1) AND + * (only non-deleted entities and associations) + * ee.revision_type != DEL AND + * e.revision_type != DEL + */ + String revisionPropertyPath = verEntCfg.getRevisionPropPath(); + String originalIdPropertyName = verEntCfg.getOriginalIdPropName(); + + StringBuilder querySB = new StringBuilder(); + querySB + .append("SELECT new list(ee, e) FROM ").append(referencedIdData.getVersionsEntityName()).append(" e") + .append(", ").append(versionsMiddleEntityName).append(" ee ") + .append("WHERE ") + // e.revision = (SELECT max(e2.revision) ... + .append("e.").append(revisionPropertyPath).append(" = (SELECT max(e2.").append(revisionPropertyPath) + .append(") FROM ").append(referencedIdData.getVersionsEntityName()).append(" e2 ") + // e2.revision <= :revision + .append("WHERE e2.").append(revisionPropertyPath).append(" <= :revision AND ") + // e2.id1 = e.id1) + .append(referencedIdData.getOriginalMapper().getIdsEqualQuery("e." + originalIdPropertyName, "e2." + originalIdPropertyName)) + .append(") AND ") + // ee.id1 = e.id1 + .append(referencedIdData.getPrefixedMapper().getIdsEqualQuery("ee." + originalIdPropertyName, + referencedIdData.getOriginalMapper(), "e." + originalIdPropertyName)) + .append(" AND ") + // ee.id2 = :id2 + .append(referencingIdData.getPrefixedMapper().getIdEqualsQuery("ee." + originalIdPropertyName, true)) + .append(" AND ") + // ee.revision = (SELECT max(ee2.revision) ... + .append("ee.").append(revisionPropertyPath).append(" = (SELECT max(ee2.").append(revisionPropertyPath) + .append(") FROM ").append(versionsMiddleEntityName).append(" ee2 ") + // ee2.revision <= :revision + .append("WHERE ee2.").append(revisionPropertyPath).append(" <= :revision AND ") + // ee2.id2 = :id2) + .append(referencingIdData.getPrefixedMapper().getIdEqualsQuery("ee2." + originalIdPropertyName, true)) + .append(" AND ") + // ee2.id1 = ee.id1) + .append(referencedIdData.getPrefixedMapper().getIdsEqualQuery("ee." + originalIdPropertyName, "ee2." + originalIdPropertyName)) + .append(") AND ") + // e.revision_type != DEL AND + .append("e.").append(verEntCfg.getRevisionTypePropName()).append(" != ").append(":delrevisiontype") + .append(" AND ") + // ee.revision_type != DEL + .append("ee.").append(verEntCfg.getRevisionTypePropName()).append(" != ").append(":delrevisiontype"); + + queryString = querySB.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; + } +} Modified: trunk/src/main/org/jboss/envers/event/VersionsEventListener.java =================================================================== --- trunk/src/main/org/jboss/envers/event/VersionsEventListener.java 2008-09-17 15:38:55 UTC (rev 150) +++ trunk/src/main/org/jboss/envers/event/VersionsEventListener.java 2008-09-19 14:51:22 UTC (rev 151) @@ -134,7 +134,8 @@ RelationDescription relDesc = verCfg.getEntCfg().getRelationDescription(event.getAffectedOwnerEntityName(), workUnit.getReferencingPropertyName()); - if (relDesc.isBidirectional()) { + // TODO: remove the null check (relation info should be added in the metadata generator) + if (relDesc != null && relDesc.isBidirectional()) { String relatedEntityName = relDesc.getToEntityName(); IdMapper relatedIdMapper = verCfg.getEntCfg().get(relatedEntityName).getIdMapper(); Modified: trunk/src/main/org/jboss/envers/exception/NotVersionedException.java =================================================================== --- trunk/src/main/org/jboss/envers/exception/NotVersionedException.java 2008-09-17 15:38:55 UTC (rev 150) +++ trunk/src/main/org/jboss/envers/exception/NotVersionedException.java 2008-09-19 14:51:22 UTC (rev 151) @@ -22,6 +22,7 @@ package org.jboss.envers.exception; /** + * TODO: add a field describing what isn't versioned. * @author Adam Warski (adam at warski dot org) */ public class NotVersionedException extends VersionsException { Modified: trunk/src/main/org/jboss/envers/exception/RevisionDoesNotExistException.java =================================================================== --- trunk/src/main/org/jboss/envers/exception/RevisionDoesNotExistException.java 2008-09-17 15:38:55 UTC (rev 150) +++ trunk/src/main/org/jboss/envers/exception/RevisionDoesNotExistException.java 2008-09-19 14:51:22 UTC (rev 151) @@ -24,6 +24,7 @@ import java.util.Date; /** + * TODO: add the revision number. * @author Adam Warski (adam at warski dot org) */ public class RevisionDoesNotExistException extends VersionsException { Modified: trunk/src/main/org/jboss/envers/query/VersionsQueryCreator.java =================================================================== --- trunk/src/main/org/jboss/envers/query/VersionsQueryCreator.java 2008-09-17 15:38:55 UTC (rev 150) +++ trunk/src/main/org/jboss/envers/query/VersionsQueryCreator.java 2008-09-19 14:51:22 UTC (rev 151) @@ -67,7 +67,7 @@ * @param c Class of the entities for which to query. * @param selectEntitiesOnly If true, instead of a list of three-element arrays, a list of entites will be * returned as a result of executing this query. - * @param selectDeletedEntities If true, also revisions where entities where deleted will be returned. The + * @param selectDeletedEntities If true, also revisions where entities were deleted will be returned. The additional * entities will have revision type "delete", and contain no data (all fields null), except for the id field. * @return A query for revisions at which instances of the given entity were modified, to which * conditions can be added (for example - a specific id of an entity of class c), and which From jboss-envers-commits at lists.jboss.org Sun Sep 21 08:21:26 2008 From: jboss-envers-commits at lists.jboss.org (jboss-envers-commits at lists.jboss.org) Date: Sun, 21 Sep 2008 08:21:26 -0400 Subject: [jboss-envers-commits] JBoss Envers SVN: r152 - in trunk/src: main/org/jboss/envers/entities and 3 other directories. Message-ID: Author: adamw Date: 2008-09-21 08:21:25 -0400 (Sun, 21 Sep 2008) New Revision: 152 Modified: trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java trunk/src/main/org/jboss/envers/configuration/metadata/ToOneRelationMetadataGenerator.java trunk/src/main/org/jboss/envers/entities/EntitiesConfigurations.java trunk/src/main/org/jboss/envers/entities/EntityConfiguration.java trunk/src/main/org/jboss/envers/entities/RelationType.java trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleDummyComponentMapper.java trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleRelatedComponentMapper.java trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleSimpleComponentMapper.java trunk/src/main/org/jboss/envers/event/VersionsEventListener.java trunk/src/test/org/jboss/envers/test/integration/naming/OneToManyUnidirectionalNaming.java Log: ENVERS-42, ENVERS-44: many-to-many relations support working again Modified: trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java =================================================================== --- trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java 2008-09-19 14:51:22 UTC (rev 151) +++ trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java 2008-09-21 12:21:25 UTC (rev 152) @@ -28,7 +28,7 @@ * Generates metadata for collection-valued properties. * @author Adam Warski (adam at warski dot org) */ -public final class CollectionMetadataGenerator { +public final class CollectionMetadataGenerator { private final VersionsMetadataGenerator mainGenerator; CollectionMetadataGenerator(VersionsMetadataGenerator versionsMetadataGenerator) { @@ -102,7 +102,7 @@ IdMapper ownedIdMapper = referencingIdMapping.getIdMapper().prefixMappedProperties(lastPropertyPrefix); // Storing information about this relation - mainGenerator.getEntitiesConfigurations().get(entityName).addOneToManyAttachedRelation(name, owningReferencePropertyName, + mainGenerator.getEntitiesConfigurations().get(entityName).addToManyNotOwningRelation(name, owningReferencePropertyName, owningEntityName, ownedIdMapper); // Adding mapper for the id @@ -119,6 +119,15 @@ } @SuppressWarnings({"unchecked"}) + private void addRelatedToXmlMapping(Element xmlMapping, String prefix, Value relatedValue, IdMappingData relatedIdMapping) { + Element properties = (Element) relatedIdMapping.getXmlRelationMapping().clone(); + MetadataTools.prefixNamesInPropertyElement(properties, prefix, relatedValue.getColumnIterator(), true); + for (Element idProperty : (java.util.List) properties.elements()) { + xmlMapping.add((Element) idProperty.clone()); + } + } + + @SuppressWarnings({"unchecked"}) private void addWithMiddleTable(String name, Collection value, CompositeMapperBuilder currentMapper, String entityName, EntityXmlMappingData xmlMappingData) { // Generating the name of the middle table, its schema and catalog @@ -135,16 +144,13 @@ // If the relation is inverse, will be later checked by comparing middleEntityXml with null. Element middleEntityXml; Element middleEntityXmlId; - Iterator middleEntityOriginalColumns; if (!value.isInverse()) { middleEntityXml = MetadataTools.createEntity(xmlMappingData.newAdditionalMapping(), versionsMiddleEntityName, versionsMiddleTableName, schema, catalog, null); middleEntityXmlId = middleEntityXml.addElement("composite-id"); - middleEntityOriginalColumns = value.getCollectionTable().getColumnIterator(); } else { middleEntityXml = null; middleEntityXmlId = null; - middleEntityOriginalColumns = null; } // ****** @@ -200,11 +206,8 @@ if (middleEntityXml != null) { middleEntityXmlId.addAttribute("name", mainGenerator.getVerEntCfg().getOriginalIdPropName()); - Element properties = (Element) referencingIdMapping.getXmlRelationMapping().clone(); - MetadataTools.prefixNamesInPropertyElement(properties, referencingPrefix, middleEntityOriginalColumns, true); - for (Element idProperty : (java.util.List) properties.elements()) { - middleEntityXmlId.add((Element) idProperty.clone()); - } + // Adding related-entity (in this case: the referencing's entity id) id mapping to the xml. + addRelatedToXmlMapping(middleEntityXmlId, referencingPrefix, value.getKey(), referencingIdMapping); // Adding the revision number as a foreign key to the revision info entity to the composite id of the // middle table. @@ -218,7 +221,7 @@ // Generating the element mapping. // ****** MiddleComponentData elementComponentData = addValueToMiddleTable(value.getElement(), middleEntityXmlId, - middleEntityOriginalColumns, queryGeneratorBuilder, referencedPrefix); + queryGeneratorBuilder, referencedPrefix); // ****** // Optionally, generating the index mapping. @@ -254,6 +257,17 @@ } else { throw new RuntimeException(); } + + // ****** + // Storing information about this relation. + // ****** + if (value.isInverse()) { + mainGenerator.getEntitiesConfigurations().get(entityName).addToManyMiddleNotOwningRelation(name, mappedBy, + referencedEntityName); + } else { + mainGenerator.getEntitiesConfigurations().get(entityName).addToManyMiddleRelation(name, + referencedEntityName); + } } /** @@ -261,15 +275,12 @@ * @param value Value, which should be mapped to the middle-table, either as a relation to another entity, * or as a simple value. * @param middleEntityXml If not null, xml mapping for this value is added to this element. - * @param middleEntityOriginalColumns If middleEntityXml is not null, this iterator is used - * to read column names for the generated xml mapping. * @param queryGeneratorBuilder In case value is a relation to another entity, information about it * should be added to the given. * @param prefix Prefix for proeprty names of related entities identifiers. * @return Data for mapping this component. */ private MiddleComponentData addValueToMiddleTable(Value value, Element middleEntityXml, - Iterator middleEntityOriginalColumns, QueryGeneratorBuilder queryGeneratorBuilder, String prefix) { Type type = value.getType(); @@ -279,6 +290,12 @@ IdMappingData referencedIdMapping = mainGenerator.getEntitiesConfigurations().get( toOneValue.getReferencedEntityName()).getIdMappingData(); + // Adding related-entity (in this case: the referenced entities id) id mapping to the xml only if the + // relation isn't inverse (so when middleEntityXml is not null). + if (middleEntityXml != null) { + addRelatedToXmlMapping(middleEntityXml, prefix, value, referencedIdMapping); + } + // Storing the id data of the referenced entity: original mapper, prefixed mapper and entity name. IdMapper referencedPrefixedIdMapper = referencedIdMapping.getIdMapper().prefixMappedProperties(prefix); MiddleIdData referencedIdData = new MiddleIdData( @@ -302,114 +319,4 @@ throw new RuntimeException(); } } - - private String getMiddleEntityName(String entityName, String referencedEntityName, ManyToOne mto) { - return entityName + "_" + referencedEntityName + "_" + mto.getTable().getName(); - } - - @SuppressWarnings({"unchecked"}) - private void addOneToManyDetached(String name, Collection value, CompositeMapperBuilder mapper, String entityName, - EntityXmlMappingData xmlMappingData) { - ManyToOne mto = (ManyToOne) value.getElement(); - - String referencedEntityName = mto.getReferencedEntityName(); - - EntityConfiguration configuration = mainGenerator.getEntitiesConfigurations().get(entityName); - if (configuration == null) { - throw new MappingException("A versioned relation to a non-versioned entity " + entityName + "!"); - } - - EntityConfiguration referencedConfiguration = mainGenerator.getEntitiesConfigurations().get(referencedEntityName); - if (referencedConfiguration == null) { - throw new MappingException("A versioned relation to a non-versioned entity " + referencedEntityName + "!"); - } - - IdMappingData referencingIdMapping = configuration.getIdMappingData(); - IdMappingData referencedIdMapping = referencedConfiguration.getIdMappingData(); - - String referencingPrefix = StringTools.getLastComponent(entityName) + "_"; - String referencedPrefix = name + "_"; - - // Name of the entity that will be used to store the relation between the two entities. - String middleEntityName = getMiddleEntityName(entityName, referencedEntityName, mto); - String versionsMiddleEntityName = mainGenerator.getVerEntCfg().getVersionsEntityName(middleEntityName); - String versionsMiddleTableName = mainGenerator.getVerEntCfg().getVersionsTableName(middleEntityName, mto.getTable().getName()); - - Element middleEntity = MetadataTools.createEntity(xmlMappingData.newAdditionalMapping(), versionsMiddleEntityName, - versionsMiddleTableName, mto.getTable().getSchema(), mto.getTable().getCatalog(), null); - - Element middleEntityId = middleEntity.addElement("composite-id"); - middleEntityId.addAttribute("name", mainGenerator.getVerEntCfg().getOriginalIdPropName()); - - Iterator columnIterator = mto.getTable().getColumnIterator(); - - // Adding elements to the mapping corresponding to the referencing entity id's - Element properties = (Element) referencingIdMapping.getXmlRelationMapping().clone(); - MetadataTools.prefixNamesInPropertyElement(properties, referencingPrefix, columnIterator, true); - for (Element idProperty : (java.util.List) properties.elements()) { - middleEntityId.add((Element) idProperty.clone()); - } - - // Adding elements to the mapping corresponding to the referenced entity id's - properties = (Element) referencedIdMapping.getXmlRelationMapping().clone(); - MetadataTools.prefixNamesInPropertyElement(properties, referencedPrefix, columnIterator, true); - for (Element idProperty : (java.util.List) properties.elements()) { - middleEntityId.add((Element) idProperty.clone()); - } - - mainGenerator.addRevisionInfoRelation(middleEntityId); - mainGenerator.addRevisionType(middleEntity); - - mainGenerator.getEntitiesConfigurations().get(entityName).addOneToManyDetachedRelation(name, referencedEntityName); - - // Adding the property mapper - mapper.addComposite(name, new OneToManyDetachedMapper(mainGenerator.getVerEntCfg(), entityName, - referencedEntityName, name, mainGenerator.getVerEntCfg().getVersionsEntityName(referencedEntityName), - versionsMiddleEntityName, referencingIdMapping.getIdMapper().prefixMappedProperties(referencingPrefix), - referencedIdMapping.getIdMapper().prefixMappedProperties(referencedPrefix), - mainGenerator.getEntitiesConfigurations().get(referencedEntityName).getIdMapper())); - } - - private void addManyToManyNotOwning(String name, Collection value, CompositeMapperBuilder mapper, String entityName) { - ManyToOne mto = (ManyToOne) value.getElement(); - - String referencedEntityName = mto.getReferencedEntityName(); - - EntityConfiguration configuration = mainGenerator.getEntitiesConfigurations().get(entityName); - if (configuration == null) { - throw new MappingException("A versioned relation to a non-versioned entity " + entityName + "!"); - } - - EntityConfiguration referencedConfiguration = mainGenerator.getEntitiesConfigurations().get(referencedEntityName); - if (referencedConfiguration == null) { - throw new MappingException("A versioned relation to a non-versioned entity " + referencedEntityName + "!"); - } - - String mappedBy = getMappedBy(value.getCollectionTable(), mainGenerator.getCfg().getClassMapping(referencedEntityName)); - if (mappedBy == null) { - throw new MappingException("Unable to read the mapped by attribute for " + name); - } - - IdMappingData referencingIdMapping = configuration.getIdMappingData(); - IdMappingData referencedIdMapping = referencedConfiguration.getIdMappingData(); - - String referencingPrefix = mappedBy + "_"; - String referencedPrefix = StringTools.getLastComponent(referencedEntityName) + "_"; - - // Name of the entity that will be used to store the relation between the two entities. - // The order is inverse, as the referenced and referencing entity names are swapped with respect to the - // name of the entity that was created on the owning side in addOneToManyDetached. - String middleEntityName = getMiddleEntityName(referencedEntityName, entityName, mto); - String versionsMiddleEntityName = mainGenerator.getVerEntCfg().getVersionsEntityName(middleEntityName); - - mainGenerator.getEntitiesConfigurations().get(entityName).addManyToManyNotOwningRelation(name, mappedBy, - referencedEntityName); - - // Adding the property mapper - mapper.addComposite(name, new ManyToManyNotOwningMapper(mainGenerator.getVerEntCfg(), entityName, - referencedEntityName, name, mainGenerator.getVerEntCfg().getVersionsEntityName(referencedEntityName), - versionsMiddleEntityName, referencingIdMapping.getIdMapper().prefixMappedProperties(referencingPrefix), - referencedIdMapping.getIdMapper().prefixMappedProperties(referencedPrefix), - mainGenerator.getEntitiesConfigurations().get(referencedEntityName).getIdMapper())); - } } Modified: trunk/src/main/org/jboss/envers/configuration/metadata/ToOneRelationMetadataGenerator.java =================================================================== --- trunk/src/main/org/jboss/envers/configuration/metadata/ToOneRelationMetadataGenerator.java 2008-09-19 14:51:22 UTC (rev 151) +++ trunk/src/main/org/jboss/envers/configuration/metadata/ToOneRelationMetadataGenerator.java 2008-09-21 12:21:25 UTC (rev 152) @@ -77,7 +77,7 @@ IdMapper ownedIdMapper = ownedIdMapping.getIdMapper().prefixMappedProperties(lastPropertyPrefix); // Storing information about this relation - mainGenerator.getEntitiesConfigurations().get(entityName).addOneToOneNotOwningRelation(name, owningReferencePropertyName, + mainGenerator.getEntitiesConfigurations().get(entityName).addToOneNotOwningRelation(name, owningReferencePropertyName, referencedEntityName, ownedIdMapper); // Adding mapper for the id Modified: trunk/src/main/org/jboss/envers/entities/EntitiesConfigurations.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/EntitiesConfigurations.java 2008-09-19 14:51:22 UTC (rev 151) +++ trunk/src/main/org/jboss/envers/entities/EntitiesConfigurations.java 2008-09-21 12:21:25 UTC (rev 152) @@ -59,7 +59,7 @@ // If this is an "owned" relation, checking the related entity, if it has a relation that has // a mapped-by attribute to the currently checked. If so, this is a bidirectional relation. if (relDesc.getRelationType() == RelationType.TO_ONE || - relDesc.getRelationType() == RelationType.ONE_TO_MANY_DETACHED) { + relDesc.getRelationType() == RelationType.TO_MANY_MIDDLE) { for (RelationDescription other : entitiesConfigurations.get(relDesc.getToEntityName()).getRelationsIterator()) { if (relDesc.getFromPropertyName().equals(other.getMappedByPropertyName()) && (entityName.equals(other.getToEntityName()))) { Modified: trunk/src/main/org/jboss/envers/entities/EntityConfiguration.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/EntityConfiguration.java 2008-09-19 14:51:22 UTC (rev 151) +++ trunk/src/main/org/jboss/envers/entities/EntityConfiguration.java 2008-09-21 12:21:25 UTC (rev 152) @@ -55,26 +55,25 @@ toEntityName, null, idMapper)); } - public void addOneToOneNotOwningRelation(String fromPropertyName, String mappedByPropertyName, String toEntityName, + public void addToOneNotOwningRelation(String fromPropertyName, String mappedByPropertyName, String toEntityName, IdMapper idMapper) { - relations.put(fromPropertyName, new RelationDescription(fromPropertyName, RelationType.ONE_TO_ONE, + relations.put(fromPropertyName, new RelationDescription(fromPropertyName, RelationType.TO_ONE_NOT_OWNING, toEntityName, mappedByPropertyName, idMapper)); } - public void addOneToManyAttachedRelation(String fromPropertyName, String mappedByPropertyName, String toEntityName, + public void addToManyNotOwningRelation(String fromPropertyName, String mappedByPropertyName, String toEntityName, IdMapper idMapper) { - relations.put(fromPropertyName, new RelationDescription(fromPropertyName, RelationType.ONE_TO_MANY, + relations.put(fromPropertyName, new RelationDescription(fromPropertyName, RelationType.TO_MANY_NOT_OWNING, toEntityName, mappedByPropertyName, idMapper)); } - public void addOneToManyDetachedRelation(String fromPropertyName, String toEntityName) { - relations.put(fromPropertyName, new RelationDescription(fromPropertyName, RelationType.ONE_TO_MANY_DETACHED, + public void addToManyMiddleRelation(String fromPropertyName, String toEntityName) { + relations.put(fromPropertyName, new RelationDescription(fromPropertyName, RelationType.TO_MANY_MIDDLE, toEntityName, null, null)); } - public void addManyToManyNotOwningRelation(String fromPropertyName, String mappedByPropertyName, - String toEntityName) { - relations.put(fromPropertyName, new RelationDescription(fromPropertyName, RelationType.MANY_TO_MANY_NOT_OWNING, + public void addToManyMiddleNotOwningRelation(String fromPropertyName, String mappedByPropertyName, String toEntityName) { + relations.put(fromPropertyName, new RelationDescription(fromPropertyName, RelationType.TO_MANY_MIDDLE_NOT_OWNING, toEntityName, mappedByPropertyName, null)); } Modified: trunk/src/main/org/jboss/envers/entities/RelationType.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/RelationType.java 2008-09-19 14:51:22 UTC (rev 151) +++ trunk/src/main/org/jboss/envers/entities/RelationType.java 2008-09-21 12:21:25 UTC (rev 152) @@ -24,12 +24,31 @@ package org.jboss.envers.entities; /** + * Type of a relation between two entities. * @author Adam Warski (adam at warski dot org) */ public enum RelationType { + /** + * A single-reference-valued relation. The entity owns the relation. + */ TO_ONE, - ONE_TO_ONE, - ONE_TO_MANY, - ONE_TO_MANY_DETACHED, - MANY_TO_MANY_NOT_OWNING + /** + * A single-reference-valued relation. The entity doesn't own the relation. It is directly mapped in the related + * entity. + */ + TO_ONE_NOT_OWNING, + /** + * A collection-of-references-valued relation. The entity doesn't own the relation. It is directly mapped in the + * related entity. + */ + TO_MANY_NOT_OWNING, + /** + * A collection-of-references-valued relation. The entity owns the relation. It is mapped using a middle table. + */ + TO_MANY_MIDDLE, + /** + * A collection-of-references-valued relation. The entity doesn't own the relation. It is mapped using a middle + * table. + */ + TO_MANY_MIDDLE_NOT_OWNING } Modified: trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleDummyComponentMapper.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleDummyComponentMapper.java 2008-09-19 14:51:22 UTC (rev 151) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleDummyComponentMapper.java 2008-09-21 12:21:25 UTC (rev 152) @@ -1,12 +1,12 @@ package org.jboss.envers.entities.mapper.relation.component; import org.jboss.envers.entities.EntityInstantiator; -import org.jboss.envers.entities.mapper.relation.component.MiddleComponentMapper; import java.util.Map; /** * @author Adam Warski (adam at warski dot org) + * @deprecated */ public final class MiddleDummyComponentMapper implements MiddleComponentMapper { public Object mapToObjectFromFullMap(EntityInstantiator entityInstantiator, Map data, Number revision) { Modified: trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleRelatedComponentMapper.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleRelatedComponentMapper.java 2008-09-19 14:51:22 UTC (rev 151) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleRelatedComponentMapper.java 2008-09-21 12:21:25 UTC (rev 152) @@ -1,7 +1,6 @@ package org.jboss.envers.entities.mapper.relation.component; import org.jboss.envers.entities.EntityInstantiator; -import org.jboss.envers.entities.mapper.relation.component.MiddleComponentMapper; import org.jboss.envers.entities.mapper.relation.MiddleIdData; import java.util.Map; @@ -22,6 +21,6 @@ } public void mapToMapFromObject(Map data, Object obj) { - relatedIdData.getOriginalMapper().mapToMapFromEntity(data, obj); + relatedIdData.getPrefixedMapper().mapToMapFromEntity(data, obj); } } Modified: trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleSimpleComponentMapper.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleSimpleComponentMapper.java 2008-09-19 14:51:22 UTC (rev 151) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleSimpleComponentMapper.java 2008-09-21 12:21:25 UTC (rev 152) @@ -1,7 +1,6 @@ package org.jboss.envers.entities.mapper.relation.component; import org.jboss.envers.entities.EntityInstantiator; -import org.jboss.envers.entities.mapper.relation.component.MiddleComponentMapper; import org.jboss.envers.configuration.VersionsEntitiesConfiguration; import java.util.Map; Modified: trunk/src/main/org/jboss/envers/event/VersionsEventListener.java =================================================================== --- trunk/src/main/org/jboss/envers/event/VersionsEventListener.java 2008-09-19 14:51:22 UTC (rev 151) +++ trunk/src/main/org/jboss/envers/event/VersionsEventListener.java 2008-09-21 12:21:25 UTC (rev 152) @@ -52,7 +52,7 @@ return; } - // Checks every property of the entity, if it is an "owned" relation to another entity. + // Checks every property of the entity, if it is an "owned" to-one relation to another entity. // If the value of that property changed, and the relation is bi-directional, a new revision // for the related entity is generated. String[] propertyNames = entityPersister.getPropertyNames(); @@ -134,7 +134,7 @@ RelationDescription relDesc = verCfg.getEntCfg().getRelationDescription(event.getAffectedOwnerEntityName(), workUnit.getReferencingPropertyName()); - // TODO: remove the null check (relation info should be added in the metadata generator) + // relDesc can be null if this is a collection of simple values (not a relation). if (relDesc != null && relDesc.isBidirectional()) { String relatedEntityName = relDesc.getToEntityName(); IdMapper relatedIdMapper = verCfg.getEntCfg().get(relatedEntityName).getIdMapper(); Modified: trunk/src/test/org/jboss/envers/test/integration/naming/OneToManyUnidirectionalNaming.java =================================================================== --- trunk/src/test/org/jboss/envers/test/integration/naming/OneToManyUnidirectionalNaming.java 2008-09-19 14:51:22 UTC (rev 151) +++ trunk/src/test/org/jboss/envers/test/integration/naming/OneToManyUnidirectionalNaming.java 2008-09-21 12:21:25 UTC (rev 152) @@ -76,7 +76,7 @@ } private final static String MIDDLE_VERSIONS_ENTITY_NAME = - "org.jboss.envers.test.integration.naming.DetachedNamingTestEntity_org.jboss.envers.test.entities.StrTestEntity_UNI_NAMING_TEST_versions"; + "UNI_NAMING_TEST_versions"; @Test public void testTableName() { assert "UNI_NAMING_TEST_versions".equals( @@ -93,11 +93,12 @@ boolean id2Found = false; while (columns.hasNext()) { - if ("ID_1".equals(columns.next().getName())) { + Column column = columns.next(); + if ("ID_1".equals(column.getName())) { id1Found = true; } - if ("ID_2".equals(columns.next().getName())) { + if ("ID_2".equals(column.getName())) { id2Found = true; } } From jboss-envers-commits at lists.jboss.org Sun Sep 21 08:51:10 2008 From: jboss-envers-commits at lists.jboss.org (jboss-envers-commits at lists.jboss.org) Date: Sun, 21 Sep 2008 08:51:10 -0400 Subject: [jboss-envers-commits] JBoss Envers SVN: r153 - in trunk: src/main/org/jboss/envers/configuration/metadata and 4 other directories. Message-ID: Author: adamw Date: 2008-09-21 08:51:09 -0400 (Sun, 21 Sep 2008) New Revision: 153 Added: trunk/src/test/org/jboss/envers/test/entities/collection/ trunk/src/test/org/jboss/envers/test/entities/collection/StringSetEntity.java trunk/src/test/org/jboss/envers/test/integration/collection/ trunk/src/test/org/jboss/envers/test/integration/collection/StringSet.java Modified: trunk/resources/test/testng.xml trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java Log: ENVERS-42: support for sets, with a test Modified: trunk/resources/test/testng.xml =================================================================== --- trunk/resources/test/testng.xml 2008-09-21 12:21:25 UTC (rev 152) +++ trunk/resources/test/testng.xml 2008-09-21 12:51:09 UTC (rev 153) @@ -4,6 +4,7 @@ + Modified: trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java =================================================================== --- trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java 2008-09-21 12:21:25 UTC (rev 152) +++ trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java 2008-09-21 12:51:09 UTC (rev 153) @@ -261,12 +261,16 @@ // ****** // Storing information about this relation. // ****** - if (value.isInverse()) { - mainGenerator.getEntitiesConfigurations().get(entityName).addToManyMiddleNotOwningRelation(name, mappedBy, - referencedEntityName); - } else { - mainGenerator.getEntitiesConfigurations().get(entityName).addToManyMiddleRelation(name, - referencedEntityName); + + // Only if this is a relation (when there is a referenced entity). + if (referencedEntityName != null) { + if (value.isInverse()) { + mainGenerator.getEntitiesConfigurations().get(entityName).addToManyMiddleNotOwningRelation(name, mappedBy, + referencedEntityName); + } else { + mainGenerator.getEntitiesConfigurations().get(entityName).addToManyMiddleRelation(name, + referencedEntityName); + } } } Added: trunk/src/test/org/jboss/envers/test/entities/collection/StringSetEntity.java =================================================================== --- trunk/src/test/org/jboss/envers/test/entities/collection/StringSetEntity.java (rev 0) +++ trunk/src/test/org/jboss/envers/test/entities/collection/StringSetEntity.java 2008-09-21 12:51:09 UTC (rev 153) @@ -0,0 +1,63 @@ +package org.jboss.envers.test.entities.collection; + +import org.jboss.envers.Versioned; +import org.hibernate.annotations.CollectionOfElements; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.GeneratedValue; +import java.util.Set; +import java.util.HashSet; + +/** + * @author Adam Warski (adam at warski dot org) + */ + at Entity +public class StringSetEntity { + @Id + @GeneratedValue + private Integer id; + + @Versioned + @CollectionOfElements + private Set strings; + + public StringSetEntity() { + strings = new HashSet(); + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public Set getStrings() { + return strings; + } + + public void setStrings(Set strings) { + this.strings = strings; + } + + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof StringSetEntity)) return false; + + StringSetEntity that = (StringSetEntity) 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 "SSE(id = " + id + ", strings = " + strings + ")"; + } +} \ No newline at end of file Copied: trunk/src/test/org/jboss/envers/test/integration/collection/StringSet.java (from rev 148, trunk/src/test/org/jboss/envers/test/integration/manytomany/BasicSet.java) =================================================================== --- trunk/src/test/org/jboss/envers/test/integration/collection/StringSet.java (rev 0) +++ trunk/src/test/org/jboss/envers/test/integration/collection/StringSet.java 2008-09-21 12:51:09 UTC (rev 153) @@ -0,0 +1,101 @@ +package org.jboss.envers.test.integration.collection; + +import org.jboss.envers.test.integration.AbstractEntityTest; +import org.jboss.envers.test.tools.TestTools; +import org.jboss.envers.test.entities.collection.StringSetEntity; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; +import org.hibernate.ejb.Ejb3Configuration; + +import javax.persistence.EntityManager; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; + +/** + * @author Adam Warski (adam at warski dot org) + */ +public class StringSet extends AbstractEntityTest { + private Integer sse1_id; + private Integer sse2_id; + + public void configure(Ejb3Configuration cfg) { + cfg.addAnnotatedClass(StringSetEntity.class); + } + + @BeforeClass(dependsOnMethods = "init") + public void initData() { + EntityManager em = getEntityManager(); + + StringSetEntity sse1 = new StringSetEntity(); + StringSetEntity sse2 = new StringSetEntity(); + + // Revision 1 + em.getTransaction().begin(); + + sse2.getStrings().add("sse2_string1"); + + em.persist(sse1); + em.persist(sse2); + + em.getTransaction().commit(); + + // Revision 2 + + em.getTransaction().begin(); + + sse1 = em.find(StringSetEntity.class, sse1.getId()); + sse2 = em.find(StringSetEntity.class, sse2.getId()); + + sse1.getStrings().add("sse1_string1"); + sse1.getStrings().add("sse1_string2"); + + sse2.getStrings().add("sse2_string1"); + + em.getTransaction().commit(); + + // Revision 3 + em.getTransaction().begin(); + + sse1 = em.find(StringSetEntity.class, sse1.getId()); + sse2 = em.find(StringSetEntity.class, sse2.getId()); + + sse1.getStrings().remove("sse1_string3"); + sse2.getStrings().remove("sse2_string1"); + + em.getTransaction().commit(); + + // + + sse1_id = sse1.getId(); + sse2_id = sse2.getId(); + } + + @Test + public void testRevisionsCounts() { + assert Arrays.asList(1, 2).equals(getVersionsReader().getRevisions(StringSetEntity.class, sse1_id)); + assert Arrays.asList(1, 3).equals(getVersionsReader().getRevisions(StringSetEntity.class, sse2_id)); + } + + @Test + public void testHistoryOfSse1() { + StringSetEntity rev1 = getVersionsReader().find(StringSetEntity.class, sse1_id, 1); + StringSetEntity rev2 = getVersionsReader().find(StringSetEntity.class, sse1_id, 2); + StringSetEntity rev3 = getVersionsReader().find(StringSetEntity.class, sse1_id, 3); + + assert rev1.getStrings().equals(Collections.EMPTY_SET); + assert rev2.getStrings().equals(TestTools.makeSet("sse1_string1", "sse1_string2")); + assert rev3.getStrings().equals(TestTools.makeSet("sse1_string1", "sse1_string2")); + } + + @Test + public void testHistoryOfSse2() { + StringSetEntity rev1 = getVersionsReader().find(StringSetEntity.class, sse2_id, 1); + StringSetEntity rev2 = getVersionsReader().find(StringSetEntity.class, sse2_id, 2); + StringSetEntity rev3 = getVersionsReader().find(StringSetEntity.class, sse2_id, 3); + + assert rev1.getStrings().equals(TestTools.makeSet("sse2_string1")); + assert rev2.getStrings().equals(TestTools.makeSet("sse2_string1")); + assert rev3.getStrings().equals(Collections.EMPTY_SET); + } +} \ No newline at end of file Property changes on: trunk/src/test/org/jboss/envers/test/integration/collection/StringSet.java ___________________________________________________________________ Name: svn:mergeinfo + From jboss-envers-commits at lists.jboss.org Sun Sep 21 15:13:25 2008 From: jboss-envers-commits at lists.jboss.org (jboss-envers-commits at lists.jboss.org) Date: Sun, 21 Sep 2008 15:13:25 -0400 Subject: [jboss-envers-commits] JBoss Envers SVN: r154 - in trunk: src/main/org/jboss/envers/configuration/metadata and 10 other directories. Message-ID: Author: adamw Date: 2008-09-21 15:13:24 -0400 (Sun, 21 Sep 2008) New Revision: 154 Added: trunk/src/main/org/jboss/envers/entities/mapper/relation/CommonCollectionMapperData.java trunk/src/main/org/jboss/envers/entities/mapper/relation/MapCollectionMapper.java trunk/src/test/org/jboss/envers/test/entities/collection/StringMapEntity.java trunk/src/test/org/jboss/envers/test/integration/collection/StringMap.java Removed: trunk/src/main/org/jboss/envers/entities/mapper/relation/ManyToManyNotOwningMapper.java trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyDetachedMapper.java Modified: trunk/envers.iml 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/id/AbstractIdMapper.java trunk/src/main/org/jboss/envers/entities/mapper/id/IdMapper.java trunk/src/main/org/jboss/envers/entities/mapper/relation/AbstractCollectionMapper.java trunk/src/main/org/jboss/envers/entities/mapper/relation/BasicCollectionMapper.java trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleComponentMapper.java trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleDummyComponentMapper.java trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleMapKeyComponentMapper.java trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleRelatedComponentMapper.java trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleSimpleComponentMapper.java trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/MapCollectionInitializor.java trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/CollectionProxy.java trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/MapProxy.java trunk/src/main/org/jboss/envers/entities/mapper/relation/query/OneEntityQueryGenerator.java trunk/src/main/org/jboss/envers/tools/query/Parameters.java trunk/src/main/org/jboss/envers/tools/query/QueryBuilder.java trunk/src/test/org/jboss/envers/test/integration/collection/StringSet.java trunk/src/test/org/jboss/envers/test/tools/TestTools.java Log: ENVERS-42: fixed support for sets of simple values, added support for maps of simple values Modified: trunk/envers.iml =================================================================== --- trunk/envers.iml 2008-09-21 12:51:09 UTC (rev 153) +++ trunk/envers.iml 2008-09-21 19:13:24 UTC (rev 154) @@ -11,7 +11,7 @@ - + Modified: trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java =================================================================== --- trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java 2008-09-21 12:51:09 UTC (rev 153) +++ trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java 2008-09-21 19:13:24 UTC (rev 154) @@ -6,12 +6,11 @@ import org.hibernate.type.*; import org.jboss.envers.entities.mapper.CompositeMapperBuilder; import org.jboss.envers.entities.mapper.relation.*; -import org.jboss.envers.entities.mapper.relation.lazy.proxy.SetProxy; -import org.jboss.envers.entities.mapper.relation.lazy.proxy.ListProxy; +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.component.MiddleDummyComponentMapper; import org.jboss.envers.entities.mapper.relation.component.MiddleRelatedComponentMapper; import org.jboss.envers.entities.mapper.relation.component.MiddleSimpleComponentMapper; +import org.jboss.envers.entities.mapper.relation.component.MiddleDummyComponentMapper; import org.jboss.envers.entities.mapper.id.IdMapper; import org.jboss.envers.entities.EntityConfiguration; import org.jboss.envers.entities.IdMappingData; @@ -23,6 +22,7 @@ import java.util.*; import java.util.Set; import java.util.List; +import java.util.Map; /** * Generates metadata for collection-valued properties. @@ -169,8 +169,8 @@ // Only valid for an inverse relation; null otherwise. String mappedBy; - String referencingPrefix; - // Only valid if referencedEntityName isn't null. + // The referencing prefix is always for a related entity. So it has always the "_" at the end added. + String referencingPrefixRelated; String referencedPrefix; if (value.isInverse()) { @@ -180,19 +180,19 @@ throw new MappingException("Unable to read the mapped by attribute for " + name); } - referencingPrefix = mappedBy + "_"; - referencedPrefix = StringTools.getLastComponent(referencedEntityName) + "_"; + referencingPrefixRelated = mappedBy + "_"; + referencedPrefix = referencedEntityName == null ? "element" : StringTools.getLastComponent(referencedEntityName); } else { mappedBy = null; - referencingPrefix = StringTools.getLastComponent(entityName) + "_"; - referencedPrefix = name + "_"; + referencingPrefixRelated = StringTools.getLastComponent(entityName) + "_"; + referencedPrefix = referencedEntityName == null ? "element" : name; } // Storing the id data of the referencing entity: original mapper, prefixed mapper and entity name. MiddleIdData referencingIdData = new MiddleIdData( referencingIdMapping.getIdMapper(), - referencingIdMapping.getIdMapper().prefixMappedProperties(referencingPrefix), + referencingIdMapping.getIdMapper().prefixMappedProperties(referencingPrefixRelated), entityName, mainGenerator.getVerEntCfg().getVersionsEntityName(entityName)); @@ -207,7 +207,7 @@ middleEntityXmlId.addAttribute("name", mainGenerator.getVerEntCfg().getOriginalIdPropName()); // Adding related-entity (in this case: the referencing's entity id) id mapping to the xml. - addRelatedToXmlMapping(middleEntityXmlId, referencingPrefix, value.getKey(), referencingIdMapping); + addRelatedToXmlMapping(middleEntityXmlId, referencingPrefixRelated, value.getKey(), referencingIdMapping); // Adding the revision number as a foreign key to the revision info entity to the composite id of the // middle table. @@ -229,7 +229,8 @@ MiddleComponentData indexComponentData; if (value instanceof IndexedCollection) { IndexedCollection indexedValue = (IndexedCollection) value; - indexComponentData = null; + indexComponentData = addValueToMiddleTable(indexedValue.getIndex(), middleEntityXmlId, + queryGeneratorBuilder, "mapkey"); // TODO } else { // No index - creating a dummy mapper. @@ -242,18 +243,31 @@ // ****** // Building the query generator. - MiddleTableQueryGenerator queryGenerator = queryGeneratorBuilder.build(); + MiddleTableQueryGenerator queryGenerator = queryGeneratorBuilder.build(elementComponentData, indexComponentData); + // Creating common data + CommonCollectionMapperData commonCollectionMapperData = new CommonCollectionMapperData( + mainGenerator.getVerEntCfg(), versionsMiddleEntityName, name, referencingIdData, queryGenerator); + // Checking the type of the collection and adding an appropriate mapper. Type type = value.getType(); - if (type instanceof SetType) { - currentMapper.addComposite(name, new BasicCollectionMapper(mainGenerator.getVerEntCfg(), - versionsMiddleEntityName, referencingIdData, HashSet.class, SetProxy.class, queryGenerator, - name, elementComponentData)); + if (type instanceof SortedSetType) { + currentMapper.addComposite(name, new BasicCollectionMapper(commonCollectionMapperData, + TreeSet.class, SortedSetProxy.class, elementComponentData)); + } else if (type instanceof SetType) { + currentMapper.addComposite(name, new BasicCollectionMapper(commonCollectionMapperData, + HashSet.class, SetProxy.class, elementComponentData)); + } else if (type instanceof SortedMapType) { + // Indexed collection, so indexComponentData is not null. + currentMapper.addComposite(name, new MapCollectionMapper(commonCollectionMapperData, + TreeMap.class, SortedMapProxy.class, elementComponentData, indexComponentData)); + } else if (type instanceof MapType) { + // Indexed collection, so indexComponentData is not null. + currentMapper.addComposite(name, new MapCollectionMapper(commonCollectionMapperData, + HashMap.class, MapProxy.class, elementComponentData, indexComponentData)); } else if (type instanceof BagType) { - currentMapper.addComposite(name, new BasicCollectionMapper(mainGenerator.getVerEntCfg(), - versionsMiddleEntityName, referencingIdData, ArrayList.class, ListProxy.class, queryGenerator, - name, elementComponentData)); + currentMapper.addComposite(name, new BasicCollectionMapper(commonCollectionMapperData, + ArrayList.class, ListProxy.class, elementComponentData)); } else { throw new RuntimeException(); } @@ -289,6 +303,8 @@ String prefix) { Type type = value.getType(); if (type instanceof ManyToOneType) { + String prefixRelated = prefix + "_"; + ToOne toOneValue = (ToOne) value; String referencedEntityName = toOneValue.getReferencedEntityName(); IdMappingData referencedIdMapping = mainGenerator.getEntitiesConfigurations().get( @@ -297,11 +313,11 @@ // Adding related-entity (in this case: the referenced entities id) id mapping to the xml only if the // relation isn't inverse (so when middleEntityXml is not null). if (middleEntityXml != null) { - addRelatedToXmlMapping(middleEntityXml, prefix, value, referencedIdMapping); + addRelatedToXmlMapping(middleEntityXml, prefixRelated, value, referencedIdMapping); } // Storing the id data of the referenced entity: original mapper, prefixed mapper and entity name. - IdMapper referencedPrefixedIdMapper = referencedIdMapping.getIdMapper().prefixMappedProperties(prefix); + IdMapper referencedPrefixedIdMapper = referencedIdMapping.getIdMapper().prefixMappedProperties(prefixRelated); MiddleIdData referencedIdData = new MiddleIdData( referencedIdMapping.getIdMapper(), referencedPrefixedIdMapper, @@ -314,9 +330,9 @@ queryGeneratorBuilder.getCurrentIndex()); } else if (type instanceof ImmutableType || type instanceof MutableType) { // TODO: add support for enums, components, custom types - mainGenerator.getBasicMetadataGenerator().addSimpleValue(middleEntityXml, "element", value, null, ModificationStore.FULL, true); + mainGenerator.getBasicMetadataGenerator().addSimpleValue(middleEntityXml, prefix, value, null, ModificationStore.FULL, true); - return new MiddleComponentData(new MiddleSimpleComponentMapper(mainGenerator.getVerEntCfg(), "element"), + return new MiddleComponentData(new MiddleSimpleComponentMapper(mainGenerator.getVerEntCfg(), prefix), queryGeneratorBuilder.getCurrentIndex()); } else { // TODO: throw an exception Modified: trunk/src/main/org/jboss/envers/configuration/metadata/QueryGeneratorBuilder.java =================================================================== --- trunk/src/main/org/jboss/envers/configuration/metadata/QueryGeneratorBuilder.java 2008-09-21 12:51:09 UTC (rev 153) +++ trunk/src/main/org/jboss/envers/configuration/metadata/QueryGeneratorBuilder.java 2008-09-21 19:13:24 UTC (rev 154) @@ -1,6 +1,7 @@ package org.jboss.envers.configuration.metadata; 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.OneEntityQueryGenerator; import org.jboss.envers.entities.mapper.relation.query.TwoEntityQueryGenerator; @@ -34,9 +35,10 @@ idDatas.add(idData); } - MiddleTableQueryGenerator build() { + MiddleTableQueryGenerator build(MiddleComponentData... componentDatas) { if (idDatas.size() == 0) { - return new OneEntityQueryGenerator(verEntCfg, versionsMiddleEntityName, referencingIdData); + return new OneEntityQueryGenerator(verEntCfg, versionsMiddleEntityName, referencingIdData, + componentDatas); } else if (idDatas.size() == 1) { return new TwoEntityQueryGenerator(verEntCfg, versionsMiddleEntityName, referencingIdData, idDatas.get(0)); Modified: trunk/src/main/org/jboss/envers/entities/mapper/id/AbstractIdMapper.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/id/AbstractIdMapper.java 2008-09-21 12:51:09 UTC (rev 153) +++ trunk/src/main/org/jboss/envers/entities/mapper/id/AbstractIdMapper.java 2008-09-21 19:13:24 UTC (rev 154) @@ -30,15 +30,18 @@ * @author Adam Warski (adam at warski dot org) */ public abstract class AbstractIdMapper implements IdMapper { + private Parameters getParametersToUse(Parameters parameters, List paramDatas) { + if (paramDatas.size() > 1) { + return parameters.addSubParameters("and"); + } else { + return parameters; + } + } + public void addIdsEqualToQuery(Parameters parameters, String prefix1, String prefix2) { List paramDatas = mapToQueryParametersFromId(null); - Parameters parametersToUse; - if (paramDatas.size() > 1) { - parametersToUse = parameters.addSubParameters("and"); - } else { - parametersToUse = parameters; - } + Parameters parametersToUse = getParametersToUse(parameters, paramDatas); for (QueryParameterData paramData : paramDatas) { parametersToUse.addWhere(paramData.getProperty(prefix1), false, "=", paramData.getProperty(prefix2), false); @@ -48,18 +51,23 @@ public void addIdEqualsToQuery(Parameters parameters, Object id, String prefix, boolean equals) { List paramDatas = mapToQueryParametersFromId(id); - Parameters parametersToUse; - if (paramDatas.size() > 1) { - parametersToUse = parameters.addSubParameters("and"); - } else { - parametersToUse = parameters; - } + Parameters parametersToUse = getParametersToUse(parameters, paramDatas); for (QueryParameterData paramData : paramDatas) { parametersToUse.addWhereWithParam(paramData.getProperty(prefix), equals ? "=" : "<>", paramData.getValue()); } } + public void addNamedIdEqualsToQuery(Parameters parameters, String prefix, boolean equals) { + List paramDatas = mapToQueryParametersFromId(null); + + Parameters parametersToUse = getParametersToUse(parameters, paramDatas); + + for (QueryParameterData paramData : paramDatas) { + parametersToUse.addWhereWithNamedParam(paramData.getProperty(prefix), equals ? "=" : "<>", paramData.getQueryParameterName()); + } + } + public String getIdsEqualQuery(String prefix1, String prefix2) { List paramDatas = mapToQueryParametersFromId(null); Modified: trunk/src/main/org/jboss/envers/entities/mapper/id/IdMapper.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/id/IdMapper.java 2008-09-21 12:51:09 UTC (rev 153) +++ trunk/src/main/org/jboss/envers/entities/mapper/id/IdMapper.java 2008-09-21 19:13:24 UTC (rev 154) @@ -83,6 +83,16 @@ void addIdEqualsToQuery(Parameters parameters, Object id, String prefix, boolean equals); /** + * Adds query statements, which contains named parameters, which express the property that the id of the entity + * with alias prefix, is equal to the given object. It is the responsibility of the using method to read + * parameter values from the id and specify them on the final query object. + * @param parameters Parameters, to which to add the statements. + * @param prefix Prefix to add to the properties (may be null). + * @param equals Should this query express the "=" relation or the "<>" relation. + */ + void addNamedIdEqualsToQuery(Parameters parameters, String prefix, boolean equals); + + /** * Gets a query string, which contains equalities, which express the property that the id of the entity * with alias prefix, is equal to some values (which are left to be specified later, by specifiying parameter * values) Modified: trunk/src/main/org/jboss/envers/entities/mapper/relation/AbstractCollectionMapper.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/AbstractCollectionMapper.java 2008-09-21 12:51:09 UTC (rev 153) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/AbstractCollectionMapper.java 2008-09-21 19:13:24 UTC (rev 154) @@ -2,28 +2,39 @@ import org.jboss.envers.entities.mapper.PersistentCollectionChangeData; import org.jboss.envers.entities.mapper.PropertyMapper; +import org.jboss.envers.entities.mapper.relation.lazy.initializor.Initializor; import org.jboss.envers.RevisionType; -import org.jboss.envers.configuration.VersionsEntitiesConfiguration; +import org.jboss.envers.exception.VersionsException; +import org.jboss.envers.tools.reflection.ReflectionTools; +import org.jboss.envers.reader.VersionsReaderImplementor; +import org.jboss.envers.configuration.VersionsConfiguration; import org.hibernate.collection.PersistentCollection; +import org.hibernate.property.Setter; import java.util.*; import java.io.Serializable; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Constructor; /** * @author Adam Warski (adam at warski dot org) */ -public abstract class AbstractCollectionMapper implements PropertyMapper { - private final VersionsEntitiesConfiguration verEntCfg; - private final String versionsMiddleEntityName; - private final String collectionReferencingPropertyName; - private final MiddleIdData referencingIdData; +public abstract class AbstractCollectionMapper implements PropertyMapper { + protected final CommonCollectionMapperData commonCollectionMapperData; + protected final Class collectionClass; - protected AbstractCollectionMapper(VersionsEntitiesConfiguration verEntCfg, String versionsMiddleEntityName, - String collectionReferencingPropertyName, MiddleIdData referencingIdData) { - this.verEntCfg = verEntCfg; - this.versionsMiddleEntityName = versionsMiddleEntityName; - this.collectionReferencingPropertyName = collectionReferencingPropertyName; - this.referencingIdData = referencingIdData; + private final Constructor proxyConstructor; + + protected AbstractCollectionMapper(CommonCollectionMapperData commonCollectionMapperData, + Class collectionClass, Class proxyClass) { + this.commonCollectionMapperData = commonCollectionMapperData; + this.collectionClass = collectionClass; + + try { + proxyConstructor = proxyClass.getConstructor(Initializor.class); + } catch (NoSuchMethodException e) { + throw new VersionsException(e); + } } protected abstract Collection getNewCollectionContent(PersistentCollection newCollection); @@ -41,16 +52,17 @@ for (Object changedObj : changed) { Map entityData = new HashMap(); Map originalId = new HashMap(); - entityData.put(verEntCfg.getOriginalIdPropName(), originalId); + entityData.put(commonCollectionMapperData.getVerEntCfg().getOriginalIdPropName(), originalId); - collectionChanges.add(new PersistentCollectionChangeData(versionsMiddleEntityName, entityData, changedObj)); + collectionChanges.add(new PersistentCollectionChangeData( + commonCollectionMapperData.getVersionsMiddleEntityName(), entityData, changedObj)); // Mapping the collection owner's id. - referencingIdData.getPrefixedMapper().mapToMapFromId(originalId, id); + commonCollectionMapperData.getReferencingIdData().getPrefixedMapper().mapToMapFromId(originalId, id); // Mapping collection element and index (if present). mapToMapFromObject(originalId, changedObj); - entityData.put(verEntCfg.getRevisionTypePropName(), revisionType); + entityData.put(commonCollectionMapperData.getVerEntCfg().getRevisionTypePropName(), revisionType); } } @@ -58,7 +70,7 @@ public List mapCollectionChanges(String referencingPropertyName, PersistentCollection newColl, Serializable oldColl, Serializable id) { - if (!collectionReferencingPropertyName.equals(referencingPropertyName)) { + if (!commonCollectionMapperData.getCollectionReferencingPropertyName().equals(referencingPropertyName)) { return null; } @@ -87,4 +99,23 @@ // Changes are mapped in the "mapCollectionChanges" method. return false; } + + protected abstract Initializor getInitializor(VersionsConfiguration verCfg, + VersionsReaderImplementor versionsReader, Object primaryKey, + Number revision); + + public void mapToEntityFromMap(VersionsConfiguration verCfg, Object obj, Map data, Object primaryKey, + VersionsReaderImplementor versionsReader, Number revision) { + Setter setter = ReflectionTools.getSetter(obj.getClass(), + commonCollectionMapperData.getCollectionReferencingPropertyName()); + try { + setter.set(obj, proxyConstructor.newInstance(getInitializor(verCfg, versionsReader, primaryKey, revision)), null); + } catch (InstantiationException e) { + throw new VersionsException(e); + } catch (IllegalAccessException e) { + throw new VersionsException(e); + } catch (InvocationTargetException e) { + throw new VersionsException(e); + } + } } Modified: trunk/src/main/org/jboss/envers/entities/mapper/relation/BasicCollectionMapper.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/BasicCollectionMapper.java 2008-09-21 12:51:09 UTC (rev 153) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/BasicCollectionMapper.java 2008-09-21 19:13:24 UTC (rev 154) @@ -1,71 +1,33 @@ 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.entities.mapper.relation.query.MiddleTableQueryGenerator; import org.jboss.envers.entities.mapper.relation.lazy.initializor.Initializor; import org.jboss.envers.entities.mapper.relation.lazy.initializor.BasicCollectionInitializor; import org.jboss.envers.configuration.VersionsConfiguration; -import org.jboss.envers.configuration.VersionsEntitiesConfiguration; import org.jboss.envers.reader.VersionsReaderImplementor; -import org.jboss.envers.tools.reflection.ReflectionTools; -import org.jboss.envers.exception.VersionsException; import org.hibernate.collection.PersistentCollection; -import org.hibernate.property.Setter; import java.util.Map; -import java.util.List; import java.util.Collection; import java.io.Serializable; -import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; /** * @author Adam Warski (adam at warski dot org) */ -public final class BasicCollectionMapper extends AbstractCollectionMapper implements PropertyMapper { - private final Class collectionClass; - private final MiddleTableQueryGenerator queryGenerator; - private final String propertyName; +public final class BasicCollectionMapper extends AbstractCollectionMapper implements PropertyMapper { private final MiddleComponentData elementComponentData; - private final Constructor proxyConstructor; - - public BasicCollectionMapper(VersionsEntitiesConfiguration verEntCfg, String versionsMiddleEntityName, - MiddleIdData referencingIdData, + public BasicCollectionMapper(CommonCollectionMapperData commonCollectionMapperData, Class collectionClass, Class proxyClass, - MiddleTableQueryGenerator queryGenerator, - String propertyName, MiddleComponentData elementComponentData) { - super(verEntCfg, versionsMiddleEntityName, propertyName, referencingIdData); - this.collectionClass = collectionClass; - this.queryGenerator = queryGenerator; - this.propertyName = propertyName; + MiddleComponentData elementComponentData) { + super(commonCollectionMapperData, collectionClass, proxyClass); this.elementComponentData = elementComponentData; - - try { - proxyConstructor = proxyClass.getConstructor(Initializor.class); - } catch (NoSuchMethodException e) { - throw new VersionsException(e); - } } - public void mapToEntityFromMap(VersionsConfiguration verCfg, Object obj, Map data, Object primaryKey, - VersionsReaderImplementor versionsReader, Number revision) { - // Creating the initializator and passing to the proxy, which will use it to initialize the collection - // when and if it is accessed. - Initializor collectionInitializor = new BasicCollectionInitializor(verCfg, - versionsReader, queryGenerator, primaryKey, revision, collectionClass, elementComponentData); - - Setter setter = ReflectionTools.getSetter(obj.getClass(), propertyName); - try { - setter.set(obj, proxyConstructor.newInstance(collectionInitializor), null); - } catch (InstantiationException e) { - throw new VersionsException(e); - } catch (IllegalAccessException e) { - throw new VersionsException(e); - } catch (InvocationTargetException e) { - throw new VersionsException(e); - } + protected Initializor getInitializor(VersionsConfiguration verCfg, VersionsReaderImplementor versionsReader, + Object primaryKey, Number revision) { + return new BasicCollectionInitializor(verCfg, versionsReader, commonCollectionMapperData.getQueryGenerator(), + primaryKey, revision, collectionClass, elementComponentData); } protected Collection getNewCollectionContent(PersistentCollection newCollection) { @@ -73,7 +35,9 @@ } protected Collection getOldCollectionContent(Serializable oldCollection) { - if (oldCollection instanceof Map) { + if (oldCollection == null) { + return null; + } else if (oldCollection instanceof Map) { return ((Map) oldCollection).keySet(); } else { return (Collection) oldCollection; Added: trunk/src/main/org/jboss/envers/entities/mapper/relation/CommonCollectionMapperData.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/CommonCollectionMapperData.java (rev 0) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/CommonCollectionMapperData.java 2008-09-21 19:13:24 UTC (rev 154) @@ -0,0 +1,46 @@ +package org.jboss.envers.entities.mapper.relation; + +import org.jboss.envers.configuration.VersionsEntitiesConfiguration; +import org.jboss.envers.entities.mapper.relation.query.MiddleTableQueryGenerator; + +/** + * Data that is used by all collection mappers, regardless of the type. + * @author Adam Warski (adam at warski dot org) + */ +public final class CommonCollectionMapperData { + private final VersionsEntitiesConfiguration verEntCfg; + private final String versionsMiddleEntityName; + private final String collectionReferencingPropertyName; + private final MiddleIdData referencingIdData; + private final MiddleTableQueryGenerator queryGenerator; + + public CommonCollectionMapperData(VersionsEntitiesConfiguration verEntCfg, String versionsMiddleEntityName, + String collectionReferencingPropertyName, MiddleIdData referencingIdData, + MiddleTableQueryGenerator queryGenerator) { + this.verEntCfg = verEntCfg; + this.versionsMiddleEntityName = versionsMiddleEntityName; + this.collectionReferencingPropertyName = collectionReferencingPropertyName; + this.referencingIdData = referencingIdData; + this.queryGenerator = queryGenerator; + } + + public VersionsEntitiesConfiguration getVerEntCfg() { + return verEntCfg; + } + + public String getVersionsMiddleEntityName() { + return versionsMiddleEntityName; + } + + public String getCollectionReferencingPropertyName() { + return collectionReferencingPropertyName; + } + + public MiddleIdData getReferencingIdData() { + return referencingIdData; + } + + public MiddleTableQueryGenerator getQueryGenerator() { + return queryGenerator; + } +} Deleted: trunk/src/main/org/jboss/envers/entities/mapper/relation/ManyToManyNotOwningMapper.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/ManyToManyNotOwningMapper.java 2008-09-21 12:51:09 UTC (rev 153) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/ManyToManyNotOwningMapper.java 2008-09-21 19:13:24 UTC (rev 154) @@ -1,82 +0,0 @@ -/* - * JBoss, Home of Professional Open Source - * - * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated - * by the @authors tag. All rights reserved. - * - * See the copyright.txt in the distribution for a full listing of individual - * contributors. 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.entities.mapper.relation.lazy.DetachedRelationInitializor; -import org.jboss.envers.entities.mapper.relation.lazy.initializor.Initializor; -import org.jboss.envers.entities.mapper.id.IdMapper; -import org.jboss.envers.reader.VersionsReaderImplementor; -import org.jboss.envers.configuration.VersionsEntitiesConfiguration; -import org.jboss.envers.configuration.VersionsConfiguration; -import org.hibernate.collection.PersistentCollection; - -import java.util.*; -import java.io.Serializable; - -/** - * @author Adam Warski (adam at warski dot org) - */ -public class ManyToManyNotOwningMapper extends AbstractOneToManyMapper implements PropertyMapper { - /** - * Name of the entity that declares the relation. - */ - private final String referencedEntityName; - - // private final DetachedRelationQueryGenerator queryGenerator; - - public ManyToManyNotOwningMapper(VersionsEntitiesConfiguration verEntCfg, String referencingEntityName, - String referencedEntityName, String collectionReferencingPropertyName, - String versionsReferencedEntityName, String versionsMiddleEntityName, - IdMapper referencingMiddleIdMapper, IdMapper referencedMiddleIdMapper, - IdMapper referencedIdMapper) { - super(referencingEntityName, collectionReferencingPropertyName); - - this.referencedEntityName = referencedEntityName; - - // queryGenerator = new DetachedRelationQueryGenerator(verEntCfg, versionsReferencedEntityName, - // versionsMiddleEntityName, referencingMiddleIdMapper, referencedMiddleIdMapper, referencedIdMapper); - } - - public List mapCollectionChanges(String referencingPropertyName, - PersistentCollection newColl, - Serializable oldColl, Serializable id) { - // This is the not-owning side of the collection. - return null; - } - - public boolean mapToMapFromEntity(Map data, Object newObj, Object oldObj) { - // This is the not-owning side of the collection. - return false; - } - - protected Initializor getInitializator(VersionsConfiguration verCfg, - VersionsReaderImplementor versionsReader, - Class entityClass, Object primaryKey, - Number revision, Class collectionClass) { - return null; //new DetachedRelationInitializor(verCfg, referencedEntityName, queryGenerator, - // versionsReader, primaryKey, revision, collectionClass); - } -} \ No newline at end of file Copied: trunk/src/main/org/jboss/envers/entities/mapper/relation/MapCollectionMapper.java (from rev 151, trunk/src/main/org/jboss/envers/entities/mapper/relation/BasicCollectionMapper.java) =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/MapCollectionMapper.java (rev 0) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/MapCollectionMapper.java 2008-09-21 19:13:24 UTC (rev 154) @@ -0,0 +1,55 @@ +package org.jboss.envers.entities.mapper.relation; + +import org.jboss.envers.entities.mapper.PropertyMapper; +import org.jboss.envers.entities.mapper.relation.lazy.initializor.Initializor; +import org.jboss.envers.entities.mapper.relation.lazy.initializor.MapCollectionInitializor; +import org.jboss.envers.configuration.VersionsConfiguration; +import org.jboss.envers.reader.VersionsReaderImplementor; +import org.hibernate.collection.PersistentCollection; + +import java.util.Map; +import java.util.Collection; +import java.io.Serializable; + +/** + * @author Adam Warski (adam at warski dot org) + */ +public final class MapCollectionMapper extends AbstractCollectionMapper implements PropertyMapper { + private final MiddleComponentData elementComponentData; + private final MiddleComponentData indexComponentData; + + public MapCollectionMapper(CommonCollectionMapperData commonCollectionMapperData, + Class collectionClass, Class proxyClass, + MiddleComponentData elementComponentData, MiddleComponentData indexComponentData) { + super(commonCollectionMapperData, collectionClass, proxyClass); + this.elementComponentData = elementComponentData; + this.indexComponentData = indexComponentData; + } + + protected Initializor getInitializor(VersionsConfiguration verCfg, VersionsReaderImplementor versionsReader, + Object primaryKey, Number revision) { + return new MapCollectionInitializor(verCfg, versionsReader, commonCollectionMapperData.getQueryGenerator(), + primaryKey, revision, collectionClass, elementComponentData, indexComponentData); + } + + protected Collection getNewCollectionContent(PersistentCollection newCollection) { + if (newCollection == null) { + return null; + } else { + return ((Map) newCollection).entrySet(); + } + } + + protected Collection getOldCollectionContent(Serializable oldCollection) { + if (oldCollection == null) { + return null; + } else { + return ((Map) oldCollection).entrySet(); + } + } + + protected void mapToMapFromObject(Map data, Object changed) { + elementComponentData.getComponentMapper().mapToMapFromObject(data, ((Map.Entry) changed).getValue()); + indexComponentData.getComponentMapper().mapToMapFromObject(data, ((Map.Entry) changed).getKey()); + } +} \ No newline at end of file Property changes on: trunk/src/main/org/jboss/envers/entities/mapper/relation/MapCollectionMapper.java ___________________________________________________________________ Name: svn:mergeinfo + Deleted: trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyDetachedMapper.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyDetachedMapper.java 2008-09-21 12:51:09 UTC (rev 153) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyDetachedMapper.java 2008-09-21 19:13:24 UTC (rev 154) @@ -1,144 +0,0 @@ -/* - * JBoss, Home of Professional Open Source - * - * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated - * by the @authors tag. All rights reserved. - * - * See the copyright.txt in the distribution for a full listing of individual - * contributors. 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.entities.mapper.relation.lazy.DetachedRelationInitializor; -import org.jboss.envers.entities.mapper.relation.lazy.initializor.Initializor; -import org.jboss.envers.entities.mapper.id.IdMapper; -import org.jboss.envers.reader.VersionsReaderImplementor; -import org.jboss.envers.configuration.VersionsEntitiesConfiguration; -import org.jboss.envers.configuration.VersionsConfiguration; -import org.jboss.envers.RevisionType; -import org.hibernate.collection.PersistentCollection; - -import java.util.*; -import java.io.Serializable; - -/** - * @author Adam Warski (adam at warski dot org) - */ -public class OneToManyDetachedMapper extends AbstractOneToManyMapper implements PropertyMapper { - private final VersionsEntitiesConfiguration verEntCfg; - /** - * Name of the entity that declares the relation. - */ - private final String referencedEntityName; - /** - * Name of the property in referencedEntityName that holds the value of the relation. - */ - private final String collectionReferencingPropertyName; - /** - * Generated middle entity, which stores the bindings of the relation. - */ - private final String versionsMiddleEntityName; - private final IdMapper referencingMiddleIdMapper; - private final IdMapper referencedMiddleIdMapper; - - //private final DetachedRelationQueryGenerator queryGenerator; - - public OneToManyDetachedMapper(VersionsEntitiesConfiguration verEntCfg, String referencingEntityName, - String referencedEntityName, String collectionReferencingPropertyName, - String versionsReferencedEntityName, String versionsMiddleEntityName, - IdMapper referencingMiddleIdMapper, IdMapper referencedMiddleIdMapper, - IdMapper referencedIdMapper) { - super(referencingEntityName, collectionReferencingPropertyName); - - this.verEntCfg = verEntCfg; - this.referencedEntityName = referencedEntityName; - this.collectionReferencingPropertyName = collectionReferencingPropertyName; - this.versionsMiddleEntityName = versionsMiddleEntityName; - this.referencingMiddleIdMapper = referencingMiddleIdMapper; - this.referencedMiddleIdMapper = referencedMiddleIdMapper; - - //queryGenerator = new DetachedRelationQueryGenerator(verEntCfg, versionsReferencedEntityName, - // versionsMiddleEntityName, referencingMiddleIdMapper, referencedMiddleIdMapper, referencedIdMapper); - } - - private Collection getOldCollection(Serializable oldColl) { - if (oldColl instanceof Map) { - return ((Map) oldColl).keySet(); - } else { - return (Collection) oldColl; - } - } - - private void addCollectionChanges(List collectionChanges, Set changed, - RevisionType revisionType, Serializable id) { - for (Object changedEntity : changed) { - Map entityData = new HashMap(); - Map originalId = new HashMap(); - entityData.put(verEntCfg.getOriginalIdPropName(), originalId); - - collectionChanges.add(new PersistentCollectionChangeData(versionsMiddleEntityName, entityData, changedEntity)); - referencingMiddleIdMapper.mapToMapFromId(originalId, id); - referencedMiddleIdMapper.mapToMapFromEntity(originalId, changedEntity); - - entityData.put(verEntCfg.getRevisionTypePropName(), revisionType); - } - } - - @SuppressWarnings({"unchecked"}) - public List mapCollectionChanges(String referencingPropertyName, - PersistentCollection newColl, - Serializable oldColl, Serializable id) { - if (!collectionReferencingPropertyName.equals(referencingPropertyName)) { - return null; - } - - List collectionChanges = new ArrayList(); - - Collection newCollection = (Collection) newColl; - Collection oldCollection = getOldCollection(oldColl); - - Set added = new HashSet(); - if (newColl != null) { added.addAll(newCollection); } - if (oldColl != null) { added.removeAll(oldCollection); } - - addCollectionChanges(collectionChanges, added, RevisionType.ADD, id); - - Set deleted = new HashSet(); - if (oldColl != null) { deleted.addAll(oldCollection); } - if (newColl != null) { deleted.removeAll(newCollection); } - - addCollectionChanges(collectionChanges, deleted, RevisionType.DEL, id); - - return collectionChanges; - } - - @SuppressWarnings({"unchecked"}) - public boolean mapToMapFromEntity(Map data, Object newObj, Object oldObj) { - // Changes are mapped in the "mapCollectionChanges" method. - return false; - } - - protected Initializor getInitializator(VersionsConfiguration verCfg, - VersionsReaderImplementor versionsReader, - Class entityClass, Object primaryKey, - Number revision, Class collectionClass) { - return null;// new DetachedRelationInitializor(verCfg, referencedEntityName, queryGenerator, - //versionsReader, primaryKey, revision, collectionClass); - } -} Modified: trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleComponentMapper.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleComponentMapper.java 2008-09-21 12:51:09 UTC (rev 153) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleComponentMapper.java 2008-09-21 19:13:24 UTC (rev 154) @@ -1,6 +1,7 @@ package org.jboss.envers.entities.mapper.relation.component; import org.jboss.envers.entities.EntityInstantiator; +import org.jboss.envers.tools.query.Parameters; import java.util.Map; @@ -24,4 +25,14 @@ * @param obj Object to map from. */ void mapToMapFromObject(Map data, Object obj); + + /** + * Adds query statements, which contains restrictions, which express the property that part of the middle + * entity with alias prefix1, is equal to part of the middle entity with alias prefix2 (the entity is the same). + * The part is the component's representation in the middle entity. + * @param parameters Parameters, to which to add the statements. + * @param prefix1 First alias of the entity + prefix to add to the properties. + * @param prefix2 Second alias of the entity + prefix to add to the properties. + */ + void addMiddleEqualToQuery(Parameters parameters, String prefix1, String prefix2); } Modified: trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleDummyComponentMapper.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleDummyComponentMapper.java 2008-09-21 12:51:09 UTC (rev 153) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleDummyComponentMapper.java 2008-09-21 19:13:24 UTC (rev 154) @@ -1,12 +1,12 @@ package org.jboss.envers.entities.mapper.relation.component; import org.jboss.envers.entities.EntityInstantiator; +import org.jboss.envers.tools.query.Parameters; import java.util.Map; /** * @author Adam Warski (adam at warski dot org) - * @deprecated */ public final class MiddleDummyComponentMapper implements MiddleComponentMapper { public Object mapToObjectFromFullMap(EntityInstantiator entityInstantiator, Map data, Number revision) { @@ -15,4 +15,7 @@ public void mapToMapFromObject(Map data, Object obj) { } + + public void addMiddleEqualToQuery(Parameters parameters, String prefix1, String prefix2) { + } } \ No newline at end of file Modified: 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-21 12:51:09 UTC (rev 153) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleMapKeyComponentMapper.java 2008-09-21 19:13:24 UTC (rev 154) @@ -2,6 +2,7 @@ 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; @@ -26,4 +27,8 @@ public void mapToMapFromObject(Map data, Object obj) { // Doing nothing. } + + public void addMiddleEqualToQuery(Parameters parameters, String prefix1, String prefix2) { + // Doing nothing. + } } \ No newline at end of file Modified: trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleRelatedComponentMapper.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleRelatedComponentMapper.java 2008-09-21 12:51:09 UTC (rev 153) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleRelatedComponentMapper.java 2008-09-21 19:13:24 UTC (rev 154) @@ -2,6 +2,7 @@ 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; @@ -23,4 +24,8 @@ public void mapToMapFromObject(Map data, Object obj) { relatedIdData.getPrefixedMapper().mapToMapFromEntity(data, obj); } + + public void addMiddleEqualToQuery(Parameters parameters, String prefix1, String prefix2) { + relatedIdData.getPrefixedMapper().addIdsEqualToQuery(parameters, prefix1, prefix2); + } } Modified: trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleSimpleComponentMapper.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleSimpleComponentMapper.java 2008-09-21 12:51:09 UTC (rev 153) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleSimpleComponentMapper.java 2008-09-21 19:13:24 UTC (rev 154) @@ -2,6 +2,7 @@ import org.jboss.envers.entities.EntityInstantiator; import org.jboss.envers.configuration.VersionsEntitiesConfiguration; +import org.jboss.envers.tools.query.Parameters; import java.util.Map; @@ -25,4 +26,8 @@ public void mapToMapFromObject(Map data, Object obj) { data.put(propertyName, obj); } + + public void addMiddleEqualToQuery(Parameters parameters, String prefix1, String prefix2) { + parameters.addWhere(prefix1 + "." + propertyName, false, "=", prefix2 + "." + propertyName, false); + } } 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-21 12:51:09 UTC (rev 153) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/MapCollectionInitializor.java 2008-09-21 19:13:24 UTC (rev 154) @@ -12,8 +12,8 @@ * Initializes a map. * @author Adam Warski (adam at warski dot org) */ -public class MapCollectionInitializor> extends AbstractCollectionInitializor { - private final Class collectionClass; +public class MapCollectionInitializor extends AbstractCollectionInitializor { + private final Class collectionClass; private final MiddleComponentData elementComponentData; private final MiddleComponentData indexComponentData; @@ -21,7 +21,7 @@ VersionsReaderImplementor versionsReader, MiddleTableQueryGenerator queryGenerator, Object primaryKey, Number revision, - Class collectionClass, + Class collectionClass, MiddleComponentData elementComponentData, MiddleComponentData indexComponentData) { super(verCfg, versionsReader, queryGenerator, primaryKey, revision); Modified: trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/CollectionProxy.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/CollectionProxy.java 2008-09-21 12:51:09 UTC (rev 153) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/CollectionProxy.java 2008-09-21 19:13:24 UTC (rev 154) @@ -74,6 +74,7 @@ } public boolean add(U o) { + checkInit(); return delegate.add(o); } Modified: trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/MapProxy.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/MapProxy.java 2008-09-21 12:51:09 UTC (rev 153) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/proxy/MapProxy.java 2008-09-21 19:13:24 UTC (rev 154) @@ -106,12 +106,20 @@ return delegate.entrySet(); } + @Override + public String toString() { + checkInit(); + return delegate.toString(); + } + @SuppressWarnings({"EqualsWhichDoesntCheckParameterClass"}) - public boolean equals(Object o) { + @Override + public boolean equals(Object obj) { checkInit(); - return delegate.equals(o); + return delegate.equals(obj); } + @Override public int hashCode() { checkInit(); return delegate.hashCode(); 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-21 12:51:09 UTC (rev 153) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/query/OneEntityQueryGenerator.java 2008-09-21 19:13:24 UTC (rev 154) @@ -1,12 +1,17 @@ package org.jboss.envers.entities.mapper.relation.query; import org.jboss.envers.entities.mapper.relation.MiddleIdData; +import org.jboss.envers.entities.mapper.relation.MiddleComponentData; import org.jboss.envers.entities.mapper.id.QueryParameterData; 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; + /** * @author Adam Warski (adam at warski dot org) */ @@ -16,42 +21,53 @@ public OneEntityQueryGenerator(VersionsEntitiesConfiguration verEntCfg, String versionsMiddleEntityName, - MiddleIdData referencingIdData) { + MiddleIdData referencingIdData, + MiddleComponentData... componentDatas) { this.referencingIdData = referencingIdData; /* * The query that we need to create: * SELECT new list(ee) FROM middleEntity ee WHERE - * (only entities referenced by the association) - * ee.id2 = :id2 AND + * (only entities referenced by the association; id_ref_ing = id of the referencing entity) + * ee.originalId.id_ref_ing = :id_ref_ing AND * (the association at revision :revision) * ee.revision = (SELECT max(ee2.revision) FROM middleEntity ee2 - * WHERE ee2.revision <= :revision AND ee2.id2 = :id2) AND + * WHERE ee2.revision <= :revision AND ee2.originalId.* = ee.originalId.*) AND * (only non-deleted entities and associations) * ee.revision_type != DEL */ String revisionPropertyPath = verEntCfg.getRevisionPropPath(); String originalIdPropertyName = verEntCfg.getOriginalIdPropName(); - StringBuilder querySB = new StringBuilder(); - querySB - .append("SELECT new list(ee) FROM ").append(versionsMiddleEntityName).append(" ee ") - .append("WHERE ") - // ee.id2 = :id2 - .append(referencingIdData.getPrefixedMapper().getIdEqualsQuery("ee." + originalIdPropertyName, true)) - .append(" AND ") - // ee.revision = (SELECT max(ee2.revision) ... - .append("ee.").append(revisionPropertyPath).append(" = (SELECT max(ee2.").append(revisionPropertyPath) - .append(") FROM ").append(versionsMiddleEntityName).append(" ee2 ") - // ee2.revision <= :revision - .append("WHERE ee2.").append(revisionPropertyPath).append(" <= :revision AND ") - // ee2.id2 = :id2) - .append(referencingIdData.getPrefixedMapper().getIdEqualsQuery("ee2." + originalIdPropertyName, true)) - .append(") AND ") - // ee.revision_type != DEL - .append("ee.").append(verEntCfg.getRevisionTypePropName()).append(" != ").append(":delrevisiontype"); + // SELECT new list(ee) FROM middleEntity ee + QueryBuilder qb = new QueryBuilder(versionsMiddleEntityName, "ee"); + qb.addProjection("new list", "ee", false, false); + // WHERE + Parameters rootParameters = qb.getRootParameters(); + // ee.originalId.id_ref_ing = :id_ref_ing + referencingIdData.getPrefixedMapper().addNamedIdEqualsToQuery(rootParameters, originalIdPropertyName, true); + // SELECT max(ee2.revision) FROM middleEntity ee2 + QueryBuilder maxRevQb = qb.newSubQueryBuilder(versionsMiddleEntityName, "ee2"); + maxRevQb.addProjection("max", revisionPropertyPath, false); + // WHERE + Parameters maxRevQbParameters = maxRevQb.getRootParameters(); + // ee2.revision <= :revision + maxRevQbParameters.addWhereWithNamedParam(revisionPropertyPath, "<=", "revision"); + // ee2.originalId.* = ee.originalId.* + String eeOriginalIdPropertyPath = "ee." + originalIdPropertyName; + String ee2OriginalIdPropertyPath = "ee2." + originalIdPropertyName; + referencingIdData.getPrefixedMapper().addIdsEqualToQuery(maxRevQbParameters, eeOriginalIdPropertyPath, ee2OriginalIdPropertyPath); + for (MiddleComponentData componentData : componentDatas) { + componentData.getComponentMapper().addMiddleEqualToQuery(maxRevQbParameters, eeOriginalIdPropertyPath, ee2OriginalIdPropertyPath); + } + // ee.revision = (SELECT max(...) ...) + rootParameters.addWhere(revisionPropertyPath, "=", maxRevQb); + // ee.revision_type != DEL + rootParameters.addWhereWithNamedParam(verEntCfg.getRevisionTypePropName(), "!=", "delrevisiontype"); - queryString = querySB.toString(); + StringBuilder sb = new StringBuilder(); + qb.build(sb, Collections.emptyMap()); + queryString = sb.toString(); } public Query getQuery(VersionsReaderImplementor versionsReader, Object primaryKey, Number revision) { Modified: trunk/src/main/org/jboss/envers/tools/query/Parameters.java =================================================================== --- trunk/src/main/org/jboss/envers/tools/query/Parameters.java 2008-09-21 12:51:09 UTC (rev 153) +++ trunk/src/main/org/jboss/envers/tools/query/Parameters.java 2008-09-21 19:13:24 UTC (rev 154) @@ -135,15 +135,22 @@ } public void addWhereWithParam(String left, boolean addAlias, String op, Object paramValue) { + String paramName = generateQueryParam(); + localQueryParamValues.put(paramName, paramValue); + + addWhereWithNamedParam(left, addAlias, op, paramName); + } + + public void addWhereWithNamedParam(String left, String op, String paramName) { + addWhereWithNamedParam(left, true, op, paramName); + } + + public void addWhereWithNamedParam(String left, boolean addAlias, String op, String paramName) { StringBuilder expression = new StringBuilder(); if (addAlias) { expression.append(alias).append("."); } expression.append(left); - expression.append(" ").append(op).append(" "); - - String paramName = generateQueryParam(); - localQueryParamValues.put(paramName, paramValue); expression.append(":").append(paramName); expressions.add(expression.toString()); Modified: trunk/src/main/org/jboss/envers/tools/query/QueryBuilder.java =================================================================== --- trunk/src/main/org/jboss/envers/tools/query/QueryBuilder.java 2008-09-21 12:51:09 UTC (rev 153) +++ trunk/src/main/org/jboss/envers/tools/query/QueryBuilder.java 2008-09-21 19:13:24 UTC (rev 154) @@ -130,10 +130,14 @@ } public void addProjection(String function, String propertyName, boolean distinct) { + addProjection(function, propertyName, distinct, true); + } + + public void addProjection(String function, String propertyName, boolean distinct, boolean addAlias) { if (function == null) { - projections.add((distinct ? "distinct " : "") + alias + "." + propertyName); + projections.add((distinct ? "distinct " : "") + (addAlias ? alias+ "." : "") + propertyName); } else { - projections.add(function + "(" + (distinct ? "distinct " : "") + alias + "." + propertyName + ")"); + projections.add(function + "(" + (distinct ? "distinct " : "") + (addAlias ? alias + "." : "") + propertyName + ")"); } } @@ -141,7 +145,7 @@ * Builds the given query, appending results to the given string buffer, and adding all query parameter values * that are used to the map provided. * @param sb String builder to which the query will be appended. - * @param queryParamValues Values of parameters used in the query. + * @param queryParamValues Map to which name and values of parameters used in the query should be added. */ public void build(StringBuilder sb, Map queryParamValues) { sb.append("select "); Copied: trunk/src/test/org/jboss/envers/test/entities/collection/StringMapEntity.java (from rev 153, trunk/src/test/org/jboss/envers/test/entities/collection/StringSetEntity.java) =================================================================== --- trunk/src/test/org/jboss/envers/test/entities/collection/StringMapEntity.java (rev 0) +++ trunk/src/test/org/jboss/envers/test/entities/collection/StringMapEntity.java 2008-09-21 19:13:24 UTC (rev 154) @@ -0,0 +1,63 @@ +package org.jboss.envers.test.entities.collection; + +import org.jboss.envers.Versioned; +import org.hibernate.annotations.CollectionOfElements; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.GeneratedValue; +import java.util.Map; +import java.util.HashMap; + +/** + * @author Adam Warski (adam at warski dot org) + */ + at Entity +public class StringMapEntity { + @Id + @GeneratedValue + private Integer id; + + @Versioned + @CollectionOfElements + private Map strings; + + public StringMapEntity() { + strings = new HashMap(); + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public Map getStrings() { + return strings; + } + + public void setStrings(Map strings) { + this.strings = strings; + } + + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof StringMapEntity)) return false; + + StringMapEntity that = (StringMapEntity) 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 "SME(id = " + id + ", strings = " + strings + ")"; + } +} \ No newline at end of file Property changes on: trunk/src/test/org/jboss/envers/test/entities/collection/StringMapEntity.java ___________________________________________________________________ Name: svn:mergeinfo + Copied: trunk/src/test/org/jboss/envers/test/integration/collection/StringMap.java (from rev 153, trunk/src/test/org/jboss/envers/test/integration/collection/StringSet.java) =================================================================== --- trunk/src/test/org/jboss/envers/test/integration/collection/StringMap.java (rev 0) +++ trunk/src/test/org/jboss/envers/test/integration/collection/StringMap.java 2008-09-21 19:13:24 UTC (rev 154) @@ -0,0 +1,115 @@ +package org.jboss.envers.test.integration.collection; + +import org.jboss.envers.test.integration.AbstractEntityTest; +import org.jboss.envers.test.tools.TestTools; +import org.jboss.envers.test.entities.collection.StringMapEntity; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; +import org.hibernate.ejb.Ejb3Configuration; + +import javax.persistence.EntityManager; +import java.util.Arrays; +import java.util.Collections; + +/** + * @author Adam Warski (adam at warski dot org) + */ +public class StringMap extends AbstractEntityTest { + private Integer sme1_id; + private Integer sme2_id; + + public void configure(Ejb3Configuration cfg) { + cfg.addAnnotatedClass(StringMapEntity.class); + } + + @BeforeClass(dependsOnMethods = "init") + public void initData() { + EntityManager em = getEntityManager(); + + StringMapEntity sme1 = new StringMapEntity(); + StringMapEntity sme2 = new StringMapEntity(); + + // Revision 1 (sme1: initialy empty, sme2: initialy 1 mapping) + em.getTransaction().begin(); + + sme2.getStrings().put("1", "a"); + + em.persist(sme1); + em.persist(sme2); + + em.getTransaction().commit(); + + // Revision 2 (sme1: adding 2 mappings, sme2: no changes) + em.getTransaction().begin(); + + sme1 = em.find(StringMapEntity.class, sme1.getId()); + sme2 = em.find(StringMapEntity.class, sme2.getId()); + + sme1.getStrings().put("1", "a"); + sme1.getStrings().put("2", "b"); + + em.getTransaction().commit(); + + // Revision 3 (sme1: removing an existing mapping, sme2: replacing a value) + em.getTransaction().begin(); + + sme1 = em.find(StringMapEntity.class, sme1.getId()); + sme2 = em.find(StringMapEntity.class, sme2.getId()); + + sme1.getStrings().remove("1"); + sme2.getStrings().put("1", "b"); + + em.getTransaction().commit(); + + // Revision 4 (sme1: removing a non-existing mapping, sme2: replacing with the same value) + em.getTransaction().begin(); + + sme1 = em.find(StringMapEntity.class, sme1.getId()); + sme2 = em.find(StringMapEntity.class, sme2.getId()); + + sme1.getStrings().remove("3"); + sme2.getStrings().put("1", "b"); + + em.getTransaction().commit(); + + // + + sme1_id = sme1.getId(); + sme2_id = sme2.getId(); + } + + @Test + public void testRevisionsCounts() { + assert Arrays.asList(1, 2, 3).equals(getVersionsReader().getRevisions(StringMapEntity.class, sme1_id)); + assert Arrays.asList(1, 3).equals(getVersionsReader().getRevisions(StringMapEntity.class, sme2_id)); + } + + @Test + public void testHistoryOfSse1() { + StringMapEntity rev1 = getVersionsReader().find(StringMapEntity.class, sme1_id, 1); + StringMapEntity rev2 = getVersionsReader().find(StringMapEntity.class, sme1_id, 2); + StringMapEntity rev3 = getVersionsReader().find(StringMapEntity.class, sme1_id, 3); + StringMapEntity rev4 = getVersionsReader().find(StringMapEntity.class, sme1_id, 4); + + assert rev1.getStrings().equals(Collections.EMPTY_MAP); + assert rev2.getStrings().equals(TestTools.makeMap("1", "a", "2", "b")); + System.out.println(rev2.getStrings()); + System.out.println(rev3.getStrings()); + System.out.println(rev4.getStrings()); + assert rev3.getStrings().equals(TestTools.makeMap("2", "b")); + assert rev4.getStrings().equals(TestTools.makeMap("2", "b")); + } + + @Test + public void testHistoryOfSse2() { + StringMapEntity rev1 = getVersionsReader().find(StringMapEntity.class, sme2_id, 1); + StringMapEntity rev2 = getVersionsReader().find(StringMapEntity.class, sme2_id, 2); + StringMapEntity rev3 = getVersionsReader().find(StringMapEntity.class, sme2_id, 3); + StringMapEntity rev4 = getVersionsReader().find(StringMapEntity.class, sme2_id, 4); + + assert rev1.getStrings().equals(TestTools.makeMap("1", "a")); + assert rev2.getStrings().equals(TestTools.makeMap("1", "a")); + assert rev3.getStrings().equals(TestTools.makeMap("1", "b")); + assert rev4.getStrings().equals(TestTools.makeMap("1", "b")); + } +} \ No newline at end of file Modified: trunk/src/test/org/jboss/envers/test/integration/collection/StringSet.java =================================================================== --- trunk/src/test/org/jboss/envers/test/integration/collection/StringSet.java 2008-09-21 12:51:09 UTC (rev 153) +++ trunk/src/test/org/jboss/envers/test/integration/collection/StringSet.java 2008-09-21 19:13:24 UTC (rev 154) @@ -10,7 +10,6 @@ import javax.persistence.EntityManager; import java.util.Arrays; import java.util.Collections; -import java.util.HashSet; /** * @author Adam Warski (adam at warski dot org) @@ -30,18 +29,18 @@ StringSetEntity sse1 = new StringSetEntity(); StringSetEntity sse2 = new StringSetEntity(); - // Revision 1 + // Revision 1 (sse1: initialy empty, sse2: initialy 2 elements) em.getTransaction().begin(); sse2.getStrings().add("sse2_string1"); + sse2.getStrings().add("sse2_string2"); em.persist(sse1); em.persist(sse2); em.getTransaction().commit(); - // Revision 2 - + // Revision 2 (sse1: adding 2 elements, sse2: adding an existing element) em.getTransaction().begin(); sse1 = em.find(StringSetEntity.class, sse1.getId()); @@ -54,7 +53,7 @@ em.getTransaction().commit(); - // Revision 3 + // Revision 3 (sse1: removing a non-existing element, sse2: removing one element) em.getTransaction().begin(); sse1 = em.find(StringSetEntity.class, sse1.getId()); @@ -94,8 +93,8 @@ StringSetEntity rev2 = getVersionsReader().find(StringSetEntity.class, sse2_id, 2); StringSetEntity rev3 = getVersionsReader().find(StringSetEntity.class, sse2_id, 3); - assert rev1.getStrings().equals(TestTools.makeSet("sse2_string1")); - assert rev2.getStrings().equals(TestTools.makeSet("sse2_string1")); - assert rev3.getStrings().equals(Collections.EMPTY_SET); + assert rev1.getStrings().equals(TestTools.makeSet("sse2_string1", "sse2_string2")); + assert rev2.getStrings().equals(TestTools.makeSet("sse2_string1", "sse2_string2")); + assert rev3.getStrings().equals(TestTools.makeSet("sse2_string2")); } } \ No newline at end of file Modified: trunk/src/test/org/jboss/envers/test/tools/TestTools.java =================================================================== --- trunk/src/test/org/jboss/envers/test/tools/TestTools.java 2008-09-21 12:51:09 UTC (rev 153) +++ trunk/src/test/org/jboss/envers/test/tools/TestTools.java 2008-09-21 19:13:24 UTC (rev 154) @@ -1,8 +1,6 @@ package org.jboss.envers.test.tools; -import java.util.Set; -import java.util.HashSet; -import java.util.List; +import java.util.*; /** * @author Adam Warski (adam at warski dot org) @@ -18,6 +16,17 @@ return ret; } + public static Map makeMap(Object... objects) { + Map ret = new HashMap(); + // The number of objects must be divisable by 2. + //noinspection ManualArrayToCollectionCopy + for (int i=0; i boolean checkList(List list, T... objects) { if (list.size() != objects.length) { return false; From jboss-envers-commits at lists.jboss.org Sun Sep 21 16:42:00 2008 From: jboss-envers-commits at lists.jboss.org (jboss-envers-commits at lists.jboss.org) Date: Sun, 21 Sep 2008 16:42:00 -0400 Subject: [jboss-envers-commits] JBoss Envers SVN: r155 - in trunk/src/main/org/jboss/envers: entities/mapper/id and 3 other directories. Message-ID: Author: adamw Date: 2008-09-21 16:42:00 -0400 (Sun, 21 Sep 2008) New Revision: 155 Modified: trunk/src/main/org/jboss/envers/configuration/metadata/QueryGeneratorBuilder.java trunk/src/main/org/jboss/envers/entities/mapper/id/AbstractIdMapper.java trunk/src/main/org/jboss/envers/entities/mapper/id/IdMapper.java trunk/src/main/org/jboss/envers/entities/mapper/relation/query/TwoEntityQueryGenerator.java trunk/src/main/org/jboss/envers/synchronization/VersionsSync.java trunk/src/main/org/jboss/envers/tools/query/Parameters.java Log: ENVERS-42: fixed and migrated many-to-many relations querying to the query builder Modified: trunk/src/main/org/jboss/envers/configuration/metadata/QueryGeneratorBuilder.java =================================================================== --- trunk/src/main/org/jboss/envers/configuration/metadata/QueryGeneratorBuilder.java 2008-09-21 19:13:24 UTC (rev 154) +++ trunk/src/main/org/jboss/envers/configuration/metadata/QueryGeneratorBuilder.java 2008-09-21 20:42:00 UTC (rev 155) @@ -41,8 +41,9 @@ componentDatas); } else if (idDatas.size() == 1) { return new TwoEntityQueryGenerator(verEntCfg, versionsMiddleEntityName, referencingIdData, - idDatas.get(0)); + idDatas.get(0), componentDatas); } else if (idDatas.size() == 1) { + // TODO: add componentDatas return new ThreeEntityQueryGenerator(verEntCfg, versionsMiddleEntityName, referencingIdData, idDatas.get(0), idDatas.get(1)); } else { Modified: trunk/src/main/org/jboss/envers/entities/mapper/id/AbstractIdMapper.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/id/AbstractIdMapper.java 2008-09-21 19:13:24 UTC (rev 154) +++ trunk/src/main/org/jboss/envers/entities/mapper/id/AbstractIdMapper.java 2008-09-21 20:42:00 UTC (rev 155) @@ -48,88 +48,39 @@ } } - public void addIdEqualsToQuery(Parameters parameters, Object id, String prefix, boolean equals) { - List paramDatas = mapToQueryParametersFromId(id); - - Parameters parametersToUse = getParametersToUse(parameters, paramDatas); - - for (QueryParameterData paramData : paramDatas) { - parametersToUse.addWhereWithParam(paramData.getProperty(prefix), equals ? "=" : "<>", paramData.getValue()); - } - } - - public void addNamedIdEqualsToQuery(Parameters parameters, String prefix, boolean equals) { - List paramDatas = mapToQueryParametersFromId(null); - - Parameters parametersToUse = getParametersToUse(parameters, paramDatas); - - for (QueryParameterData paramData : paramDatas) { - parametersToUse.addWhereWithNamedParam(paramData.getProperty(prefix), equals ? "=" : "<>", paramData.getQueryParameterName()); - } - } - - public String getIdsEqualQuery(String prefix1, String prefix2) { - List paramDatas = mapToQueryParametersFromId(null); - - StringBuilder query = new StringBuilder(); - - Iterator paramDataIter = paramDatas.iterator(); - - while (paramDataIter.hasNext()) { - QueryParameterData paramData = paramDataIter.next(); - query.append(paramData.getProperty(prefix1)).append(" = ").append(paramData.getProperty(prefix2)); - if (paramDataIter.hasNext()) { - query.append(" AND "); - } - } - - return query.toString(); - } - - public String getIdsEqualQuery(String prefix1, IdMapper mapper2, String prefix2) { + public void addIdsEqualToQuery(Parameters parameters, String prefix1, IdMapper mapper2, String prefix2) { List paramDatas1 = mapToQueryParametersFromId(null); List paramDatas2 = mapper2.mapToQueryParametersFromId(null); - StringBuilder query = new StringBuilder(); + Parameters parametersToUse = getParametersToUse(parameters, paramDatas1); Iterator paramDataIter1 = paramDatas1.iterator(); Iterator paramDataIter2 = paramDatas2.iterator(); while (paramDataIter1.hasNext()) { QueryParameterData paramData1 = paramDataIter1.next(); QueryParameterData paramData2 = paramDataIter2.next(); - query.append(paramData1.getProperty(prefix1)).append(" = ").append(paramData2.getProperty(prefix2)); - if (paramDataIter1.hasNext()) { - query.append(" AND "); - } + parametersToUse.addWhere(paramData1.getProperty(prefix1), false, "=", paramData2.getProperty(prefix2), false); } - - return query.toString(); } - public String getIdEqualsQuery(String prefix, boolean equals) { - List paramDatas = mapToQueryParametersFromId(null); + public void addIdEqualsToQuery(Parameters parameters, Object id, String prefix, boolean equals) { + List paramDatas = mapToQueryParametersFromId(id); - StringBuilder query = new StringBuilder(); + Parameters parametersToUse = getParametersToUse(parameters, paramDatas); - Iterator paramDataIter = paramDatas.iterator(); + for (QueryParameterData paramData : paramDatas) { + parametersToUse.addWhereWithParam(paramData.getProperty(prefix), equals ? "=" : "<>", paramData.getValue()); + } + } - while (paramDataIter.hasNext()) { - QueryParameterData paramData = paramDataIter.next(); + public void addNamedIdEqualsToQuery(Parameters parameters, String prefix, boolean equals) { + List paramDatas = mapToQueryParametersFromId(null); - query.append(paramData.getProperty(prefix)); - if (equals) { - query.append(" = "); - } else { - query.append(" != "); - } - query.append(":").append(paramData.getQueryParameterName()); + Parameters parametersToUse = getParametersToUse(parameters, paramDatas); - if (paramDataIter.hasNext()) { - query.append(" AND "); - } + for (QueryParameterData paramData : paramDatas) { + parametersToUse.addWhereWithNamedParam(paramData.getProperty(prefix), equals ? "=" : "<>", paramData.getQueryParameterName()); } - - return query.toString(); } } Modified: trunk/src/main/org/jboss/envers/entities/mapper/id/IdMapper.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/id/IdMapper.java 2008-09-21 19:13:24 UTC (rev 154) +++ trunk/src/main/org/jboss/envers/entities/mapper/id/IdMapper.java 2008-09-21 20:42:00 UTC (rev 155) @@ -55,22 +55,25 @@ List mapToQueryParametersFromId(Object obj); /** - * Gets a query string, which contains equalities, which express the property that the id of the entity + * Adds query statements, which contains restrictions, which express the property that the id of the entity * with alias prefix1, is equal to the id of the entity with alias prefix2 (the entity is the same). - * @param prefix1 First alias of the entity. - * @param prefix2 Second alias of the entity. - * @return A query string expressing the property described above. + * @param parameters Parameters, to which to add the statements. + * @param prefix1 First alias of the entity + prefix to add to the properties. + * @param prefix2 Second alias of the entity + prefix to add to the properties. */ - String getIdsEqualQuery(String prefix1, String prefix2); + void addIdsEqualToQuery(Parameters parameters, String prefix1, String prefix2); /** * Adds query statements, which contains restrictions, which express the property that the id of the entity - * with alias prefix1, is equal to the id of the entity with alias prefix2 (the entity is the same). + * with alias prefix1, is equal to the id of the entity with alias prefix2 mapped by the second mapper + * (the second mapper must be for the same entity, but it can have, for example, prefixed properties). * @param parameters Parameters, to which to add the statements. * @param prefix1 First alias of the entity + prefix to add to the properties. + * @param mapper2 Second mapper for the same entity, which will be used to get properties for the right side + * of the equation. * @param prefix2 Second alias of the entity + prefix to add to the properties. */ - void addIdsEqualToQuery(Parameters parameters, String prefix1, String prefix2); + void addIdsEqualToQuery(Parameters parameters, String prefix1, IdMapper mapper2, String prefix2); /** * Adds query statements, which contains restrictions, which express the property that the id of the entity @@ -91,26 +94,4 @@ * @param equals Should this query express the "=" relation or the "<>" relation. */ void addNamedIdEqualsToQuery(Parameters parameters, String prefix, boolean equals); - - /** - * Gets a query string, which contains equalities, which express the property that the id of the entity - * with alias prefix, is equal to some values (which are left to be specified later, by specifiying parameter - * values) - * @param prefix Alias of the entity (may be null). - * @param equals Should this query express the "=" relation or the "<>" relation. - * @return A query string expressing the property described above. - */ - String getIdEqualsQuery(String prefix, boolean equals); - - /** - * Gets a query string, which contains equalities, which express the property that the id of the entity - * with alias prefix1, is equal to the id of the entity with alias prefix2, mapped by the second mapper - * (the second mapper must be for the same entity, but it can have, for example, prefixed properties). - * @param prefix1 First alias of the entity. - * @param mapper2 Second mapper for the same entity, which will be used to get properties for the right side - * of the equation. - * @param prefix2 Second alias of the entity. - * @return A query string expressing the property described above. - */ - String getIdsEqualQuery(String prefix1, IdMapper mapper2, String prefix2); } 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-21 19:13:24 UTC (rev 154) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/query/TwoEntityQueryGenerator.java 2008-09-21 20:42:00 UTC (rev 155) @@ -2,11 +2,16 @@ import org.jboss.envers.entities.mapper.id.QueryParameterData; import org.jboss.envers.entities.mapper.relation.MiddleIdData; +import org.jboss.envers.entities.mapper.relation.MiddleComponentData; 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; + /** * @author Adam Warski (adam at warski dot org) */ @@ -17,22 +22,24 @@ public TwoEntityQueryGenerator(VersionsEntitiesConfiguration verEntCfg, String versionsMiddleEntityName, MiddleIdData referencingIdData, - MiddleIdData referencedIdData) { + MiddleIdData referencedIdData, + MiddleComponentData... componentDatas) { this.referencingIdData = referencingIdData; /* * The query that we need to create: * SELECT new list(ee, e) FROM versionsReferencedEntity e, middleEntity ee * WHERE + * (entities referenced by the middle table; id_ref_ed = id of the referenced entity) + * ee.id_ref_ed = e.id_ref_ed AND + * (only entities referenced by the association; id_ref_ing = id of the referencing entity) + * ee.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.id1 = e.id1) AND - * (only entities referenced by the association) - * ee.id1 = e.id1 AND - * ee.id2 = :id2 AND + * WHERE e2.revision <= :revision AND e2.id_ref_ed = e.id_ref_ed) AND * (the association at revision :revision) * ee.revision = (SELECT max(ee2.revision) FROM middleEntity ee2 - * WHERE ee2.revision <= :revision AND ee2.id2 = :id2 AND ee2.id1 = ee.id1) AND + * WHERE ee2.revision <= :revision AND ee2.originalId.* = ee.originalId.*) AND * (only non-deleted entities and associations) * ee.revision_type != DEL AND * e.revision_type != DEL @@ -40,44 +47,58 @@ String revisionPropertyPath = verEntCfg.getRevisionPropPath(); String originalIdPropertyName = verEntCfg.getOriginalIdPropName(); - StringBuilder querySB = new StringBuilder(); - querySB - .append("SELECT new list(ee, e) FROM ").append(referencedIdData.getVersionsEntityName()).append(" e") - .append(", ").append(versionsMiddleEntityName).append(" ee ") - .append("WHERE ") - // e.revision = (SELECT max(e2.revision) ... - .append("e.").append(revisionPropertyPath).append(" = (SELECT max(e2.").append(revisionPropertyPath) - .append(") FROM ").append(referencedIdData.getVersionsEntityName()).append(" e2 ") - // e2.revision <= :revision - .append("WHERE e2.").append(revisionPropertyPath).append(" <= :revision AND ") - // e2.id1 = e.id1) - .append(referencedIdData.getOriginalMapper().getIdsEqualQuery("e." + originalIdPropertyName, "e2." + originalIdPropertyName)) - .append(") AND ") - // ee.id1 = e.id1 - .append(referencedIdData.getPrefixedMapper().getIdsEqualQuery("ee." + originalIdPropertyName, - referencedIdData.getOriginalMapper(), "e." + originalIdPropertyName)) - .append(" AND ") - // ee.id2 = :id2 - .append(referencingIdData.getPrefixedMapper().getIdEqualsQuery("ee." + originalIdPropertyName, true)) - .append(" AND ") - // ee.revision = (SELECT max(ee2.revision) ... - .append("ee.").append(revisionPropertyPath).append(" = (SELECT max(ee2.").append(revisionPropertyPath) - .append(") FROM ").append(versionsMiddleEntityName).append(" ee2 ") - // ee2.revision <= :revision - .append("WHERE ee2.").append(revisionPropertyPath).append(" <= :revision AND ") - // ee2.id2 = :id2) - .append(referencingIdData.getPrefixedMapper().getIdEqualsQuery("ee2." + originalIdPropertyName, true)) - .append(" AND ") - // ee2.id1 = ee.id1) - .append(referencedIdData.getPrefixedMapper().getIdsEqualQuery("ee." + originalIdPropertyName, "ee2." + originalIdPropertyName)) - .append(") AND ") - // e.revision_type != DEL AND - .append("e.").append(verEntCfg.getRevisionTypePropName()).append(" != ").append(":delrevisiontype") - .append(" AND ") - // ee.revision_type != DEL - .append("ee.").append(verEntCfg.getRevisionTypePropName()).append(" != ").append(":delrevisiontype"); + String eeOriginalIdPropertyPath = "ee." + originalIdPropertyName; - queryString = querySB.toString(); + // SELECT new list(ee) FROM middleEntity ee + QueryBuilder qb = new QueryBuilder(versionsMiddleEntityName, "ee"); + qb.addFrom(referencedIdData.getVersionsEntityName(), "e"); + qb.addProjection("new list", "ee, e", false, false); + // WHERE + Parameters rootParameters = qb.getRootParameters(); + // ee.id_ref_ed = e.id_ref_ed + referencedIdData.getPrefixedMapper().addIdsEqualToQuery(rootParameters, eeOriginalIdPropertyPath, + referencedIdData.getOriginalMapper(), "e." + originalIdPropertyName); + // ee.originalId.id_ref_ing = :id_ref_ing + referencingIdData.getPrefixedMapper().addNamedIdEqualsToQuery(rootParameters, originalIdPropertyName, true); + + // SELECT max(e.revision) FROM versionsReferencedEntity e2 + QueryBuilder maxERevQb = qb.newSubQueryBuilder(referencedIdData.getVersionsEntityName(), "e2"); + maxERevQb.addProjection("max", revisionPropertyPath, false); + // WHERE + Parameters maxERevQbParameters = maxERevQb.getRootParameters(); + // e2.revision <= :revision + maxERevQbParameters.addWhereWithNamedParam(revisionPropertyPath, "<=", "revision"); + // e2.id_ref_ed = e.id_ref_ed + referencedIdData.getOriginalMapper().addIdsEqualToQuery(maxERevQbParameters, "e." + originalIdPropertyName, "e2." + originalIdPropertyName); + + // e.revision = (SELECT max(...) ...) + rootParameters.addWhere("e." + revisionPropertyPath, false, "=", maxERevQb); + + // SELECT max(ee2.revision) FROM middleEntity ee2 + QueryBuilder maxEeRevQb = qb.newSubQueryBuilder(versionsMiddleEntityName, "ee2"); + maxEeRevQb.addProjection("max", revisionPropertyPath, false); + // WHERE + Parameters maxEeRevQbParameters = maxEeRevQb.getRootParameters(); + // ee2.revision <= :revision + maxEeRevQbParameters.addWhereWithNamedParam(revisionPropertyPath, "<=", "revision"); + // ee2.originalId.* = ee.originalId.* + String ee2OriginalIdPropertyPath = "ee2." + originalIdPropertyName; + referencingIdData.getPrefixedMapper().addIdsEqualToQuery(maxEeRevQbParameters, eeOriginalIdPropertyPath, ee2OriginalIdPropertyPath); + for (MiddleComponentData componentData : componentDatas) { + componentData.getComponentMapper().addMiddleEqualToQuery(maxEeRevQbParameters, eeOriginalIdPropertyPath, ee2OriginalIdPropertyPath); + } + + // ee.revision = (SELECT max(...) ...) + rootParameters.addWhere(revisionPropertyPath, "=", maxEeRevQb); + + // ee.revision_type != DEL + rootParameters.addWhereWithNamedParam(verEntCfg.getRevisionTypePropName(), "!=", "delrevisiontype"); + // e.revision_type != DEL + rootParameters.addWhereWithNamedParam("e." + verEntCfg.getRevisionTypePropName(), false, "!=", "delrevisiontype"); + + StringBuilder sb = new StringBuilder(); + qb.build(sb, Collections.emptyMap()); + queryString = sb.toString(); } public Query getQuery(VersionsReaderImplementor versionsReader, Object primaryKey, Number revision) { Modified: trunk/src/main/org/jboss/envers/synchronization/VersionsSync.java =================================================================== --- trunk/src/main/org/jboss/envers/synchronization/VersionsSync.java 2008-09-21 19:13:24 UTC (rev 154) +++ trunk/src/main/org/jboss/envers/synchronization/VersionsSync.java 2008-09-21 20:42:00 UTC (rev 155) @@ -129,8 +129,6 @@ if (FlushMode.isManualFlushMode(session.getFlushMode())) { Session temporarySession = null; try { - //noinspection deprecation - //temporarySession = session.getSessionFactory().openSession(session.connection()); temporarySession = session.getFactory().openTemporarySession(); executeInSession(temporarySession); Modified: trunk/src/main/org/jboss/envers/tools/query/Parameters.java =================================================================== --- trunk/src/main/org/jboss/envers/tools/query/Parameters.java 2008-09-21 19:13:24 UTC (rev 154) +++ trunk/src/main/org/jboss/envers/tools/query/Parameters.java 2008-09-21 20:42:00 UTC (rev 155) @@ -178,10 +178,18 @@ } public void addWhere(String left, String op, QueryBuilder right) { + addWhere(left, true, op, right); + } + + public void addWhere(String left, boolean addAlias, String op, QueryBuilder right) { StringBuilder expression = new StringBuilder(); - expression.append(alias).append(".").append(left); + if (addAlias) { + expression.append(alias).append("."); + } + expression.append(left); + expression.append(" ").append(op).append(" "); expression.append("("); From jboss-envers-commits at lists.jboss.org Sun Sep 21 18:00:51 2008 From: jboss-envers-commits at lists.jboss.org (jboss-envers-commits at lists.jboss.org) Date: Sun, 21 Sep 2008 18:00:51 -0400 Subject: [jboss-envers-commits] JBoss Envers SVN: r156 - in trunk/src: main/org/jboss/envers/entities/mapper and 8 other directories. Message-ID: Author: adamw Date: 2008-09-21 18:00:50 -0400 (Sun, 21 Sep 2008) New Revision: 156 Added: trunk/src/test/org/jboss/envers/test/entities/manytomany/MapOwnedEntity.java trunk/src/test/org/jboss/envers/test/entities/manytomany/MapOwningEntity.java trunk/src/test/org/jboss/envers/test/entities/manytomany/unidirectional/ListUniEntity.java trunk/src/test/org/jboss/envers/test/entities/manytomany/unidirectional/MapUniEntity.java trunk/src/test/org/jboss/envers/test/entities/manytomany/unidirectional/SetUniEntity.java trunk/src/test/org/jboss/envers/test/integration/manytomany/BasicMap.java trunk/src/test/org/jboss/envers/test/integration/manytomany/unidirectional/BasicUniMap.java trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicDetachedList.java trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicDetachedSet.java trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicDetachedSetWithEmbId.java trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicDetachedSetWithMulId.java trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/DataChangesDetachedSet.java trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/DoubleDetachedSet.java Removed: trunk/src/test/org/jboss/envers/test/entities/manytomany/unidirectional/ListOwnedUniEntity.java trunk/src/test/org/jboss/envers/test/entities/manytomany/unidirectional/SetOwnedUniEntity.java trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicNotOwnedList.java trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicNotOwnedSet.java trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicNotOwnedSetWithEmbId.java trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicNotOwnedSetWithMulId.java trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/DataChangesNotOwnedSet.java trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/DoubleNotOwnedSet.java Modified: trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java trunk/src/main/org/jboss/envers/entities/mapper/PersistentCollectionChangeData.java trunk/src/main/org/jboss/envers/entities/mapper/relation/AbstractCollectionMapper.java trunk/src/main/org/jboss/envers/entities/mapper/relation/BasicCollectionMapper.java trunk/src/main/org/jboss/envers/entities/mapper/relation/MapCollectionMapper.java trunk/src/main/org/jboss/envers/event/VersionsEventListener.java trunk/src/test/org/jboss/envers/test/integration/collection/StringMap.java trunk/src/test/org/jboss/envers/test/integration/manytomany/unidirectional/BasicUniList.java trunk/src/test/org/jboss/envers/test/integration/manytomany/unidirectional/BasicUniSet.java Log: ENVERS-25: support for many-to-many relations with map-valued properties; mapping from a simple value. Tests name refactorings to clarify meaning. Modified: trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java =================================================================== --- trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java 2008-09-21 20:42:00 UTC (rev 155) +++ trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java 2008-09-21 22:00:50 UTC (rev 156) @@ -332,8 +332,8 @@ // TODO: add support for enums, components, custom types mainGenerator.getBasicMetadataGenerator().addSimpleValue(middleEntityXml, prefix, value, null, ModificationStore.FULL, true); - return new MiddleComponentData(new MiddleSimpleComponentMapper(mainGenerator.getVerEntCfg(), prefix), - queryGeneratorBuilder.getCurrentIndex()); + // Simple values are always stored in the first entity read by the query generator. + return new MiddleComponentData(new MiddleSimpleComponentMapper(mainGenerator.getVerEntCfg(), prefix), 0); } else { // TODO: throw an exception throw new RuntimeException(); Modified: trunk/src/main/org/jboss/envers/entities/mapper/PersistentCollectionChangeData.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/PersistentCollectionChangeData.java 2008-09-21 20:42:00 UTC (rev 155) +++ trunk/src/main/org/jboss/envers/entities/mapper/PersistentCollectionChangeData.java 2008-09-21 22:00:50 UTC (rev 156) @@ -33,12 +33,12 @@ public class PersistentCollectionChangeData { private final String entityName; private final Map data; - private final Object obj; + private final Object changedElement; - public PersistentCollectionChangeData(String entityName, Map data, Object obj) { + public PersistentCollectionChangeData(String entityName, Map data, Object changedElement) { this.entityName = entityName; this.data = data; - this.obj = obj; + this.changedElement = changedElement; } /** @@ -54,10 +54,10 @@ } /** - * - * @return The affected object, which was changed (added, removed, modified) in the collection. + * For use by bi-directional associations. + * @return The affected element, which was changed (added, removed, modified) in the collection. */ - public Object getObj() { - return obj; + public Object getChangedElement() { + return changedElement; } } Modified: trunk/src/main/org/jboss/envers/entities/mapper/relation/AbstractCollectionMapper.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/AbstractCollectionMapper.java 2008-09-21 20:42:00 UTC (rev 155) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/AbstractCollectionMapper.java 2008-09-21 22:00:50 UTC (rev 156) @@ -39,6 +39,7 @@ protected abstract Collection getNewCollectionContent(PersistentCollection newCollection); protected abstract Collection getOldCollectionContent(Serializable oldCollection); + protected abstract Object getElement(Object changedObject); /** * Maps the changed collection element to the given map. @@ -55,7 +56,7 @@ entityData.put(commonCollectionMapperData.getVerEntCfg().getOriginalIdPropName(), originalId); collectionChanges.add(new PersistentCollectionChangeData( - commonCollectionMapperData.getVersionsMiddleEntityName(), entityData, changedObj)); + commonCollectionMapperData.getVersionsMiddleEntityName(), entityData, getElement(changedObj))); // Mapping the collection owner's id. commonCollectionMapperData.getReferencingIdData().getPrefixedMapper().mapToMapFromId(originalId, id); Modified: trunk/src/main/org/jboss/envers/entities/mapper/relation/BasicCollectionMapper.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/BasicCollectionMapper.java 2008-09-21 20:42:00 UTC (rev 155) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/BasicCollectionMapper.java 2008-09-21 22:00:50 UTC (rev 156) @@ -47,4 +47,8 @@ protected void mapToMapFromObject(Map data, Object changed) { elementComponentData.getComponentMapper().mapToMapFromObject(data, changed); } + + protected Object getElement(Object changedObject) { + return changedObject; + } } Modified: trunk/src/main/org/jboss/envers/entities/mapper/relation/MapCollectionMapper.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/MapCollectionMapper.java 2008-09-21 20:42:00 UTC (rev 155) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/MapCollectionMapper.java 2008-09-21 22:00:50 UTC (rev 156) @@ -52,4 +52,8 @@ elementComponentData.getComponentMapper().mapToMapFromObject(data, ((Map.Entry) changed).getValue()); indexComponentData.getComponentMapper().mapToMapFromObject(data, ((Map.Entry) changed).getKey()); } + + protected Object getElement(Object changedObject) { + return ((Map.Entry) changedObject).getValue(); + } } \ No newline at end of file Modified: trunk/src/main/org/jboss/envers/event/VersionsEventListener.java =================================================================== --- trunk/src/main/org/jboss/envers/event/VersionsEventListener.java 2008-09-21 20:42:00 UTC (rev 155) +++ trunk/src/main/org/jboss/envers/event/VersionsEventListener.java 2008-09-21 22:00:50 UTC (rev 156) @@ -140,7 +140,7 @@ IdMapper relatedIdMapper = verCfg.getEntCfg().get(relatedEntityName).getIdMapper(); for (PersistentCollectionChangeData changeData : workUnit.getCollectionChanges()) { - Object relatedObj = changeData.getObj(); + Object relatedObj = changeData.getChangedElement(); Serializable relatedId = (Serializable) relatedIdMapper.mapToIdFromEntity(relatedObj); verSync.addWorkUnit(new CollectionChangeWorkUnit(relatedEntityName, verCfg, relatedId, relatedObj)); Copied: trunk/src/test/org/jboss/envers/test/entities/manytomany/MapOwnedEntity.java (from rev 148, trunk/src/test/org/jboss/envers/test/entities/manytomany/SetOwnedEntity.java) =================================================================== --- trunk/src/test/org/jboss/envers/test/entities/manytomany/MapOwnedEntity.java (rev 0) +++ trunk/src/test/org/jboss/envers/test/entities/manytomany/MapOwnedEntity.java 2008-09-21 22:00:50 UTC (rev 156) @@ -0,0 +1,84 @@ +package org.jboss.envers.test.entities.manytomany; + +import org.jboss.envers.Versioned; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.ManyToMany; +import java.util.Set; + +/** + * Many-to-many not-owning entity + * @author Adam Warski (adam at warski dot org) + */ + at Entity +public class MapOwnedEntity { + @Id + private Integer id; + + @Versioned + private String data; + + @Versioned + @ManyToMany(mappedBy="references") + private Set referencing; + + public MapOwnedEntity() { + } + + public MapOwnedEntity(Integer id, String data) { + this.id = id; + this.data = data; + } + + public MapOwnedEntity(String data) { + this.data = data; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getData() { + return data; + } + + public void setData(String data) { + this.data = data; + } + + public Set getReferencing() { + return referencing; + } + + public void setReferencing(Set referencing) { + this.referencing = referencing; + } + + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof MapOwnedEntity)) return false; + + MapOwnedEntity that = (MapOwnedEntity) o; + + if (data != null ? !data.equals(that.data) : that.data != null) return false; + if (id != null ? !id.equals(that.id) : that.id != null) return false; + + return true; + } + + public int hashCode() { + int result; + result = (id != null ? id.hashCode() : 0); + result = 31 * result + (data != null ? data.hashCode() : 0); + return result; + } + + public String toString() { + return "MapOwnedEntity(id = " + id + ", data = " + data + ")"; + } +} \ No newline at end of file Copied: trunk/src/test/org/jboss/envers/test/entities/manytomany/MapOwningEntity.java (from rev 148, trunk/src/test/org/jboss/envers/test/entities/manytomany/ListOwningEntity.java) =================================================================== --- trunk/src/test/org/jboss/envers/test/entities/manytomany/MapOwningEntity.java (rev 0) +++ trunk/src/test/org/jboss/envers/test/entities/manytomany/MapOwningEntity.java 2008-09-21 22:00:50 UTC (rev 156) @@ -0,0 +1,82 @@ +package org.jboss.envers.test.entities.manytomany; + +import org.jboss.envers.Versioned; + +import javax.persistence.*; +import java.util.Map; +import java.util.HashMap; + +/** + * Entity owning the many-to-many relation + * @author Adam Warski (adam at warski dot org) + */ + at Entity +public class MapOwningEntity { + @Id + private Integer id; + + @Versioned + private String data; + + @Versioned + @ManyToMany + private Map references = new HashMap(); + + public MapOwningEntity() { } + + public MapOwningEntity(Integer id, String data) { + this.id = id; + this.data = data; + } + + public MapOwningEntity(String data) { + this.data = data; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getData() { + return data; + } + + public void setData(String data) { + this.data = data; + } + + public Map getReferences() { + return references; + } + + public void setReferences(Map references) { + this.references = references; + } + + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof MapOwningEntity)) return false; + + MapOwningEntity that = (MapOwningEntity) o; + + if (data != null ? !data.equals(that.data) : that.data != null) return false; + if (id != null ? !id.equals(that.id) : that.id != null) return false; + + return true; + } + + public int hashCode() { + int result; + result = (id != null ? id.hashCode() : 0); + result = 31 * result + (data != null ? data.hashCode() : 0); + return result; + } + + public String toString() { + return "MapOwningEntity(id = " + id + ", data = " + data + ")"; + } +} \ No newline at end of file Property changes on: trunk/src/test/org/jboss/envers/test/entities/manytomany/MapOwningEntity.java ___________________________________________________________________ Name: svn:mergeinfo + Deleted: trunk/src/test/org/jboss/envers/test/entities/manytomany/unidirectional/ListOwnedUniEntity.java =================================================================== --- trunk/src/test/org/jboss/envers/test/entities/manytomany/unidirectional/ListOwnedUniEntity.java 2008-09-21 20:42:00 UTC (rev 155) +++ trunk/src/test/org/jboss/envers/test/entities/manytomany/unidirectional/ListOwnedUniEntity.java 2008-09-21 22:00:50 UTC (rev 156) @@ -1,70 +0,0 @@ -package org.jboss.envers.test.entities.manytomany.unidirectional; - -import org.jboss.envers.Versioned; - -import javax.persistence.Entity; -import javax.persistence.Id; - -/** - * Many-to-many not-owning entity - * @author Adam Warski (adam at warski dot org) - */ - at Entity -public class ListOwnedUniEntity { - @Id - private Integer id; - - @Versioned - private String data; - - public ListOwnedUniEntity() { - } - - public ListOwnedUniEntity(Integer id, String data) { - this.id = id; - this.data = data; - } - - public ListOwnedUniEntity(String data) { - this.data = data; - } - - public Integer getId() { - return id; - } - - public void setId(Integer id) { - this.id = id; - } - - public String getData() { - return data; - } - - public void setData(String data) { - this.data = data; - } - - public boolean equals(Object o) { - if (this == o) return true; - if (!(o instanceof ListOwnedUniEntity)) return false; - - ListOwnedUniEntity that = (ListOwnedUniEntity) o; - - if (data != null ? !data.equals(that.data) : that.data != null) return false; - if (id != null ? !id.equals(that.id) : that.id != null) return false; - - return true; - } - - public int hashCode() { - int result; - result = (id != null ? id.hashCode() : 0); - result = 31 * result + (data != null ? data.hashCode() : 0); - return result; - } - - public String toString() { - return "SetOwnedEntity(id = " + id + ", data = " + data + ")"; - } -} \ No newline at end of file Copied: trunk/src/test/org/jboss/envers/test/entities/manytomany/unidirectional/ListUniEntity.java (from rev 148, trunk/src/test/org/jboss/envers/test/entities/manytomany/unidirectional/ListOwningUniEntity.java) =================================================================== --- trunk/src/test/org/jboss/envers/test/entities/manytomany/unidirectional/ListUniEntity.java (rev 0) +++ trunk/src/test/org/jboss/envers/test/entities/manytomany/unidirectional/ListUniEntity.java 2008-09-21 22:00:50 UTC (rev 156) @@ -0,0 +1,82 @@ +package org.jboss.envers.test.entities.manytomany.unidirectional; + +import org.jboss.envers.Versioned; +import org.jboss.envers.test.entities.StrTestEntity; + +import javax.persistence.*; +import java.util.List; + +/** + * Entity owning the many-to-many relation + * @author Adam Warski (adam at warski dot org) + */ + at Entity +public class ListUniEntity { + @Id + private Integer id; + + @Versioned + private String data; + + @Versioned + @ManyToMany + private List references; + + public ListUniEntity() { } + + public ListUniEntity(Integer id, String data) { + this.id = id; + this.data = data; + } + + public ListUniEntity(String data) { + this.data = data; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getData() { + return data; + } + + public void setData(String data) { + this.data = data; + } + + public List getReferences() { + return references; + } + + public void setReferences(List references) { + this.references = references; + } + + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof ListUniEntity)) return false; + + ListUniEntity that = (ListUniEntity) o; + + if (data != null ? !data.equals(that.data) : that.data != null) return false; + if (id != null ? !id.equals(that.id) : that.id != null) return false; + + return true; + } + + public int hashCode() { + int result; + result = (id != null ? id.hashCode() : 0); + result = 31 * result + (data != null ? data.hashCode() : 0); + return result; + } + + public String toString() { + return "ListUniEntity(id = " + id + ", data = " + data + ")"; + } +} \ No newline at end of file Copied: trunk/src/test/org/jboss/envers/test/entities/manytomany/unidirectional/MapUniEntity.java (from rev 148, trunk/src/test/org/jboss/envers/test/entities/onetomany/detached/SetRefCollEntity.java) =================================================================== --- trunk/src/test/org/jboss/envers/test/entities/manytomany/unidirectional/MapUniEntity.java (rev 0) +++ trunk/src/test/org/jboss/envers/test/entities/manytomany/unidirectional/MapUniEntity.java 2008-09-21 22:00:50 UTC (rev 156) @@ -0,0 +1,85 @@ +package org.jboss.envers.test.entities.manytomany.unidirectional; + +import org.jboss.envers.Versioned; +import org.jboss.envers.test.entities.StrTestEntity; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.ManyToMany; +import java.util.Map; + +/** + * Entity with a map from a string to an entity + * @author Adam Warski (adam at warski dot org) + */ + at Entity +public class MapUniEntity { + @Id + private Integer id; + + @Versioned + private String data; + + @Versioned + @ManyToMany + private Map map; + + public MapUniEntity() { + } + + public MapUniEntity(Integer id, String data) { + this.id = id; + this.data = data; + } + + public MapUniEntity(String data) { + this.data = data; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getData() { + return data; + } + + public void setData(String data) { + this.data = data; + } + + public Map getMap() { + return map; + } + + public void setMap(Map map) { + this.map = map; + } + + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof MapUniEntity)) return false; + + MapUniEntity that = (MapUniEntity) o; + + if (data != null ? !data.equals(that.data) : that.data != null) return false; + if (id != null ? !id.equals(that.id) : that.id != null) return false; + + return true; + } + + public int hashCode() { + int result; + result = (id != null ? id.hashCode() : 0); + result = 31 * result + (data != null ? data.hashCode() : 0); + return result; + } + + public String toString() { + return "MapUniEntity(id = " + id + ", data = " + data + ")"; + } +} \ No newline at end of file Property changes on: trunk/src/test/org/jboss/envers/test/entities/manytomany/unidirectional/MapUniEntity.java ___________________________________________________________________ Name: svn:mergeinfo + Deleted: trunk/src/test/org/jboss/envers/test/entities/manytomany/unidirectional/SetOwnedUniEntity.java =================================================================== --- trunk/src/test/org/jboss/envers/test/entities/manytomany/unidirectional/SetOwnedUniEntity.java 2008-09-21 20:42:00 UTC (rev 155) +++ trunk/src/test/org/jboss/envers/test/entities/manytomany/unidirectional/SetOwnedUniEntity.java 2008-09-21 22:00:50 UTC (rev 156) @@ -1,70 +0,0 @@ -package org.jboss.envers.test.entities.manytomany.unidirectional; - -import org.jboss.envers.Versioned; - -import javax.persistence.Entity; -import javax.persistence.Id; - -/** - * Many-to-many not-owning entity - * @author Adam Warski (adam at warski dot org) - */ - at Entity -public class SetOwnedUniEntity { - @Id - private Integer id; - - @Versioned - private String data; - - public SetOwnedUniEntity() { - } - - public SetOwnedUniEntity(Integer id, String data) { - this.id = id; - this.data = data; - } - - public SetOwnedUniEntity(String data) { - this.data = data; - } - - public Integer getId() { - return id; - } - - public void setId(Integer id) { - this.id = id; - } - - public String getData() { - return data; - } - - public void setData(String data) { - this.data = data; - } - - public boolean equals(Object o) { - if (this == o) return true; - if (!(o instanceof SetOwnedUniEntity)) return false; - - SetOwnedUniEntity that = (SetOwnedUniEntity) o; - - if (data != null ? !data.equals(that.data) : that.data != null) return false; - if (id != null ? !id.equals(that.id) : that.id != null) return false; - - return true; - } - - public int hashCode() { - int result; - result = (id != null ? id.hashCode() : 0); - result = 31 * result + (data != null ? data.hashCode() : 0); - return result; - } - - public String toString() { - return "SetOwnedUniEntity(id = " + id + ", data = " + data + ")"; - } -} \ No newline at end of file Copied: trunk/src/test/org/jboss/envers/test/entities/manytomany/unidirectional/SetUniEntity.java (from rev 148, trunk/src/test/org/jboss/envers/test/entities/manytomany/unidirectional/SetOwningUniEntity.java) =================================================================== --- trunk/src/test/org/jboss/envers/test/entities/manytomany/unidirectional/SetUniEntity.java (rev 0) +++ trunk/src/test/org/jboss/envers/test/entities/manytomany/unidirectional/SetUniEntity.java 2008-09-21 22:00:50 UTC (rev 156) @@ -0,0 +1,82 @@ +package org.jboss.envers.test.entities.manytomany.unidirectional; + +import org.jboss.envers.Versioned; +import org.jboss.envers.test.entities.StrTestEntity; + +import javax.persistence.*; +import java.util.Set; + +/** + * Entity owning the many-to-many relation + * @author Adam Warski (adam at warski dot org) + */ + at Entity +public class SetUniEntity { + @Id + private Integer id; + + @Versioned + private String data; + + @Versioned + @ManyToMany + private Set references; + + public SetUniEntity() { } + + public SetUniEntity(Integer id, String data) { + this.id = id; + this.data = data; + } + + public SetUniEntity(String data) { + this.data = data; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getData() { + return data; + } + + public void setData(String data) { + this.data = data; + } + + public Set getReferences() { + return references; + } + + public void setReferences(Set references) { + this.references = references; + } + + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof SetUniEntity)) return false; + + SetUniEntity that = (SetUniEntity) o; + + if (data != null ? !data.equals(that.data) : that.data != null) return false; + if (id != null ? !id.equals(that.id) : that.id != null) return false; + + return true; + } + + public int hashCode() { + int result; + result = (id != null ? id.hashCode() : 0); + result = 31 * result + (data != null ? data.hashCode() : 0); + return result; + } + + public String toString() { + return "SetUniEntity(id = " + id + ", data = " + data + ")"; + } +} \ No newline at end of file Modified: trunk/src/test/org/jboss/envers/test/integration/collection/StringMap.java =================================================================== --- trunk/src/test/org/jboss/envers/test/integration/collection/StringMap.java 2008-09-21 20:42:00 UTC (rev 155) +++ trunk/src/test/org/jboss/envers/test/integration/collection/StringMap.java 2008-09-21 22:00:50 UTC (rev 156) @@ -61,7 +61,7 @@ em.getTransaction().commit(); - // Revision 4 (sme1: removing a non-existing mapping, sme2: replacing with the same value) + // No revision (sme1: removing a non-existing mapping, sme2: replacing with the same value) em.getTransaction().begin(); sme1 = em.find(StringMapEntity.class, sme1.getId()); Copied: trunk/src/test/org/jboss/envers/test/integration/manytomany/BasicMap.java (from rev 148, trunk/src/test/org/jboss/envers/test/integration/manytomany/BasicSet.java) =================================================================== --- trunk/src/test/org/jboss/envers/test/integration/manytomany/BasicMap.java (rev 0) +++ trunk/src/test/org/jboss/envers/test/integration/manytomany/BasicMap.java 2008-09-21 22:00:50 UTC (rev 156) @@ -0,0 +1,163 @@ +package org.jboss.envers.test.integration.manytomany; + +import org.jboss.envers.test.integration.AbstractEntityTest; +import org.jboss.envers.test.entities.manytomany.MapOwningEntity; +import org.jboss.envers.test.entities.manytomany.MapOwnedEntity; +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; +import java.util.Collections; +import java.util.HashMap; + +/** + * @author Adam Warski (adam at warski dot org) + */ +public class BasicMap extends AbstractEntityTest { + private Integer ed1_id; + private Integer ed2_id; + + private Integer ing1_id; + private Integer ing2_id; + + public void configure(Ejb3Configuration cfg) { + cfg.addAnnotatedClass(MapOwningEntity.class); + cfg.addAnnotatedClass(MapOwnedEntity.class); + } + + @BeforeClass(dependsOnMethods = "init") + public void initData() { + EntityManager em = getEntityManager(); + + MapOwnedEntity ed1 = new MapOwnedEntity(1, "data_ed_1"); + MapOwnedEntity ed2 = new MapOwnedEntity(2, "data_ed_2"); + + MapOwningEntity ing1 = new MapOwningEntity(3, "data_ing_1"); + MapOwningEntity ing2 = new MapOwningEntity(4, "data_ing_2"); + + // Revision 1 (ing1: initialy empty, ing2: one mapping) + em.getTransaction().begin(); + + ing2.getReferences().put("2", ed2); + + em.persist(ed1); + em.persist(ed2); + em.persist(ing1); + em.persist(ing2); + + em.getTransaction().commit(); + + // Revision 2 (ing1: adding two mappings, ing2: replacing an existing mapping) + + em.getTransaction().begin(); + + ing1 = em.find(MapOwningEntity.class, ing1.getId()); + ing2 = em.find(MapOwningEntity.class, ing2.getId()); + ed1 = em.find(MapOwnedEntity.class, ed1.getId()); + ed2 = em.find(MapOwnedEntity.class, ed2.getId()); + + ing1.getReferences().put("1", ed1); + ing1.getReferences().put("2", ed1); + + ing2.getReferences().put("2", ed1); + + em.getTransaction().commit(); + + // No revision (ing1: adding an existing mapping, ing2: removing a non existing mapping) + em.getTransaction().begin(); + + ing1 = em.find(MapOwningEntity.class, ing1.getId()); + ing2 = em.find(MapOwningEntity.class, ing2.getId()); + + ing1.getReferences().put("1", ed1); + + ing2.getReferences().remove("3"); + + em.getTransaction().commit(); + + // Revision 3 (ing1: clearing, ing2: replacing with a new map) + em.getTransaction().begin(); + + ing1 = em.find(MapOwningEntity.class, ing1.getId()); + ed1 = em.find(MapOwnedEntity.class, ed1.getId()); + + ing1.getReferences().clear(); + ing2.setReferences(new HashMap()); + ing2.getReferences().put("1", ed2); + + em.getTransaction().commit(); + // + + ed1_id = ed1.getId(); + ed2_id = ed2.getId(); + + ing1_id = ing1.getId(); + ing2_id = ing2.getId(); + } + + @Test + public void testRevisionsCounts() { + assert Arrays.asList(1, 2, 3).equals(getVersionsReader().getRevisions(MapOwnedEntity.class, ed1_id)); + assert Arrays.asList(1, 2, 3).equals(getVersionsReader().getRevisions(MapOwnedEntity.class, ed2_id)); + + assert Arrays.asList(1, 2, 3).equals(getVersionsReader().getRevisions(MapOwningEntity.class, ing1_id)); + assert Arrays.asList(1, 2, 3).equals(getVersionsReader().getRevisions(MapOwningEntity.class, ing2_id)); + } + + @Test + public void testHistoryOfEdId1() { + MapOwningEntity ing1 = getEntityManager().find(MapOwningEntity.class, ing1_id); + MapOwningEntity ing2 = getEntityManager().find(MapOwningEntity.class, ing2_id); + + MapOwnedEntity rev1 = getVersionsReader().find(MapOwnedEntity.class, ed1_id, 1); + MapOwnedEntity rev2 = getVersionsReader().find(MapOwnedEntity.class, ed1_id, 2); + MapOwnedEntity rev3 = getVersionsReader().find(MapOwnedEntity.class, ed1_id, 3); + + assert rev1.getReferencing().equals(Collections.EMPTY_SET); + assert rev2.getReferencing().equals(TestTools.makeSet(ing1, ing2)); + assert rev3.getReferencing().equals(Collections.EMPTY_SET); + } + + @Test + public void testHistoryOfEdId2() { + MapOwningEntity ing2 = getEntityManager().find(MapOwningEntity.class, ing2_id); + + MapOwnedEntity rev1 = getVersionsReader().find(MapOwnedEntity.class, ed2_id, 1); + MapOwnedEntity rev2 = getVersionsReader().find(MapOwnedEntity.class, ed2_id, 2); + MapOwnedEntity rev3 = getVersionsReader().find(MapOwnedEntity.class, ed2_id, 3); + + assert rev1.getReferencing().equals(TestTools.makeSet(ing2)); + assert rev2.getReferencing().equals(Collections.EMPTY_SET); + assert rev3.getReferencing().equals(TestTools.makeSet(ing2)); + } + + @Test + public void testHistoryOfEdIng1() { + MapOwnedEntity ed1 = getEntityManager().find(MapOwnedEntity.class, ed1_id); + + MapOwningEntity rev1 = getVersionsReader().find(MapOwningEntity.class, ing1_id, 1); + MapOwningEntity rev2 = getVersionsReader().find(MapOwningEntity.class, ing1_id, 2); + MapOwningEntity rev3 = getVersionsReader().find(MapOwningEntity.class, ing1_id, 3); + + assert rev1.getReferences().equals(Collections.EMPTY_MAP); + assert rev2.getReferences().equals(TestTools.makeMap("1", ed1, "2", ed1)); + assert rev3.getReferences().equals(Collections.EMPTY_MAP); + } + + @Test + public void testHistoryOfEdIng2() { + MapOwnedEntity ed1 = getEntityManager().find(MapOwnedEntity.class, ed1_id); + MapOwnedEntity ed2 = getEntityManager().find(MapOwnedEntity.class, ed2_id); + + MapOwningEntity rev1 = getVersionsReader().find(MapOwningEntity.class, ing2_id, 1); + MapOwningEntity rev2 = getVersionsReader().find(MapOwningEntity.class, ing2_id, 2); + MapOwningEntity rev3 = getVersionsReader().find(MapOwningEntity.class, ing2_id, 3); + + assert rev1.getReferences().equals(TestTools.makeMap("2", ed2)); + assert rev2.getReferences().equals(TestTools.makeMap("2", ed1)); + assert rev3.getReferences().equals(TestTools.makeMap("1", ed2)); + } +} \ No newline at end of file Property changes on: trunk/src/test/org/jboss/envers/test/integration/manytomany/BasicMap.java ___________________________________________________________________ Name: svn:mergeinfo + Modified: trunk/src/test/org/jboss/envers/test/integration/manytomany/unidirectional/BasicUniList.java =================================================================== --- trunk/src/test/org/jboss/envers/test/integration/manytomany/unidirectional/BasicUniList.java 2008-09-21 20:42:00 UTC (rev 155) +++ trunk/src/test/org/jboss/envers/test/integration/manytomany/unidirectional/BasicUniList.java 2008-09-21 22:00:50 UTC (rev 156) @@ -1,8 +1,8 @@ package org.jboss.envers.test.integration.manytomany.unidirectional; import org.jboss.envers.test.integration.AbstractEntityTest; -import org.jboss.envers.test.entities.manytomany.unidirectional.ListOwnedUniEntity; -import org.jboss.envers.test.entities.manytomany.unidirectional.ListOwningUniEntity; +import org.jboss.envers.test.entities.manytomany.unidirectional.ListUniEntity; +import org.jboss.envers.test.entities.StrTestEntity; import org.jboss.envers.test.tools.TestTools; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; @@ -24,19 +24,19 @@ private Integer ing2_id; public void configure(Ejb3Configuration cfg) { - cfg.addAnnotatedClass(ListOwningUniEntity.class); - cfg.addAnnotatedClass(ListOwnedUniEntity.class); + cfg.addAnnotatedClass(ListUniEntity.class); + cfg.addAnnotatedClass(StrTestEntity.class); } @BeforeClass(dependsOnMethods = "init") public void initData() { EntityManager em = getEntityManager(); - ListOwnedUniEntity ed1 = new ListOwnedUniEntity(1, "data_ed_1"); - ListOwnedUniEntity ed2 = new ListOwnedUniEntity(2, "data_ed_2"); + StrTestEntity ed1 = new StrTestEntity("data_ed_1"); + StrTestEntity ed2 = new StrTestEntity("data_ed_2"); - ListOwningUniEntity ing1 = new ListOwningUniEntity(3, "data_ing_1"); - ListOwningUniEntity ing2 = new ListOwningUniEntity(4, "data_ing_2"); + ListUniEntity ing1 = new ListUniEntity(3, "data_ing_1"); + ListUniEntity ing2 = new ListUniEntity(4, "data_ing_2"); // Revision 1 em.getTransaction().begin(); @@ -52,15 +52,15 @@ em.getTransaction().begin(); - ing1 = em.find(ListOwningUniEntity.class, ing1.getId()); - ing2 = em.find(ListOwningUniEntity.class, ing2.getId()); - ed1 = em.find(ListOwnedUniEntity.class, ed1.getId()); - ed2 = em.find(ListOwnedUniEntity.class, ed2.getId()); + ing1 = em.find(ListUniEntity.class, ing1.getId()); + ing2 = em.find(ListUniEntity.class, ing2.getId()); + ed1 = em.find(StrTestEntity.class, ed1.getId()); + ed2 = em.find(StrTestEntity.class, ed2.getId()); - ing1.setReferences(new ArrayList()); + ing1.setReferences(new ArrayList()); ing1.getReferences().add(ed1); - ing2.setReferences(new ArrayList()); + ing2.setReferences(new ArrayList()); ing2.getReferences().add(ed1); ing2.getReferences().add(ed2); @@ -69,9 +69,9 @@ // Revision 3 em.getTransaction().begin(); - ing1 = em.find(ListOwningUniEntity.class, ing1.getId()); - ed2 = em.find(ListOwnedUniEntity.class, ed2.getId()); - ed1 = em.find(ListOwnedUniEntity.class, ed1.getId()); + ing1 = em.find(ListUniEntity.class, ing1.getId()); + ed2 = em.find(StrTestEntity.class, ed2.getId()); + ed1 = em.find(StrTestEntity.class, ed1.getId()); ing1.getReferences().add(ed2); @@ -80,9 +80,9 @@ // Revision 4 em.getTransaction().begin(); - ing1 = em.find(ListOwningUniEntity.class, ing1.getId()); - ed2 = em.find(ListOwnedUniEntity.class, ed2.getId()); - ed1 = em.find(ListOwnedUniEntity.class, ed1.getId()); + ing1 = em.find(ListUniEntity.class, ing1.getId()); + ed2 = em.find(StrTestEntity.class, ed2.getId()); + ed1 = em.find(StrTestEntity.class, ed1.getId()); ing1.getReferences().remove(ed1); @@ -91,7 +91,7 @@ // Revision 5 em.getTransaction().begin(); - ing1 = em.find(ListOwningUniEntity.class, ing1.getId()); + ing1 = em.find(ListUniEntity.class, ing1.getId()); ing1.setReferences(null); @@ -108,23 +108,23 @@ @Test public void testRevisionsCounts() { - assert Arrays.asList(1).equals(getVersionsReader().getRevisions(ListOwnedUniEntity.class, ed1_id)); - assert Arrays.asList(1).equals(getVersionsReader().getRevisions(ListOwnedUniEntity.class, ed2_id)); + assert Arrays.asList(1).equals(getVersionsReader().getRevisions(StrTestEntity.class, ed1_id)); + assert Arrays.asList(1).equals(getVersionsReader().getRevisions(StrTestEntity.class, ed2_id)); - assert Arrays.asList(1, 2, 3, 4, 5).equals(getVersionsReader().getRevisions(ListOwningUniEntity.class, ing1_id)); - assert Arrays.asList(1, 2).equals(getVersionsReader().getRevisions(ListOwningUniEntity.class, ing2_id)); + assert Arrays.asList(1, 2, 3, 4, 5).equals(getVersionsReader().getRevisions(ListUniEntity.class, ing1_id)); + assert Arrays.asList(1, 2).equals(getVersionsReader().getRevisions(ListUniEntity.class, ing2_id)); } @Test public void testHistoryOfEdIng1() { - ListOwnedUniEntity ed1 = getEntityManager().find(ListOwnedUniEntity.class, ed1_id); - ListOwnedUniEntity ed2 = getEntityManager().find(ListOwnedUniEntity.class, ed2_id); + StrTestEntity ed1 = getEntityManager().find(StrTestEntity.class, ed1_id); + StrTestEntity ed2 = getEntityManager().find(StrTestEntity.class, ed2_id); - ListOwningUniEntity rev1 = getVersionsReader().find(ListOwningUniEntity.class, ing1_id, 1); - ListOwningUniEntity rev2 = getVersionsReader().find(ListOwningUniEntity.class, ing1_id, 2); - ListOwningUniEntity rev3 = getVersionsReader().find(ListOwningUniEntity.class, ing1_id, 3); - ListOwningUniEntity rev4 = getVersionsReader().find(ListOwningUniEntity.class, ing1_id, 4); - ListOwningUniEntity rev5 = getVersionsReader().find(ListOwningUniEntity.class, ing1_id, 5); + ListUniEntity rev1 = getVersionsReader().find(ListUniEntity.class, ing1_id, 1); + ListUniEntity rev2 = getVersionsReader().find(ListUniEntity.class, ing1_id, 2); + ListUniEntity rev3 = getVersionsReader().find(ListUniEntity.class, ing1_id, 3); + ListUniEntity rev4 = getVersionsReader().find(ListUniEntity.class, ing1_id, 4); + ListUniEntity rev5 = getVersionsReader().find(ListUniEntity.class, ing1_id, 5); assert rev1.getReferences().equals(Collections.EMPTY_LIST); assert TestTools.checkList(rev2.getReferences(), ed1); @@ -135,14 +135,14 @@ @Test public void testHistoryOfEdIng2() { - ListOwnedUniEntity ed1 = getEntityManager().find(ListOwnedUniEntity.class, ed1_id); - ListOwnedUniEntity ed2 = getEntityManager().find(ListOwnedUniEntity.class, ed2_id); + StrTestEntity ed1 = getEntityManager().find(StrTestEntity.class, ed1_id); + StrTestEntity ed2 = getEntityManager().find(StrTestEntity.class, ed2_id); - ListOwningUniEntity rev1 = getVersionsReader().find(ListOwningUniEntity.class, ing2_id, 1); - ListOwningUniEntity rev2 = getVersionsReader().find(ListOwningUniEntity.class, ing2_id, 2); - ListOwningUniEntity rev3 = getVersionsReader().find(ListOwningUniEntity.class, ing2_id, 3); - ListOwningUniEntity rev4 = getVersionsReader().find(ListOwningUniEntity.class, ing2_id, 4); - ListOwningUniEntity rev5 = getVersionsReader().find(ListOwningUniEntity.class, ing2_id, 5); + ListUniEntity rev1 = getVersionsReader().find(ListUniEntity.class, ing2_id, 1); + ListUniEntity rev2 = getVersionsReader().find(ListUniEntity.class, ing2_id, 2); + ListUniEntity rev3 = getVersionsReader().find(ListUniEntity.class, ing2_id, 3); + ListUniEntity rev4 = getVersionsReader().find(ListUniEntity.class, ing2_id, 4); + ListUniEntity rev5 = getVersionsReader().find(ListUniEntity.class, ing2_id, 5); assert rev1.getReferences().equals(Collections.EMPTY_LIST); assert TestTools.checkList(rev2.getReferences(), ed1, ed2); Copied: trunk/src/test/org/jboss/envers/test/integration/manytomany/unidirectional/BasicUniMap.java (from rev 148, trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicNotOwnedSet.java) =================================================================== --- trunk/src/test/org/jboss/envers/test/integration/manytomany/unidirectional/BasicUniMap.java (rev 0) +++ trunk/src/test/org/jboss/envers/test/integration/manytomany/unidirectional/BasicUniMap.java 2008-09-21 22:00:50 UTC (rev 156) @@ -0,0 +1,115 @@ +package org.jboss.envers.test.integration.manytomany.unidirectional; + +import org.jboss.envers.test.integration.AbstractEntityTest; +import org.jboss.envers.test.entities.manytomany.unidirectional.MapUniEntity; +import org.jboss.envers.test.entities.StrTestEntity; +import org.jboss.envers.test.tools.TestTools; +import org.hibernate.ejb.Ejb3Configuration; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +import javax.persistence.EntityManager; +import java.util.Arrays; +import java.util.HashMap; + +/** + * @author Adam Warski (adam at warski dot org) + */ +public class BasicUniMap extends AbstractEntityTest { + private Integer str1_id; + private Integer str2_id; + + private Integer coll1_id; + + public void configure(Ejb3Configuration cfg) { + cfg.addAnnotatedClass(StrTestEntity.class); + cfg.addAnnotatedClass(MapUniEntity.class); + } + + @BeforeClass(dependsOnMethods = "init") + public void initData() { + EntityManager em = getEntityManager(); + + StrTestEntity str1 = new StrTestEntity("str1"); + StrTestEntity str2 = new StrTestEntity("str2"); + + MapUniEntity coll1 = new MapUniEntity(3, "coll1"); + + // Revision 1 (coll1: initialy one mapping) + em.getTransaction().begin(); + + em.persist(str1); + em.persist(str2); + + coll1.setMap(new HashMap()); + coll1.getMap().put("1", str1); + em.persist(coll1); + + em.getTransaction().commit(); + + // Revision 2 (coll1: adding one mapping) + em.getTransaction().begin(); + + str2 = em.find(StrTestEntity.class, str2.getId()); + coll1 = em.find(MapUniEntity.class, coll1.getId()); + + coll1.getMap().put("2", str2); + + em.getTransaction().commit(); + + // Revision 3 (coll1: replacing one mapping) + em.getTransaction().begin(); + + str1 = em.find(StrTestEntity.class, str1.getId()); + coll1 = em.find(MapUniEntity.class, coll1.getId()); + + coll1.getMap().put("2", str1); + + em.getTransaction().commit(); + + // Revision 4 (coll1: removing one mapping) + em.getTransaction().begin(); + + coll1 = em.find(MapUniEntity.class, coll1.getId()); + + coll1.getMap().remove("1"); + + em.getTransaction().commit(); + + // + + str1_id = str1.getId(); + str2_id = str2.getId(); + + coll1_id = coll1.getId(); + } + + @Test + public void testRevisionsCounts() { + assert Arrays.asList(1, 2, 3, 4).equals(getVersionsReader().getRevisions(MapUniEntity.class, coll1_id)); + + assert Arrays.asList(1).equals(getVersionsReader().getRevisions(StrTestEntity.class, str1_id)); + assert Arrays.asList(1).equals(getVersionsReader().getRevisions(StrTestEntity.class, str2_id)); + } + + @Test + public void testHistoryOfColl1() { + StrTestEntity str1 = getEntityManager().find(StrTestEntity.class, str1_id); + StrTestEntity str2 = getEntityManager().find(StrTestEntity.class, str2_id); + + MapUniEntity rev1 = getVersionsReader().find(MapUniEntity.class, coll1_id, 1); + MapUniEntity rev2 = getVersionsReader().find(MapUniEntity.class, coll1_id, 2); + MapUniEntity rev3 = getVersionsReader().find(MapUniEntity.class, coll1_id, 3); + MapUniEntity rev4 = getVersionsReader().find(MapUniEntity.class, coll1_id, 4); + + assert rev1.getMap().equals(TestTools.makeMap("1", str1)); + assert rev2.getMap().equals(TestTools.makeMap("1", str1, "2", str2)); + assert rev3.getMap().equals(TestTools.makeMap("1", str1, "2", str1)); + assert rev4.getMap().equals(TestTools.makeMap("2", str1)); + + assert "coll1".equals(rev1.getData()); + assert "coll1".equals(rev2.getData()); + assert "coll1".equals(rev3.getData()); + assert "coll1".equals(rev4.getData()); + } +} \ No newline at end of file Property changes on: trunk/src/test/org/jboss/envers/test/integration/manytomany/unidirectional/BasicUniMap.java ___________________________________________________________________ Name: svn:mergeinfo + Modified: trunk/src/test/org/jboss/envers/test/integration/manytomany/unidirectional/BasicUniSet.java =================================================================== --- trunk/src/test/org/jboss/envers/test/integration/manytomany/unidirectional/BasicUniSet.java 2008-09-21 20:42:00 UTC (rev 155) +++ trunk/src/test/org/jboss/envers/test/integration/manytomany/unidirectional/BasicUniSet.java 2008-09-21 22:00:50 UTC (rev 156) @@ -1,8 +1,8 @@ package org.jboss.envers.test.integration.manytomany.unidirectional; import org.jboss.envers.test.integration.AbstractEntityTest; -import org.jboss.envers.test.entities.manytomany.unidirectional.SetOwnedUniEntity; -import org.jboss.envers.test.entities.manytomany.unidirectional.SetOwningUniEntity; +import org.jboss.envers.test.entities.manytomany.unidirectional.SetUniEntity; +import org.jboss.envers.test.entities.StrTestEntity; import org.jboss.envers.test.tools.TestTools; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; @@ -24,19 +24,19 @@ private Integer ing2_id; public void configure(Ejb3Configuration cfg) { - cfg.addAnnotatedClass(SetOwningUniEntity.class); - cfg.addAnnotatedClass(SetOwnedUniEntity.class); + cfg.addAnnotatedClass(SetUniEntity.class); + cfg.addAnnotatedClass(StrTestEntity.class); } @BeforeClass(dependsOnMethods = "init") public void initData() { EntityManager em = getEntityManager(); - SetOwnedUniEntity ed1 = new SetOwnedUniEntity(1, "data_ed_1"); - SetOwnedUniEntity ed2 = new SetOwnedUniEntity(2, "data_ed_2"); + StrTestEntity ed1 = new StrTestEntity("data_ed_1"); + StrTestEntity ed2 = new StrTestEntity("data_ed_2"); - SetOwningUniEntity ing1 = new SetOwningUniEntity(3, "data_ing_1"); - SetOwningUniEntity ing2 = new SetOwningUniEntity(4, "data_ing_2"); + SetUniEntity ing1 = new SetUniEntity(3, "data_ing_1"); + SetUniEntity ing2 = new SetUniEntity(4, "data_ing_2"); // Revision 1 em.getTransaction().begin(); @@ -52,15 +52,15 @@ em.getTransaction().begin(); - ing1 = em.find(SetOwningUniEntity.class, ing1.getId()); - ing2 = em.find(SetOwningUniEntity.class, ing2.getId()); - ed1 = em.find(SetOwnedUniEntity.class, ed1.getId()); - ed2 = em.find(SetOwnedUniEntity.class, ed2.getId()); + ing1 = em.find(SetUniEntity.class, ing1.getId()); + ing2 = em.find(SetUniEntity.class, ing2.getId()); + ed1 = em.find(StrTestEntity.class, ed1.getId()); + ed2 = em.find(StrTestEntity.class, ed2.getId()); - ing1.setReferences(new HashSet()); + ing1.setReferences(new HashSet()); ing1.getReferences().add(ed1); - ing2.setReferences(new HashSet()); + ing2.setReferences(new HashSet()); ing2.getReferences().add(ed1); ing2.getReferences().add(ed2); @@ -69,9 +69,9 @@ // Revision 3 em.getTransaction().begin(); - ing1 = em.find(SetOwningUniEntity.class, ing1.getId()); - ed2 = em.find(SetOwnedUniEntity.class, ed2.getId()); - ed1 = em.find(SetOwnedUniEntity.class, ed1.getId()); + ing1 = em.find(SetUniEntity.class, ing1.getId()); + ed2 = em.find(StrTestEntity.class, ed2.getId()); + ed1 = em.find(StrTestEntity.class, ed1.getId()); ing1.getReferences().add(ed2); @@ -80,9 +80,9 @@ // Revision 4 em.getTransaction().begin(); - ing1 = em.find(SetOwningUniEntity.class, ing1.getId()); - ed2 = em.find(SetOwnedUniEntity.class, ed2.getId()); - ed1 = em.find(SetOwnedUniEntity.class, ed1.getId()); + ing1 = em.find(SetUniEntity.class, ing1.getId()); + ed2 = em.find(StrTestEntity.class, ed2.getId()); + ed1 = em.find(StrTestEntity.class, ed1.getId()); ing1.getReferences().remove(ed1); @@ -91,7 +91,7 @@ // Revision 5 em.getTransaction().begin(); - ing1 = em.find(SetOwningUniEntity.class, ing1.getId()); + ing1 = em.find(SetUniEntity.class, ing1.getId()); ing1.setReferences(null); @@ -108,23 +108,23 @@ @Test public void testRevisionsCounts() { - assert Arrays.asList(1).equals(getVersionsReader().getRevisions(SetOwnedUniEntity.class, ed1_id)); - assert Arrays.asList(1).equals(getVersionsReader().getRevisions(SetOwnedUniEntity.class, ed2_id)); + assert Arrays.asList(1).equals(getVersionsReader().getRevisions(StrTestEntity.class, ed1_id)); + assert Arrays.asList(1).equals(getVersionsReader().getRevisions(StrTestEntity.class, ed2_id)); - assert Arrays.asList(1, 2, 3, 4, 5).equals(getVersionsReader().getRevisions(SetOwningUniEntity.class, ing1_id)); - assert Arrays.asList(1, 2).equals(getVersionsReader().getRevisions(SetOwningUniEntity.class, ing2_id)); + assert Arrays.asList(1, 2, 3, 4, 5).equals(getVersionsReader().getRevisions(SetUniEntity.class, ing1_id)); + assert Arrays.asList(1, 2).equals(getVersionsReader().getRevisions(SetUniEntity.class, ing2_id)); } @Test public void testHistoryOfEdIng1() { - SetOwnedUniEntity ed1 = getEntityManager().find(SetOwnedUniEntity.class, ed1_id); - SetOwnedUniEntity ed2 = getEntityManager().find(SetOwnedUniEntity.class, ed2_id); + StrTestEntity ed1 = getEntityManager().find(StrTestEntity.class, ed1_id); + StrTestEntity ed2 = getEntityManager().find(StrTestEntity.class, ed2_id); - SetOwningUniEntity rev1 = getVersionsReader().find(SetOwningUniEntity.class, ing1_id, 1); - SetOwningUniEntity rev2 = getVersionsReader().find(SetOwningUniEntity.class, ing1_id, 2); - SetOwningUniEntity rev3 = getVersionsReader().find(SetOwningUniEntity.class, ing1_id, 3); - SetOwningUniEntity rev4 = getVersionsReader().find(SetOwningUniEntity.class, ing1_id, 4); - SetOwningUniEntity rev5 = getVersionsReader().find(SetOwningUniEntity.class, ing1_id, 5); + SetUniEntity rev1 = getVersionsReader().find(SetUniEntity.class, ing1_id, 1); + SetUniEntity rev2 = getVersionsReader().find(SetUniEntity.class, ing1_id, 2); + SetUniEntity rev3 = getVersionsReader().find(SetUniEntity.class, ing1_id, 3); + SetUniEntity rev4 = getVersionsReader().find(SetUniEntity.class, ing1_id, 4); + SetUniEntity rev5 = getVersionsReader().find(SetUniEntity.class, ing1_id, 5); assert rev1.getReferences().equals(Collections.EMPTY_SET); assert rev2.getReferences().equals(TestTools.makeSet(ed1)); @@ -135,14 +135,14 @@ @Test public void testHistoryOfEdIng2() { - SetOwnedUniEntity ed1 = getEntityManager().find(SetOwnedUniEntity.class, ed1_id); - SetOwnedUniEntity ed2 = getEntityManager().find(SetOwnedUniEntity.class, ed2_id); + StrTestEntity ed1 = getEntityManager().find(StrTestEntity.class, ed1_id); + StrTestEntity ed2 = getEntityManager().find(StrTestEntity.class, ed2_id); - SetOwningUniEntity rev1 = getVersionsReader().find(SetOwningUniEntity.class, ing2_id, 1); - SetOwningUniEntity rev2 = getVersionsReader().find(SetOwningUniEntity.class, ing2_id, 2); - SetOwningUniEntity rev3 = getVersionsReader().find(SetOwningUniEntity.class, ing2_id, 3); - SetOwningUniEntity rev4 = getVersionsReader().find(SetOwningUniEntity.class, ing2_id, 4); - SetOwningUniEntity rev5 = getVersionsReader().find(SetOwningUniEntity.class, ing2_id, 5); + SetUniEntity rev1 = getVersionsReader().find(SetUniEntity.class, ing2_id, 1); + SetUniEntity rev2 = getVersionsReader().find(SetUniEntity.class, ing2_id, 2); + SetUniEntity rev3 = getVersionsReader().find(SetUniEntity.class, ing2_id, 3); + SetUniEntity rev4 = getVersionsReader().find(SetUniEntity.class, ing2_id, 4); + SetUniEntity rev5 = getVersionsReader().find(SetUniEntity.class, ing2_id, 5); assert rev1.getReferences().equals(Collections.EMPTY_SET); assert rev2.getReferences().equals(TestTools.makeSet(ed1, ed2)); Copied: trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicDetachedList.java (from rev 148, trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicNotOwnedList.java) =================================================================== --- trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicDetachedList.java (rev 0) +++ trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicDetachedList.java 2008-09-21 22:00:50 UTC (rev 156) @@ -0,0 +1,115 @@ +package org.jboss.envers.test.integration.onetomany.detached; + +import org.jboss.envers.test.integration.AbstractEntityTest; +import org.jboss.envers.test.entities.onetomany.detached.ListRefCollEntity; +import org.jboss.envers.test.entities.StrTestEntity; +import org.jboss.envers.test.tools.TestTools; +import org.hibernate.ejb.Ejb3Configuration; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +import javax.persistence.EntityManager; +import java.util.Arrays; +import java.util.ArrayList; + +/** + * @author Adam Warski (adam at warski dot org) + */ +public class BasicDetachedList extends AbstractEntityTest { + private Integer str1_id; + private Integer str2_id; + + private Integer coll1_id; + + public void configure(Ejb3Configuration cfg) { + cfg.addAnnotatedClass(StrTestEntity.class); + cfg.addAnnotatedClass(ListRefCollEntity.class); + } + + @BeforeClass(dependsOnMethods = "init") + public void initData() { + EntityManager em = getEntityManager(); + + StrTestEntity str1 = new StrTestEntity("str1"); + StrTestEntity str2 = new StrTestEntity("str2"); + + ListRefCollEntity coll1 = new ListRefCollEntity(3, "coll1"); + + // Revision 1 + em.getTransaction().begin(); + + em.persist(str1); + em.persist(str2); + + coll1.setCollection(new ArrayList()); + coll1.getCollection().add(str1); + em.persist(coll1); + + em.getTransaction().commit(); + + // Revision 2 + em.getTransaction().begin(); + + str2 = em.find(StrTestEntity.class, str2.getId()); + coll1 = em.find(ListRefCollEntity.class, coll1.getId()); + + coll1.getCollection().add(str2); + + em.getTransaction().commit(); + + // Revision 3 + em.getTransaction().begin(); + + str1 = em.find(StrTestEntity.class, str1.getId()); + coll1 = em.find(ListRefCollEntity.class, coll1.getId()); + + coll1.getCollection().remove(str1); + + em.getTransaction().commit(); + + // Revision 4 + em.getTransaction().begin(); + + coll1 = em.find(ListRefCollEntity.class, coll1.getId()); + + coll1.getCollection().clear(); + + em.getTransaction().commit(); + + // + + str1_id = str1.getId(); + str2_id = str2.getId(); + + coll1_id = coll1.getId(); + } + + @Test + public void testRevisionsCounts() { + assert Arrays.asList(1, 2, 3, 4).equals(getVersionsReader().getRevisions(ListRefCollEntity.class, coll1_id)); + + assert Arrays.asList(1).equals(getVersionsReader().getRevisions(StrTestEntity.class, str1_id)); + assert Arrays.asList(1).equals(getVersionsReader().getRevisions(StrTestEntity.class, str2_id)); + } + + @Test + public void testHistoryOfColl1() { + StrTestEntity str1 = getEntityManager().find(StrTestEntity.class, str1_id); + StrTestEntity str2 = getEntityManager().find(StrTestEntity.class, str2_id); + + ListRefCollEntity rev1 = getVersionsReader().find(ListRefCollEntity.class, coll1_id, 1); + ListRefCollEntity rev2 = getVersionsReader().find(ListRefCollEntity.class, coll1_id, 2); + ListRefCollEntity rev3 = getVersionsReader().find(ListRefCollEntity.class, coll1_id, 3); + ListRefCollEntity rev4 = getVersionsReader().find(ListRefCollEntity.class, coll1_id, 4); + + assert TestTools.checkList(rev1.getCollection(), str1); + assert TestTools.checkList(rev2.getCollection(), str1, str2); + assert TestTools.checkList(rev3.getCollection(), str2); + assert TestTools.checkList(rev4.getCollection()); + + assert "coll1".equals(rev1.getData()); + assert "coll1".equals(rev2.getData()); + assert "coll1".equals(rev3.getData()); + assert "coll1".equals(rev4.getData()); + } +} \ No newline at end of file Property changes on: trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicDetachedList.java ___________________________________________________________________ Name: svn:mergeinfo + Copied: trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicDetachedSet.java (from rev 148, trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicNotOwnedSet.java) =================================================================== --- trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicDetachedSet.java (rev 0) +++ trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicDetachedSet.java 2008-09-21 22:00:50 UTC (rev 156) @@ -0,0 +1,115 @@ +package org.jboss.envers.test.integration.onetomany.detached; + +import org.jboss.envers.test.integration.AbstractEntityTest; +import org.jboss.envers.test.entities.onetomany.detached.SetRefCollEntity; +import org.jboss.envers.test.entities.StrTestEntity; +import org.jboss.envers.test.tools.TestTools; +import org.hibernate.ejb.Ejb3Configuration; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +import javax.persistence.EntityManager; +import java.util.Arrays; +import java.util.HashSet; + +/** + * @author Adam Warski (adam at warski dot org) + */ +public class BasicDetachedSet extends AbstractEntityTest { + private Integer str1_id; + private Integer str2_id; + + private Integer coll1_id; + + public void configure(Ejb3Configuration cfg) { + cfg.addAnnotatedClass(StrTestEntity.class); + cfg.addAnnotatedClass(SetRefCollEntity.class); + } + + @BeforeClass(dependsOnMethods = "init") + public void initData() { + EntityManager em = getEntityManager(); + + StrTestEntity str1 = new StrTestEntity("str1"); + StrTestEntity str2 = new StrTestEntity("str2"); + + SetRefCollEntity coll1 = new SetRefCollEntity(3, "coll1"); + + // Revision 1 + em.getTransaction().begin(); + + em.persist(str1); + em.persist(str2); + + coll1.setCollection(new HashSet()); + coll1.getCollection().add(str1); + em.persist(coll1); + + em.getTransaction().commit(); + + // Revision 2 + em.getTransaction().begin(); + + str2 = em.find(StrTestEntity.class, str2.getId()); + coll1 = em.find(SetRefCollEntity.class, coll1.getId()); + + coll1.getCollection().add(str2); + + em.getTransaction().commit(); + + // Revision 3 + em.getTransaction().begin(); + + str1 = em.find(StrTestEntity.class, str1.getId()); + coll1 = em.find(SetRefCollEntity.class, coll1.getId()); + + coll1.getCollection().remove(str1); + + em.getTransaction().commit(); + + // Revision 4 + em.getTransaction().begin(); + + coll1 = em.find(SetRefCollEntity.class, coll1.getId()); + + coll1.getCollection().clear(); + + em.getTransaction().commit(); + + // + + str1_id = str1.getId(); + str2_id = str2.getId(); + + coll1_id = coll1.getId(); + } + + @Test + public void testRevisionsCounts() { + assert Arrays.asList(1, 2, 3, 4).equals(getVersionsReader().getRevisions(SetRefCollEntity.class, coll1_id)); + + assert Arrays.asList(1).equals(getVersionsReader().getRevisions(StrTestEntity.class, str1_id)); + assert Arrays.asList(1).equals(getVersionsReader().getRevisions(StrTestEntity.class, str2_id)); + } + + @Test + public void testHistoryOfColl1() { + StrTestEntity str1 = getEntityManager().find(StrTestEntity.class, str1_id); + StrTestEntity str2 = getEntityManager().find(StrTestEntity.class, str2_id); + + SetRefCollEntity rev1 = getVersionsReader().find(SetRefCollEntity.class, coll1_id, 1); + SetRefCollEntity rev2 = getVersionsReader().find(SetRefCollEntity.class, coll1_id, 2); + SetRefCollEntity rev3 = getVersionsReader().find(SetRefCollEntity.class, coll1_id, 3); + SetRefCollEntity rev4 = getVersionsReader().find(SetRefCollEntity.class, coll1_id, 4); + + assert rev1.getCollection().equals(TestTools.makeSet(str1)); + assert rev2.getCollection().equals(TestTools.makeSet(str1, str2)); + assert rev3.getCollection().equals(TestTools.makeSet(str2)); + assert rev4.getCollection().equals(TestTools.makeSet()); + + assert "coll1".equals(rev1.getData()); + assert "coll1".equals(rev2.getData()); + assert "coll1".equals(rev3.getData()); + assert "coll1".equals(rev4.getData()); + } +} Copied: trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicDetachedSetWithEmbId.java (from rev 148, trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicNotOwnedSetWithEmbId.java) =================================================================== --- trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicDetachedSetWithEmbId.java (rev 0) +++ trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicDetachedSetWithEmbId.java 2008-09-21 22:00:50 UTC (rev 156) @@ -0,0 +1,114 @@ +package org.jboss.envers.test.integration.onetomany.detached; + +import org.jboss.envers.test.integration.AbstractEntityTest; +import org.jboss.envers.test.entities.onetomany.detached.ids.SetRefCollEntityEmbId; +import org.jboss.envers.test.entities.ids.EmbIdTestEntity; +import org.jboss.envers.test.entities.ids.EmbId; +import org.jboss.envers.test.tools.TestTools; +import org.hibernate.ejb.Ejb3Configuration; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +import javax.persistence.EntityManager; +import java.util.Arrays; +import java.util.HashSet; + +/** + * @author Adam Warski (adam at warski dot org) + */ +public class BasicDetachedSetWithEmbId extends AbstractEntityTest { + private EmbId str1_id; + private EmbId str2_id; + + private EmbId coll1_id; + + public void configure(Ejb3Configuration cfg) { + cfg.addAnnotatedClass(EmbIdTestEntity.class); + cfg.addAnnotatedClass(SetRefCollEntityEmbId.class); + } + + @BeforeClass(dependsOnMethods = "init") + public void initData() { + EntityManager em = getEntityManager(); + + str1_id = new EmbId(1, 2); + str2_id = new EmbId(3, 4); + + coll1_id = new EmbId(5, 6); + + EmbIdTestEntity str1 = new EmbIdTestEntity(str1_id, "str1"); + EmbIdTestEntity str2 = new EmbIdTestEntity(str2_id, "str2"); + + SetRefCollEntityEmbId coll1 = new SetRefCollEntityEmbId(coll1_id, "coll1"); + + // Revision 1 + em.getTransaction().begin(); + + em.persist(str1); + em.persist(str2); + + coll1.setCollection(new HashSet()); + coll1.getCollection().add(str1); + em.persist(coll1); + + em.getTransaction().commit(); + + // Revision 2 + em.getTransaction().begin(); + + str2 = em.find(EmbIdTestEntity.class, str2.getId()); + coll1 = em.find(SetRefCollEntityEmbId.class, coll1.getId()); + + coll1.getCollection().add(str2); + + em.getTransaction().commit(); + + // Revision 3 + em.getTransaction().begin(); + + str1 = em.find(EmbIdTestEntity.class, str1.getId()); + coll1 = em.find(SetRefCollEntityEmbId.class, coll1.getId()); + + coll1.getCollection().remove(str1); + + em.getTransaction().commit(); + + // Revision 4 + em.getTransaction().begin(); + + coll1 = em.find(SetRefCollEntityEmbId.class, coll1.getId()); + + coll1.getCollection().clear(); + + em.getTransaction().commit(); + } + + @Test + public void testRevisionsCounts() { + assert Arrays.asList(1, 2, 3, 4).equals(getVersionsReader().getRevisions(SetRefCollEntityEmbId.class, coll1_id)); + + assert Arrays.asList(1).equals(getVersionsReader().getRevisions(EmbIdTestEntity.class, str1_id)); + assert Arrays.asList(1).equals(getVersionsReader().getRevisions(EmbIdTestEntity.class, str2_id)); + } + + @Test + public void testHistoryOfColl1() { + EmbIdTestEntity str1 = getEntityManager().find(EmbIdTestEntity.class, str1_id); + EmbIdTestEntity str2 = getEntityManager().find(EmbIdTestEntity.class, str2_id); + + SetRefCollEntityEmbId rev1 = getVersionsReader().find(SetRefCollEntityEmbId.class, coll1_id, 1); + SetRefCollEntityEmbId rev2 = getVersionsReader().find(SetRefCollEntityEmbId.class, coll1_id, 2); + SetRefCollEntityEmbId rev3 = getVersionsReader().find(SetRefCollEntityEmbId.class, coll1_id, 3); + SetRefCollEntityEmbId rev4 = getVersionsReader().find(SetRefCollEntityEmbId.class, coll1_id, 4); + + assert rev1.getCollection().equals(TestTools.makeSet(str1)); + assert rev2.getCollection().equals(TestTools.makeSet(str1, str2)); + assert rev3.getCollection().equals(TestTools.makeSet(str2)); + assert rev4.getCollection().equals(TestTools.makeSet()); + + assert "coll1".equals(rev1.getData()); + assert "coll1".equals(rev2.getData()); + assert "coll1".equals(rev3.getData()); + assert "coll1".equals(rev4.getData()); + } +} \ No newline at end of file Property changes on: trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicDetachedSetWithEmbId.java ___________________________________________________________________ Name: svn:mergeinfo + Copied: trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicDetachedSetWithMulId.java (from rev 148, trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicNotOwnedSetWithMulId.java) =================================================================== --- trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicDetachedSetWithMulId.java (rev 0) +++ trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicDetachedSetWithMulId.java 2008-09-21 22:00:50 UTC (rev 156) @@ -0,0 +1,114 @@ +package org.jboss.envers.test.integration.onetomany.detached; + +import org.jboss.envers.test.integration.AbstractEntityTest; +import org.jboss.envers.test.entities.onetomany.detached.ids.SetRefCollEntityMulId; +import org.jboss.envers.test.entities.ids.MulIdTestEntity; +import org.jboss.envers.test.entities.ids.MulId; +import org.jboss.envers.test.tools.TestTools; +import org.hibernate.ejb.Ejb3Configuration; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +import javax.persistence.EntityManager; +import java.util.Arrays; +import java.util.HashSet; + +/** + * @author Adam Warski (adam at warski dot org) + */ +public class BasicDetachedSetWithMulId extends AbstractEntityTest { + private MulId str1_id; + private MulId str2_id; + + private MulId coll1_id; + + public void configure(Ejb3Configuration cfg) { + cfg.addAnnotatedClass(MulIdTestEntity.class); + cfg.addAnnotatedClass(SetRefCollEntityMulId.class); + } + + @BeforeClass(dependsOnMethods = "init") + public void initData() { + EntityManager em = getEntityManager(); + + str1_id = new MulId(1, 2); + str2_id = new MulId(3, 4); + + coll1_id = new MulId(5, 6); + + MulIdTestEntity str1 = new MulIdTestEntity(str1_id.getId1(), str1_id.getId2(), "str1"); + MulIdTestEntity str2 = new MulIdTestEntity(str2_id.getId1(), str2_id.getId2(), "str2"); + + SetRefCollEntityMulId coll1 = new SetRefCollEntityMulId(coll1_id.getId1(), coll1_id.getId2(), "coll1"); + + // Revision 1 + em.getTransaction().begin(); + + em.persist(str1); + em.persist(str2); + + coll1.setCollection(new HashSet()); + coll1.getCollection().add(str1); + em.persist(coll1); + + em.getTransaction().commit(); + + // Revision 2 + em.getTransaction().begin(); + + str2 = em.find(MulIdTestEntity.class, str2_id); + coll1 = em.find(SetRefCollEntityMulId.class, coll1_id); + + coll1.getCollection().add(str2); + + em.getTransaction().commit(); + + // Revision 3 + em.getTransaction().begin(); + + str1 = em.find(MulIdTestEntity.class, str1_id); + coll1 = em.find(SetRefCollEntityMulId.class, coll1_id); + + coll1.getCollection().remove(str1); + + em.getTransaction().commit(); + + // Revision 4 + em.getTransaction().begin(); + + coll1 = em.find(SetRefCollEntityMulId.class, coll1_id); + + coll1.getCollection().clear(); + + em.getTransaction().commit(); + } + + @Test + public void testRevisionsCounts() { + assert Arrays.asList(1, 2, 3, 4).equals(getVersionsReader().getRevisions(SetRefCollEntityMulId.class, coll1_id)); + + assert Arrays.asList(1).equals(getVersionsReader().getRevisions(MulIdTestEntity.class, str1_id)); + assert Arrays.asList(1).equals(getVersionsReader().getRevisions(MulIdTestEntity.class, str2_id)); + } + + @Test + public void testHistoryOfColl1() { + MulIdTestEntity str1 = getEntityManager().find(MulIdTestEntity.class, str1_id); + MulIdTestEntity str2 = getEntityManager().find(MulIdTestEntity.class, str2_id); + + SetRefCollEntityMulId rev1 = getVersionsReader().find(SetRefCollEntityMulId.class, coll1_id, 1); + SetRefCollEntityMulId rev2 = getVersionsReader().find(SetRefCollEntityMulId.class, coll1_id, 2); + SetRefCollEntityMulId rev3 = getVersionsReader().find(SetRefCollEntityMulId.class, coll1_id, 3); + SetRefCollEntityMulId rev4 = getVersionsReader().find(SetRefCollEntityMulId.class, coll1_id, 4); + + assert rev1.getCollection().equals(TestTools.makeSet(str1)); + assert rev2.getCollection().equals(TestTools.makeSet(str1, str2)); + assert rev3.getCollection().equals(TestTools.makeSet(str2)); + assert rev4.getCollection().equals(TestTools.makeSet()); + + assert "coll1".equals(rev1.getData()); + assert "coll1".equals(rev2.getData()); + assert "coll1".equals(rev3.getData()); + assert "coll1".equals(rev4.getData()); + } +} \ No newline at end of file Deleted: trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicNotOwnedList.java =================================================================== --- trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicNotOwnedList.java 2008-09-21 20:42:00 UTC (rev 155) +++ trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicNotOwnedList.java 2008-09-21 22:00:50 UTC (rev 156) @@ -1,115 +0,0 @@ -package org.jboss.envers.test.integration.onetomany.detached; - -import org.jboss.envers.test.integration.AbstractEntityTest; -import org.jboss.envers.test.entities.onetomany.detached.ListRefCollEntity; -import org.jboss.envers.test.entities.StrTestEntity; -import org.jboss.envers.test.tools.TestTools; -import org.hibernate.ejb.Ejb3Configuration; -import org.testng.annotations.BeforeClass; -import org.testng.annotations.Test; - -import javax.persistence.EntityManager; -import java.util.Arrays; -import java.util.ArrayList; - -/** - * @author Adam Warski (adam at warski dot org) - */ -public class BasicNotOwnedList extends AbstractEntityTest { - private Integer str1_id; - private Integer str2_id; - - private Integer coll1_id; - - public void configure(Ejb3Configuration cfg) { - cfg.addAnnotatedClass(StrTestEntity.class); - cfg.addAnnotatedClass(ListRefCollEntity.class); - } - - @BeforeClass(dependsOnMethods = "init") - public void initData() { - EntityManager em = getEntityManager(); - - StrTestEntity str1 = new StrTestEntity("str1"); - StrTestEntity str2 = new StrTestEntity("str2"); - - ListRefCollEntity coll1 = new ListRefCollEntity(3, "coll1"); - - // Revision 1 - em.getTransaction().begin(); - - em.persist(str1); - em.persist(str2); - - coll1.setCollection(new ArrayList()); - coll1.getCollection().add(str1); - em.persist(coll1); - - em.getTransaction().commit(); - - // Revision 2 - em.getTransaction().begin(); - - str2 = em.find(StrTestEntity.class, str2.getId()); - coll1 = em.find(ListRefCollEntity.class, coll1.getId()); - - coll1.getCollection().add(str2); - - em.getTransaction().commit(); - - // Revision 3 - em.getTransaction().begin(); - - str1 = em.find(StrTestEntity.class, str1.getId()); - coll1 = em.find(ListRefCollEntity.class, coll1.getId()); - - coll1.getCollection().remove(str1); - - em.getTransaction().commit(); - - // Revision 4 - em.getTransaction().begin(); - - coll1 = em.find(ListRefCollEntity.class, coll1.getId()); - - coll1.getCollection().clear(); - - em.getTransaction().commit(); - - // - - str1_id = str1.getId(); - str2_id = str2.getId(); - - coll1_id = coll1.getId(); - } - - @Test - public void testRevisionsCounts() { - assert Arrays.asList(1, 2, 3, 4).equals(getVersionsReader().getRevisions(ListRefCollEntity.class, coll1_id)); - - assert Arrays.asList(1).equals(getVersionsReader().getRevisions(StrTestEntity.class, str1_id)); - assert Arrays.asList(1).equals(getVersionsReader().getRevisions(StrTestEntity.class, str2_id)); - } - - @Test - public void testHistoryOfColl1() { - StrTestEntity str1 = getEntityManager().find(StrTestEntity.class, str1_id); - StrTestEntity str2 = getEntityManager().find(StrTestEntity.class, str2_id); - - ListRefCollEntity rev1 = getVersionsReader().find(ListRefCollEntity.class, coll1_id, 1); - ListRefCollEntity rev2 = getVersionsReader().find(ListRefCollEntity.class, coll1_id, 2); - ListRefCollEntity rev3 = getVersionsReader().find(ListRefCollEntity.class, coll1_id, 3); - ListRefCollEntity rev4 = getVersionsReader().find(ListRefCollEntity.class, coll1_id, 4); - - assert TestTools.checkList(rev1.getCollection(), str1); - assert TestTools.checkList(rev2.getCollection(), str1, str2); - assert TestTools.checkList(rev3.getCollection(), str2); - assert TestTools.checkList(rev4.getCollection()); - - assert "coll1".equals(rev1.getData()); - assert "coll1".equals(rev2.getData()); - assert "coll1".equals(rev3.getData()); - assert "coll1".equals(rev4.getData()); - } -} \ No newline at end of file Deleted: trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicNotOwnedSet.java =================================================================== --- trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicNotOwnedSet.java 2008-09-21 20:42:00 UTC (rev 155) +++ trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicNotOwnedSet.java 2008-09-21 22:00:50 UTC (rev 156) @@ -1,115 +0,0 @@ -package org.jboss.envers.test.integration.onetomany.detached; - -import org.jboss.envers.test.integration.AbstractEntityTest; -import org.jboss.envers.test.entities.onetomany.detached.SetRefCollEntity; -import org.jboss.envers.test.entities.StrTestEntity; -import org.jboss.envers.test.tools.TestTools; -import org.hibernate.ejb.Ejb3Configuration; -import org.testng.annotations.BeforeClass; -import org.testng.annotations.Test; - -import javax.persistence.EntityManager; -import java.util.Arrays; -import java.util.HashSet; - -/** - * @author Adam Warski (adam at warski dot org) - */ -public class BasicNotOwnedSet extends AbstractEntityTest { - private Integer str1_id; - private Integer str2_id; - - private Integer coll1_id; - - public void configure(Ejb3Configuration cfg) { - cfg.addAnnotatedClass(StrTestEntity.class); - cfg.addAnnotatedClass(SetRefCollEntity.class); - } - - @BeforeClass(dependsOnMethods = "init") - public void initData() { - EntityManager em = getEntityManager(); - - StrTestEntity str1 = new StrTestEntity("str1"); - StrTestEntity str2 = new StrTestEntity("str2"); - - SetRefCollEntity coll1 = new SetRefCollEntity(3, "coll1"); - - // Revision 1 - em.getTransaction().begin(); - - em.persist(str1); - em.persist(str2); - - coll1.setCollection(new HashSet()); - coll1.getCollection().add(str1); - em.persist(coll1); - - em.getTransaction().commit(); - - // Revision 2 - em.getTransaction().begin(); - - str2 = em.find(StrTestEntity.class, str2.getId()); - coll1 = em.find(SetRefCollEntity.class, coll1.getId()); - - coll1.getCollection().add(str2); - - em.getTransaction().commit(); - - // Revision 3 - em.getTransaction().begin(); - - str1 = em.find(StrTestEntity.class, str1.getId()); - coll1 = em.find(SetRefCollEntity.class, coll1.getId()); - - coll1.getCollection().remove(str1); - - em.getTransaction().commit(); - - // Revision 4 - em.getTransaction().begin(); - - coll1 = em.find(SetRefCollEntity.class, coll1.getId()); - - coll1.getCollection().clear(); - - em.getTransaction().commit(); - - // - - str1_id = str1.getId(); - str2_id = str2.getId(); - - coll1_id = coll1.getId(); - } - - @Test - public void testRevisionsCounts() { - assert Arrays.asList(1, 2, 3, 4).equals(getVersionsReader().getRevisions(SetRefCollEntity.class, coll1_id)); - - assert Arrays.asList(1).equals(getVersionsReader().getRevisions(StrTestEntity.class, str1_id)); - assert Arrays.asList(1).equals(getVersionsReader().getRevisions(StrTestEntity.class, str2_id)); - } - - @Test - public void testHistoryOfColl1() { - StrTestEntity str1 = getEntityManager().find(StrTestEntity.class, str1_id); - StrTestEntity str2 = getEntityManager().find(StrTestEntity.class, str2_id); - - SetRefCollEntity rev1 = getVersionsReader().find(SetRefCollEntity.class, coll1_id, 1); - SetRefCollEntity rev2 = getVersionsReader().find(SetRefCollEntity.class, coll1_id, 2); - SetRefCollEntity rev3 = getVersionsReader().find(SetRefCollEntity.class, coll1_id, 3); - SetRefCollEntity rev4 = getVersionsReader().find(SetRefCollEntity.class, coll1_id, 4); - - assert rev1.getCollection().equals(TestTools.makeSet(str1)); - assert rev2.getCollection().equals(TestTools.makeSet(str1, str2)); - assert rev3.getCollection().equals(TestTools.makeSet(str2)); - assert rev4.getCollection().equals(TestTools.makeSet()); - - assert "coll1".equals(rev1.getData()); - assert "coll1".equals(rev2.getData()); - assert "coll1".equals(rev3.getData()); - assert "coll1".equals(rev4.getData()); - } -} Deleted: trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicNotOwnedSetWithEmbId.java =================================================================== --- trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicNotOwnedSetWithEmbId.java 2008-09-21 20:42:00 UTC (rev 155) +++ trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicNotOwnedSetWithEmbId.java 2008-09-21 22:00:50 UTC (rev 156) @@ -1,114 +0,0 @@ -package org.jboss.envers.test.integration.onetomany.detached; - -import org.jboss.envers.test.integration.AbstractEntityTest; -import org.jboss.envers.test.entities.onetomany.detached.ids.SetRefCollEntityEmbId; -import org.jboss.envers.test.entities.ids.EmbIdTestEntity; -import org.jboss.envers.test.entities.ids.EmbId; -import org.jboss.envers.test.tools.TestTools; -import org.hibernate.ejb.Ejb3Configuration; -import org.testng.annotations.BeforeClass; -import org.testng.annotations.Test; - -import javax.persistence.EntityManager; -import java.util.Arrays; -import java.util.HashSet; - -/** - * @author Adam Warski (adam at warski dot org) - */ -public class BasicNotOwnedSetWithEmbId extends AbstractEntityTest { - private EmbId str1_id; - private EmbId str2_id; - - private EmbId coll1_id; - - public void configure(Ejb3Configuration cfg) { - cfg.addAnnotatedClass(EmbIdTestEntity.class); - cfg.addAnnotatedClass(SetRefCollEntityEmbId.class); - } - - @BeforeClass(dependsOnMethods = "init") - public void initData() { - EntityManager em = getEntityManager(); - - str1_id = new EmbId(1, 2); - str2_id = new EmbId(3, 4); - - coll1_id = new EmbId(5, 6); - - EmbIdTestEntity str1 = new EmbIdTestEntity(str1_id, "str1"); - EmbIdTestEntity str2 = new EmbIdTestEntity(str2_id, "str2"); - - SetRefCollEntityEmbId coll1 = new SetRefCollEntityEmbId(coll1_id, "coll1"); - - // Revision 1 - em.getTransaction().begin(); - - em.persist(str1); - em.persist(str2); - - coll1.setCollection(new HashSet()); - coll1.getCollection().add(str1); - em.persist(coll1); - - em.getTransaction().commit(); - - // Revision 2 - em.getTransaction().begin(); - - str2 = em.find(EmbIdTestEntity.class, str2.getId()); - coll1 = em.find(SetRefCollEntityEmbId.class, coll1.getId()); - - coll1.getCollection().add(str2); - - em.getTransaction().commit(); - - // Revision 3 - em.getTransaction().begin(); - - str1 = em.find(EmbIdTestEntity.class, str1.getId()); - coll1 = em.find(SetRefCollEntityEmbId.class, coll1.getId()); - - coll1.getCollection().remove(str1); - - em.getTransaction().commit(); - - // Revision 4 - em.getTransaction().begin(); - - coll1 = em.find(SetRefCollEntityEmbId.class, coll1.getId()); - - coll1.getCollection().clear(); - - em.getTransaction().commit(); - } - - @Test - public void testRevisionsCounts() { - assert Arrays.asList(1, 2, 3, 4).equals(getVersionsReader().getRevisions(SetRefCollEntityEmbId.class, coll1_id)); - - assert Arrays.asList(1).equals(getVersionsReader().getRevisions(EmbIdTestEntity.class, str1_id)); - assert Arrays.asList(1).equals(getVersionsReader().getRevisions(EmbIdTestEntity.class, str2_id)); - } - - @Test - public void testHistoryOfColl1() { - EmbIdTestEntity str1 = getEntityManager().find(EmbIdTestEntity.class, str1_id); - EmbIdTestEntity str2 = getEntityManager().find(EmbIdTestEntity.class, str2_id); - - SetRefCollEntityEmbId rev1 = getVersionsReader().find(SetRefCollEntityEmbId.class, coll1_id, 1); - SetRefCollEntityEmbId rev2 = getVersionsReader().find(SetRefCollEntityEmbId.class, coll1_id, 2); - SetRefCollEntityEmbId rev3 = getVersionsReader().find(SetRefCollEntityEmbId.class, coll1_id, 3); - SetRefCollEntityEmbId rev4 = getVersionsReader().find(SetRefCollEntityEmbId.class, coll1_id, 4); - - assert rev1.getCollection().equals(TestTools.makeSet(str1)); - assert rev2.getCollection().equals(TestTools.makeSet(str1, str2)); - assert rev3.getCollection().equals(TestTools.makeSet(str2)); - assert rev4.getCollection().equals(TestTools.makeSet()); - - assert "coll1".equals(rev1.getData()); - assert "coll1".equals(rev2.getData()); - assert "coll1".equals(rev3.getData()); - assert "coll1".equals(rev4.getData()); - } -} \ No newline at end of file Deleted: trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicNotOwnedSetWithMulId.java =================================================================== --- trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicNotOwnedSetWithMulId.java 2008-09-21 20:42:00 UTC (rev 155) +++ trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicNotOwnedSetWithMulId.java 2008-09-21 22:00:50 UTC (rev 156) @@ -1,114 +0,0 @@ -package org.jboss.envers.test.integration.onetomany.detached; - -import org.jboss.envers.test.integration.AbstractEntityTest; -import org.jboss.envers.test.entities.onetomany.detached.ids.SetRefCollEntityMulId; -import org.jboss.envers.test.entities.ids.MulIdTestEntity; -import org.jboss.envers.test.entities.ids.MulId; -import org.jboss.envers.test.tools.TestTools; -import org.hibernate.ejb.Ejb3Configuration; -import org.testng.annotations.BeforeClass; -import org.testng.annotations.Test; - -import javax.persistence.EntityManager; -import java.util.Arrays; -import java.util.HashSet; - -/** - * @author Adam Warski (adam at warski dot org) - */ -public class BasicNotOwnedSetWithMulId extends AbstractEntityTest { - private MulId str1_id; - private MulId str2_id; - - private MulId coll1_id; - - public void configure(Ejb3Configuration cfg) { - cfg.addAnnotatedClass(MulIdTestEntity.class); - cfg.addAnnotatedClass(SetRefCollEntityMulId.class); - } - - @BeforeClass(dependsOnMethods = "init") - public void initData() { - EntityManager em = getEntityManager(); - - str1_id = new MulId(1, 2); - str2_id = new MulId(3, 4); - - coll1_id = new MulId(5, 6); - - MulIdTestEntity str1 = new MulIdTestEntity(str1_id.getId1(), str1_id.getId2(), "str1"); - MulIdTestEntity str2 = new MulIdTestEntity(str2_id.getId1(), str2_id.getId2(), "str2"); - - SetRefCollEntityMulId coll1 = new SetRefCollEntityMulId(coll1_id.getId1(), coll1_id.getId2(), "coll1"); - - // Revision 1 - em.getTransaction().begin(); - - em.persist(str1); - em.persist(str2); - - coll1.setCollection(new HashSet()); - coll1.getCollection().add(str1); - em.persist(coll1); - - em.getTransaction().commit(); - - // Revision 2 - em.getTransaction().begin(); - - str2 = em.find(MulIdTestEntity.class, str2_id); - coll1 = em.find(SetRefCollEntityMulId.class, coll1_id); - - coll1.getCollection().add(str2); - - em.getTransaction().commit(); - - // Revision 3 - em.getTransaction().begin(); - - str1 = em.find(MulIdTestEntity.class, str1_id); - coll1 = em.find(SetRefCollEntityMulId.class, coll1_id); - - coll1.getCollection().remove(str1); - - em.getTransaction().commit(); - - // Revision 4 - em.getTransaction().begin(); - - coll1 = em.find(SetRefCollEntityMulId.class, coll1_id); - - coll1.getCollection().clear(); - - em.getTransaction().commit(); - } - - @Test - public void testRevisionsCounts() { - assert Arrays.asList(1, 2, 3, 4).equals(getVersionsReader().getRevisions(SetRefCollEntityMulId.class, coll1_id)); - - assert Arrays.asList(1).equals(getVersionsReader().getRevisions(MulIdTestEntity.class, str1_id)); - assert Arrays.asList(1).equals(getVersionsReader().getRevisions(MulIdTestEntity.class, str2_id)); - } - - @Test - public void testHistoryOfColl1() { - MulIdTestEntity str1 = getEntityManager().find(MulIdTestEntity.class, str1_id); - MulIdTestEntity str2 = getEntityManager().find(MulIdTestEntity.class, str2_id); - - SetRefCollEntityMulId rev1 = getVersionsReader().find(SetRefCollEntityMulId.class, coll1_id, 1); - SetRefCollEntityMulId rev2 = getVersionsReader().find(SetRefCollEntityMulId.class, coll1_id, 2); - SetRefCollEntityMulId rev3 = getVersionsReader().find(SetRefCollEntityMulId.class, coll1_id, 3); - SetRefCollEntityMulId rev4 = getVersionsReader().find(SetRefCollEntityMulId.class, coll1_id, 4); - - assert rev1.getCollection().equals(TestTools.makeSet(str1)); - assert rev2.getCollection().equals(TestTools.makeSet(str1, str2)); - assert rev3.getCollection().equals(TestTools.makeSet(str2)); - assert rev4.getCollection().equals(TestTools.makeSet()); - - assert "coll1".equals(rev1.getData()); - assert "coll1".equals(rev2.getData()); - assert "coll1".equals(rev3.getData()); - assert "coll1".equals(rev4.getData()); - } -} \ No newline at end of file Copied: trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/DataChangesDetachedSet.java (from rev 148, trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/DataChangesNotOwnedSet.java) =================================================================== --- trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/DataChangesDetachedSet.java (rev 0) +++ trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/DataChangesDetachedSet.java 2008-09-21 22:00:50 UTC (rev 156) @@ -0,0 +1,84 @@ +package org.jboss.envers.test.integration.onetomany.detached; + +import org.jboss.envers.test.integration.AbstractEntityTest; +import org.jboss.envers.test.entities.onetomany.detached.SetRefCollEntity; +import org.jboss.envers.test.entities.StrTestEntity; +import org.jboss.envers.test.tools.TestTools; +import org.hibernate.ejb.Ejb3Configuration; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +import javax.persistence.EntityManager; +import java.util.Arrays; +import java.util.HashSet; + +/** + * @author Adam Warski (adam at warski dot org) + */ +public class DataChangesDetachedSet extends AbstractEntityTest { + private Integer str1_id; + + private Integer coll1_id; + + public void configure(Ejb3Configuration cfg) { + cfg.addAnnotatedClass(StrTestEntity.class); + cfg.addAnnotatedClass(SetRefCollEntity.class); + } + + @BeforeClass(dependsOnMethods = "init") + public void initData() { + EntityManager em = getEntityManager(); + + StrTestEntity str1 = new StrTestEntity("str1"); + + SetRefCollEntity coll1 = new SetRefCollEntity(3, "coll1"); + + // Revision 1 + em.getTransaction().begin(); + + em.persist(str1); + + coll1.setCollection(new HashSet()); + em.persist(coll1); + + em.getTransaction().commit(); + + // Revision 2 + em.getTransaction().begin(); + + str1 = em.find(StrTestEntity.class, str1.getId()); + coll1 = em.find(SetRefCollEntity.class, coll1.getId()); + + coll1.getCollection().add(str1); + coll1.setData("coll2"); + + em.getTransaction().commit(); + + // + + str1_id = str1.getId(); + + coll1_id = coll1.getId(); + } + + @Test + public void testRevisionsCounts() { + assert Arrays.asList(1, 2).equals(getVersionsReader().getRevisions(SetRefCollEntity.class, coll1_id)); + + assert Arrays.asList(1).equals(getVersionsReader().getRevisions(StrTestEntity.class, str1_id)); + } + + @Test + public void testHistoryOfColl1() { + StrTestEntity str1 = getEntityManager().find(StrTestEntity.class, str1_id); + + SetRefCollEntity rev1 = getVersionsReader().find(SetRefCollEntity.class, coll1_id, 1); + SetRefCollEntity rev2 = getVersionsReader().find(SetRefCollEntity.class, coll1_id, 2); + + assert rev1.getCollection().equals(TestTools.makeSet()); + assert rev2.getCollection().equals(TestTools.makeSet(str1)); + + assert "coll1".equals(rev1.getData()); + assert "coll2".equals(rev2.getData()); + } +} \ No newline at end of file Property changes on: trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/DataChangesDetachedSet.java ___________________________________________________________________ Name: svn:mergeinfo + Deleted: trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/DataChangesNotOwnedSet.java =================================================================== --- trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/DataChangesNotOwnedSet.java 2008-09-21 20:42:00 UTC (rev 155) +++ trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/DataChangesNotOwnedSet.java 2008-09-21 22:00:50 UTC (rev 156) @@ -1,84 +0,0 @@ -package org.jboss.envers.test.integration.onetomany.detached; - -import org.jboss.envers.test.integration.AbstractEntityTest; -import org.jboss.envers.test.entities.onetomany.detached.SetRefCollEntity; -import org.jboss.envers.test.entities.StrTestEntity; -import org.jboss.envers.test.tools.TestTools; -import org.hibernate.ejb.Ejb3Configuration; -import org.testng.annotations.BeforeClass; -import org.testng.annotations.Test; - -import javax.persistence.EntityManager; -import java.util.Arrays; -import java.util.HashSet; - -/** - * @author Adam Warski (adam at warski dot org) - */ -public class DataChangesNotOwnedSet extends AbstractEntityTest { - private Integer str1_id; - - private Integer coll1_id; - - public void configure(Ejb3Configuration cfg) { - cfg.addAnnotatedClass(StrTestEntity.class); - cfg.addAnnotatedClass(SetRefCollEntity.class); - } - - @BeforeClass(dependsOnMethods = "init") - public void initData() { - EntityManager em = getEntityManager(); - - StrTestEntity str1 = new StrTestEntity("str1"); - - SetRefCollEntity coll1 = new SetRefCollEntity(3, "coll1"); - - // Revision 1 - em.getTransaction().begin(); - - em.persist(str1); - - coll1.setCollection(new HashSet()); - em.persist(coll1); - - em.getTransaction().commit(); - - // Revision 2 - em.getTransaction().begin(); - - str1 = em.find(StrTestEntity.class, str1.getId()); - coll1 = em.find(SetRefCollEntity.class, coll1.getId()); - - coll1.getCollection().add(str1); - coll1.setData("coll2"); - - em.getTransaction().commit(); - - // - - str1_id = str1.getId(); - - coll1_id = coll1.getId(); - } - - @Test - public void testRevisionsCounts() { - assert Arrays.asList(1, 2).equals(getVersionsReader().getRevisions(SetRefCollEntity.class, coll1_id)); - - assert Arrays.asList(1).equals(getVersionsReader().getRevisions(StrTestEntity.class, str1_id)); - } - - @Test - public void testHistoryOfColl1() { - StrTestEntity str1 = getEntityManager().find(StrTestEntity.class, str1_id); - - SetRefCollEntity rev1 = getVersionsReader().find(SetRefCollEntity.class, coll1_id, 1); - SetRefCollEntity rev2 = getVersionsReader().find(SetRefCollEntity.class, coll1_id, 2); - - assert rev1.getCollection().equals(TestTools.makeSet()); - assert rev2.getCollection().equals(TestTools.makeSet(str1)); - - assert "coll1".equals(rev1.getData()); - assert "coll2".equals(rev2.getData()); - } -} \ No newline at end of file Copied: trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/DoubleDetachedSet.java (from rev 148, trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/DoubleNotOwnedSet.java) =================================================================== --- trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/DoubleDetachedSet.java (rev 0) +++ trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/DoubleDetachedSet.java 2008-09-21 22:00:50 UTC (rev 156) @@ -0,0 +1,108 @@ +package org.jboss.envers.test.integration.onetomany.detached; + +import org.jboss.envers.test.integration.AbstractEntityTest; +import org.jboss.envers.test.entities.onetomany.detached.DoubleSetRefCollEntity; +import org.jboss.envers.test.entities.StrTestEntity; +import org.jboss.envers.test.tools.TestTools; +import org.hibernate.ejb.Ejb3Configuration; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +import javax.persistence.EntityManager; +import java.util.Arrays; +import java.util.HashSet; + +/** + * @author Adam Warski (adam at warski dot org) + */ +public class DoubleDetachedSet extends AbstractEntityTest { + private Integer str1_id; + private Integer str2_id; + + private Integer coll1_id; + + public void configure(Ejb3Configuration cfg) { + cfg.addAnnotatedClass(StrTestEntity.class); + cfg.addAnnotatedClass(DoubleSetRefCollEntity.class); + } + + @BeforeClass(dependsOnMethods = "init") + public void initData() { + EntityManager em = getEntityManager(); + + StrTestEntity str1 = new StrTestEntity("str1"); + StrTestEntity str2 = new StrTestEntity("str2"); + + DoubleSetRefCollEntity coll1 = new DoubleSetRefCollEntity(3, "coll1"); + + // Revision 1 + em.getTransaction().begin(); + + em.persist(str1); + em.persist(str2); + + coll1.setCollection(new HashSet()); + coll1.getCollection().add(str1); + em.persist(coll1); + + coll1.setCollection2(new HashSet()); + coll1.getCollection2().add(str2); + em.persist(coll1); + + em.getTransaction().commit(); + + // Revision 2 + em.getTransaction().begin(); + + str2 = em.find(StrTestEntity.class, str2.getId()); + coll1 = em.find(DoubleSetRefCollEntity.class, coll1.getId()); + + coll1.getCollection().add(str2); + + em.getTransaction().commit(); + + // Revision 3 + em.getTransaction().begin(); + + str1 = em.find(StrTestEntity.class, str1.getId()); + coll1 = em.find(DoubleSetRefCollEntity.class, coll1.getId()); + + coll1.getCollection().remove(str1); + coll1.getCollection2().add(str1); + + em.getTransaction().commit(); + + // + + str1_id = str1.getId(); + str2_id = str2.getId(); + + coll1_id = coll1.getId(); + } + + @Test + public void testRevisionsCounts() { + assert Arrays.asList(1, 2, 3).equals(getVersionsReader().getRevisions(DoubleSetRefCollEntity.class, coll1_id)); + + assert Arrays.asList(1).equals(getVersionsReader().getRevisions(StrTestEntity.class, str1_id)); + assert Arrays.asList(1).equals(getVersionsReader().getRevisions(StrTestEntity.class, str2_id)); + } + + @Test + public void testHistoryOfColl1() { + StrTestEntity str1 = getEntityManager().find(StrTestEntity.class, str1_id); + StrTestEntity str2 = getEntityManager().find(StrTestEntity.class, str2_id); + + DoubleSetRefCollEntity rev1 = getVersionsReader().find(DoubleSetRefCollEntity.class, coll1_id, 1); + DoubleSetRefCollEntity rev2 = getVersionsReader().find(DoubleSetRefCollEntity.class, coll1_id, 2); + DoubleSetRefCollEntity rev3 = getVersionsReader().find(DoubleSetRefCollEntity.class, coll1_id, 3); + + assert rev1.getCollection().equals(TestTools.makeSet(str1)); + assert rev2.getCollection().equals(TestTools.makeSet(str1, str2)); + assert rev3.getCollection().equals(TestTools.makeSet(str2)); + + assert rev1.getCollection2().equals(TestTools.makeSet(str2)); + assert rev2.getCollection2().equals(TestTools.makeSet(str2)); + assert rev3.getCollection2().equals(TestTools.makeSet(str1, str2)); + } +} \ No newline at end of file Property changes on: trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/DoubleDetachedSet.java ___________________________________________________________________ Name: svn:mergeinfo + Deleted: trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/DoubleNotOwnedSet.java =================================================================== --- trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/DoubleNotOwnedSet.java 2008-09-21 20:42:00 UTC (rev 155) +++ trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/DoubleNotOwnedSet.java 2008-09-21 22:00:50 UTC (rev 156) @@ -1,108 +0,0 @@ -package org.jboss.envers.test.integration.onetomany.detached; - -import org.jboss.envers.test.integration.AbstractEntityTest; -import org.jboss.envers.test.entities.onetomany.detached.DoubleSetRefCollEntity; -import org.jboss.envers.test.entities.StrTestEntity; -import org.jboss.envers.test.tools.TestTools; -import org.hibernate.ejb.Ejb3Configuration; -import org.testng.annotations.BeforeClass; -import org.testng.annotations.Test; - -import javax.persistence.EntityManager; -import java.util.Arrays; -import java.util.HashSet; - -/** - * @author Adam Warski (adam at warski dot org) - */ -public class DoubleNotOwnedSet extends AbstractEntityTest { - private Integer str1_id; - private Integer str2_id; - - private Integer coll1_id; - - public void configure(Ejb3Configuration cfg) { - cfg.addAnnotatedClass(StrTestEntity.class); - cfg.addAnnotatedClass(DoubleSetRefCollEntity.class); - } - - @BeforeClass(dependsOnMethods = "init") - public void initData() { - EntityManager em = getEntityManager(); - - StrTestEntity str1 = new StrTestEntity("str1"); - StrTestEntity str2 = new StrTestEntity("str2"); - - DoubleSetRefCollEntity coll1 = new DoubleSetRefCollEntity(3, "coll1"); - - // Revision 1 - em.getTransaction().begin(); - - em.persist(str1); - em.persist(str2); - - coll1.setCollection(new HashSet()); - coll1.getCollection().add(str1); - em.persist(coll1); - - coll1.setCollection2(new HashSet()); - coll1.getCollection2().add(str2); - em.persist(coll1); - - em.getTransaction().commit(); - - // Revision 2 - em.getTransaction().begin(); - - str2 = em.find(StrTestEntity.class, str2.getId()); - coll1 = em.find(DoubleSetRefCollEntity.class, coll1.getId()); - - coll1.getCollection().add(str2); - - em.getTransaction().commit(); - - // Revision 3 - em.getTransaction().begin(); - - str1 = em.find(StrTestEntity.class, str1.getId()); - coll1 = em.find(DoubleSetRefCollEntity.class, coll1.getId()); - - coll1.getCollection().remove(str1); - coll1.getCollection2().add(str1); - - em.getTransaction().commit(); - - // - - str1_id = str1.getId(); - str2_id = str2.getId(); - - coll1_id = coll1.getId(); - } - - @Test - public void testRevisionsCounts() { - assert Arrays.asList(1, 2, 3).equals(getVersionsReader().getRevisions(DoubleSetRefCollEntity.class, coll1_id)); - - assert Arrays.asList(1).equals(getVersionsReader().getRevisions(StrTestEntity.class, str1_id)); - assert Arrays.asList(1).equals(getVersionsReader().getRevisions(StrTestEntity.class, str2_id)); - } - - @Test - public void testHistoryOfColl1() { - StrTestEntity str1 = getEntityManager().find(StrTestEntity.class, str1_id); - StrTestEntity str2 = getEntityManager().find(StrTestEntity.class, str2_id); - - DoubleSetRefCollEntity rev1 = getVersionsReader().find(DoubleSetRefCollEntity.class, coll1_id, 1); - DoubleSetRefCollEntity rev2 = getVersionsReader().find(DoubleSetRefCollEntity.class, coll1_id, 2); - DoubleSetRefCollEntity rev3 = getVersionsReader().find(DoubleSetRefCollEntity.class, coll1_id, 3); - - assert rev1.getCollection().equals(TestTools.makeSet(str1)); - assert rev2.getCollection().equals(TestTools.makeSet(str1, str2)); - assert rev3.getCollection().equals(TestTools.makeSet(str2)); - - assert rev1.getCollection2().equals(TestTools.makeSet(str2)); - assert rev2.getCollection2().equals(TestTools.makeSet(str2)); - assert rev3.getCollection2().equals(TestTools.makeSet(str1, str2)); - } -} \ No newline at end of file From jboss-envers-commits at lists.jboss.org Mon Sep 22 05:12:36 2008 From: jboss-envers-commits at lists.jboss.org (jboss-envers-commits at lists.jboss.org) Date: Mon, 22 Sep 2008 05:12:36 -0400 Subject: [jboss-envers-commits] JBoss Envers SVN: r157 - in trunk/src: main/org/jboss/envers/entities/mapper/relation and 3 other directories. Message-ID: Author: adamw Date: 2008-09-22 05:12:36 -0400 (Mon, 22 Sep 2008) New Revision: 157 Removed: trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/DetachedRelationInitializor.java trunk/src/test/org/jboss/envers/test/entities/manytomany/unidirectional/ListOwningUniEntity.java trunk/src/test/org/jboss/envers/test/entities/manytomany/unidirectional/SetOwningUniEntity.java Modified: trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java trunk/src/main/org/jboss/envers/entities/mapper/relation/MiddleIdData.java trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleComponentMapper.java Log: ENVERS-26: intial support for @OneToMany+ at JoinTable Modified: trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java =================================================================== --- trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java 2008-09-21 22:00:50 UTC (rev 156) +++ trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java 2008-09-22 09:12:36 UTC (rev 157) @@ -40,7 +40,7 @@ Type type = value.getType(); if ((type instanceof BagType || type instanceof SetType) && - (value.getElement() instanceof OneToMany)) { + (value.getElement() instanceof OneToMany) && (value.isInverse())) { addOneToManyAttached(name, value, currentMapper, entityName); } else { addWithMiddleTable(name, value, currentMapper, entityName, xmlMappingData); @@ -110,9 +110,11 @@ name)); } - private String getReferencedEntityName(Collection value) { - if (value.getElement() instanceof ToOne) { - return ((ToOne) value.getElement()).getReferencedEntityName(); + private String getReferencedEntityName(Value value) { + if (value instanceof ToOne) { + return ((ToOne) value).getReferencedEntityName(); + } if (value instanceof OneToMany) { + return ((OneToMany) value).getReferencedEntityName(); } else { return null; } @@ -127,15 +129,27 @@ } } + private String getMiddleTableName(Collection value, String entityName) { + // We check how Hibernate maps the collection. + if (value.getKey().getTable().equals(value.getCollectionTable())) { + // The table of the element is the same as the collection table - this must be a @JoinColumn+ at OneToMany + // mapping. Generating the table name ourselves, as Hibernate doesn't use a middle table for mapping + // this relation. + return StringTools.getLastComponent(entityName) + "_" + StringTools.getLastComponent(getReferencedEntityName(value.getElement())); + } else { + // Hibernate uses a middle table for mapping this relation, so we get it's name directly. + return value.getCollectionTable().getName(); + } + } + @SuppressWarnings({"unchecked"}) private void addWithMiddleTable(String name, Collection value, CompositeMapperBuilder currentMapper, String entityName, EntityXmlMappingData xmlMappingData) { // Generating the name of the middle table, its schema and catalog // TODO: add support for @VersionsJoinTable - String middleTableName = value.getCollectionTable().getName(); - String middleEntityName = middleTableName; + String middleTableName = getMiddleTableName(value, entityName);//value.getCollectionTable().getName(); String versionsMiddleTableName = mainGenerator.getVerEntCfg().getVersionsTableName(null, middleTableName); - String versionsMiddleEntityName = mainGenerator.getVerEntCfg().getVersionsEntityName(middleEntityName); + String versionsMiddleEntityName = mainGenerator.getVerEntCfg().getVersionsEntityName(middleTableName); String schema = value.getCollectionTable().getSchema(); String catalog = value.getCollectionTable().getCatalog(); @@ -165,7 +179,7 @@ IdMappingData referencingIdMapping = referencingConfiguration.getIdMappingData(); // Null if this collection doesn't reference an entity. - String referencedEntityName = getReferencedEntityName(value); + String referencedEntityName = getReferencedEntityName(value.getElement()); // Only valid for an inverse relation; null otherwise. String mappedBy; @@ -305,10 +319,9 @@ if (type instanceof ManyToOneType) { String prefixRelated = prefix + "_"; - ToOne toOneValue = (ToOne) value; - String referencedEntityName = toOneValue.getReferencedEntityName(); + String referencedEntityName = getReferencedEntityName(value); IdMappingData referencedIdMapping = mainGenerator.getEntitiesConfigurations().get( - toOneValue.getReferencedEntityName()).getIdMappingData(); + referencedEntityName).getIdMappingData(); // Adding related-entity (in this case: the referenced entities id) id mapping to the xml only if the // relation isn't inverse (so when middleEntityXml is not null). 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-21 22:00:50 UTC (rev 156) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/MiddleIdData.java 2008-09-22 09:12:36 UTC (rev 157) @@ -8,9 +8,21 @@ * @author Adam Warski (adam at warski dot org) */ public final class MiddleIdData { + /** + * Original id mapper of the related entity. + */ private final IdMapper originalMapper; + /** + * Prefixed id mapper (with the names for the id fields that are used in the middle table) of the related entity. + */ private final IdMapper prefixedMapper; + /** + * Name of the related entity. + */ private final String entityName; + /** + * Versions name of the related entity. + */ private final String versionsEntityName; public MiddleIdData(IdMapper originalMapper, IdMapper prefixedMapper, String entityName, String versionsEntityName) { Modified: trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleComponentMapper.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleComponentMapper.java 2008-09-21 22:00:50 UTC (rev 156) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleComponentMapper.java 2008-09-22 09:12:36 UTC (rev 157) @@ -20,7 +20,7 @@ Number revision); /** - * Maps from an object to the object's representation (for an entity - only its id). + * Maps from an object to the object's map representation (for an entity - only its id). * @param data Map to which data should be added. * @param obj Object to map from. */ Deleted: trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/DetachedRelationInitializor.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/DetachedRelationInitializor.java 2008-09-21 22:00:50 UTC (rev 156) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/DetachedRelationInitializor.java 2008-09-22 09:12:36 UTC (rev 157) @@ -1,78 +0,0 @@ -/* - * JBoss, Home of Professional Open Source - * - * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated - * by the @authors tag. All rights reserved. - * - * See the copyright.txt in the distribution for a full listing of individual - * contributors. 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.entities.mapper.relation.lazy.initializor.Initializor; -import org.jboss.envers.entities.EntityInstantiator; -import org.jboss.envers.reader.VersionsReaderImplementor; -import org.jboss.envers.exception.VersionsException; -import org.jboss.envers.configuration.VersionsConfiguration; - -import java.util.Collection; -import java.util.List; - -/** - * @author Adam Warski (adam at warski dot org) - */ -public class DetachedRelationInitializor implements Initializor { - private final VersionsConfiguration verCfg; - private final String entityName; - //private final DetachedRelationQueryGenerator queryGenerator; - private final VersionsReaderImplementor versionsReader; - private final Object primaryKey; - private final Number revision; - private final Class collectionClass; - - public DetachedRelationInitializor(VersionsConfiguration verCfg, String entityName, - //DetachedRelationQueryGenerator queryGenerator, - VersionsReaderImplementor versionsReader, Object primaryKey, - Number revision, Class collectionClass) { - this.verCfg = verCfg; - this.entityName = entityName; - //this.queryGenerator = queryGenerator; - this.versionsReader = versionsReader; - this.primaryKey = primaryKey; - this.revision = revision; - this.collectionClass = collectionClass; - } - - @SuppressWarnings({"unchecked"}) - public T initialize() { - EntityInstantiator entityInstantiator = new EntityInstantiator(verCfg, versionsReader); - - List queryResult = null;//queryGenerator.getQuery(versionsReader, primaryKey, revision).list(); - - T result; - try { - result = collectionClass.newInstance(); - } catch (Exception e) { - throw new VersionsException(e); - } - - // TODO - entityInstantiator.addInstancesFromVersionsEntities(entityName, (Collection) result, queryResult, revision); - - return result; - } -} Deleted: trunk/src/test/org/jboss/envers/test/entities/manytomany/unidirectional/ListOwningUniEntity.java =================================================================== --- trunk/src/test/org/jboss/envers/test/entities/manytomany/unidirectional/ListOwningUniEntity.java 2008-09-21 22:00:50 UTC (rev 156) +++ trunk/src/test/org/jboss/envers/test/entities/manytomany/unidirectional/ListOwningUniEntity.java 2008-09-22 09:12:36 UTC (rev 157) @@ -1,81 +0,0 @@ -package org.jboss.envers.test.entities.manytomany.unidirectional; - -import org.jboss.envers.Versioned; - -import javax.persistence.*; -import java.util.List; - -/** - * Entity owning the many-to-many relation - * @author Adam Warski (adam at warski dot org) - */ - at Entity -public class ListOwningUniEntity { - @Id - private Integer id; - - @Versioned - private String data; - - @Versioned - @ManyToMany - private List references; - - public ListOwningUniEntity() { } - - public ListOwningUniEntity(Integer id, String data) { - this.id = id; - this.data = data; - } - - public ListOwningUniEntity(String data) { - this.data = data; - } - - public Integer getId() { - return id; - } - - public void setId(Integer id) { - this.id = id; - } - - public String getData() { - return data; - } - - public void setData(String data) { - this.data = data; - } - - public List getReferences() { - return references; - } - - public void setReferences(List references) { - this.references = references; - } - - public boolean equals(Object o) { - if (this == o) return true; - if (!(o instanceof ListOwningUniEntity)) return false; - - ListOwningUniEntity that = (ListOwningUniEntity) o; - - if (data != null ? !data.equals(that.data) : that.data != null) return false; - if (id != null ? !id.equals(that.id) : that.id != null) return false; - - return true; - } - - public int hashCode() { - int result; - result = (id != null ? id.hashCode() : 0); - result = 31 * result + (data != null ? data.hashCode() : 0); - return result; - } - - public String toString() { - return "SetOwningEntity(id = " + id + ", data = " + data + ")"; - } -} \ No newline at end of file Deleted: trunk/src/test/org/jboss/envers/test/entities/manytomany/unidirectional/SetOwningUniEntity.java =================================================================== --- trunk/src/test/org/jboss/envers/test/entities/manytomany/unidirectional/SetOwningUniEntity.java 2008-09-21 22:00:50 UTC (rev 156) +++ trunk/src/test/org/jboss/envers/test/entities/manytomany/unidirectional/SetOwningUniEntity.java 2008-09-22 09:12:36 UTC (rev 157) @@ -1,81 +0,0 @@ -package org.jboss.envers.test.entities.manytomany.unidirectional; - -import org.jboss.envers.Versioned; - -import javax.persistence.*; -import java.util.Set; - -/** - * Entity owning the many-to-many relation - * @author Adam Warski (adam at warski dot org) - */ - at Entity -public class SetOwningUniEntity { - @Id - private Integer id; - - @Versioned - private String data; - - @Versioned - @ManyToMany - private Set references; - - public SetOwningUniEntity() { } - - public SetOwningUniEntity(Integer id, String data) { - this.id = id; - this.data = data; - } - - public SetOwningUniEntity(String data) { - this.data = data; - } - - public Integer getId() { - return id; - } - - public void setId(Integer id) { - this.id = id; - } - - public String getData() { - return data; - } - - public void setData(String data) { - this.data = data; - } - - public Set getReferences() { - return references; - } - - public void setReferences(Set references) { - this.references = references; - } - - public boolean equals(Object o) { - if (this == o) return true; - if (!(o instanceof SetOwningUniEntity)) return false; - - SetOwningUniEntity that = (SetOwningUniEntity) o; - - if (data != null ? !data.equals(that.data) : that.data != null) return false; - if (id != null ? !id.equals(that.id) : that.id != null) return false; - - return true; - } - - public int hashCode() { - int result; - result = (id != null ? id.hashCode() : 0); - result = 31 * result + (data != null ? data.hashCode() : 0); - return result; - } - - public String toString() { - return "SetOwningUniEntity(id = " + id + ", data = " + data + ")"; - } -} \ No newline at end of file From jboss-envers-commits at lists.jboss.org Mon Sep 22 05:48:53 2008 From: jboss-envers-commits at lists.jboss.org (jboss-envers-commits at lists.jboss.org) Date: Mon, 22 Sep 2008 05:48:53 -0400 Subject: [jboss-envers-commits] JBoss Envers SVN: r158 - in trunk/src/main/org/jboss/envers: configuration and 1 other directories. Message-ID: Author: adamw Date: 2008-09-22 05:48:53 -0400 (Mon, 22 Sep 2008) New Revision: 158 Added: trunk/src/main/org/jboss/envers/VersionsJoinTable.java Modified: trunk/src/main/org/jboss/envers/VersionsTable.java trunk/src/main/org/jboss/envers/configuration/EntitiesConfigurator.java trunk/src/main/org/jboss/envers/configuration/metadata/AnnotationsMetadataReader.java trunk/src/main/org/jboss/envers/configuration/metadata/PersistentClassVersioningData.java Log: ENVERS-26: adding reading of the VersionsJoinTable annotation, restructuring AnnotationMetadataReader from stateless to stateful Copied: trunk/src/main/org/jboss/envers/VersionsJoinTable.java (from rev 148, trunk/src/main/org/jboss/envers/SecondaryVersionsTable.java) =================================================================== --- trunk/src/main/org/jboss/envers/VersionsJoinTable.java (rev 0) +++ trunk/src/main/org/jboss/envers/VersionsJoinTable.java 2008-09-22 09:48:53 UTC (rev 158) @@ -0,0 +1,57 @@ +/* + * 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 javax.persistence.JoinColumn; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * @author Adam Warski (adam at warski dot org) + */ + at Retention(RetentionPolicy.RUNTIME) + at Target({ElementType.FIELD, ElementType.METHOD}) +public @interface VersionsJoinTable { + /** + * @return Name of the join table. Defaults to a concatenation of the names of the primary table of the entity + * owning the association and of the primary table of the entity referenced by the association. + */ + String name() default ""; + + /** + * @return The schema of the join table. Defaults to the schema of the entity owning the association. + */ + String schema() default ""; + + /** + * @return The catalog of the join table. Defaults to the catalog of the entity owning the association. + */ + String catalog() default ""; + + /** + * @return The foreign key columns of the join table which reference the primary table of the entity owning the + * association (i.e. the owning side of the association). + */ + JoinColumn[] joinColumns() default {}; +} \ No newline at end of file Property changes on: trunk/src/main/org/jboss/envers/VersionsJoinTable.java ___________________________________________________________________ Name: svn:mergeinfo + Modified: trunk/src/main/org/jboss/envers/VersionsTable.java =================================================================== --- trunk/src/main/org/jboss/envers/VersionsTable.java 2008-09-22 09:12:36 UTC (rev 157) +++ trunk/src/main/org/jboss/envers/VersionsTable.java 2008-09-22 09:48:53 UTC (rev 158) @@ -29,19 +29,18 @@ /** * @author Adam Warski (adam at warski dot org) */ - at SuppressWarnings({"JavaDoc"}) @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) public @interface VersionsTable { String value(); /** - * The schema of the table. Defaults to the schema of the annotated entity. + * @return The schema of the table. Defaults to the schema of the annotated entity. */ String schema() default ""; /** - * The catalog of the table. Defaults to the catalog of the annotated entity. + * @return The catalog of the table. Defaults to the catalog of the annotated entity. */ String catalog() default ""; } Modified: trunk/src/main/org/jboss/envers/configuration/EntitiesConfigurator.java =================================================================== --- trunk/src/main/org/jboss/envers/configuration/EntitiesConfigurator.java 2008-09-22 09:12:36 UTC (rev 157) +++ trunk/src/main/org/jboss/envers/configuration/EntitiesConfigurator.java 2008-09-22 09:48:53 UTC (rev 158) @@ -65,12 +65,12 @@ Map xmlMappings = new HashMap(); // First pass - AnnotationsMetadataReader annotationsMetadataReader = new AnnotationsMetadataReader(); while (classes.hasNext()) { PersistentClass pc = classes.next(); // Collecting information from annotations on the persistent class pc - PersistentClassVersioningData versioningData = - annotationsMetadataReader.getVersioningData(pc, reflectionManager, globalCfg); + AnnotationsMetadataReader annotationsMetadataReader = + new AnnotationsMetadataReader(globalCfg, reflectionManager, pc); + PersistentClassVersioningData versioningData = annotationsMetadataReader.getVersioningData(); if (versioningData.isVersioned()) { pcDatas.put(pc, versioningData); Modified: trunk/src/main/org/jboss/envers/configuration/metadata/AnnotationsMetadataReader.java =================================================================== --- trunk/src/main/org/jboss/envers/configuration/metadata/AnnotationsMetadataReader.java 2008-09-22 09:12:36 UTC (rev 157) +++ trunk/src/main/org/jboss/envers/configuration/metadata/AnnotationsMetadataReader.java 2008-09-22 09:48:53 UTC (rev 158) @@ -32,87 +32,126 @@ import org.hibernate.MappingException; /** - * A helper class to read versioning meta-data from annotations on persistent classes. + * A helper class to read versioning meta-data from annotations on a persistent class. * @author Adam Warski (adam at warski dot org) * @author Sebastian Komander */ -public class AnnotationsMetadataReader { - private void addFromProperties(Iterable properties, PersistentClassVersioningData versioningData, - GlobalConfiguration globalCfg) { - for (YProperty property : properties) { - Versioned ver = property.getAnnotation(Versioned.class); - if (ver != null) { - versioningData.propertyStoreInfo.propertyStores.put(property.getName(), ver.modStore()); - } +public final class AnnotationsMetadataReader { + private final GlobalConfiguration globalCfg; + private final YReflectionManager reflectionManager; + private final PersistentClass pc; - // 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()); - } else { - // if the optimistic locking field has to be unversioned and the current property - // is the optimistic locking field, add it to the unversioned properties list - if (globalCfg.isUnversionedOptimisticLockingField()) { - Version jpaVer = property.getAnnotation(Version.class); - if (jpaVer != null) { - versioningData.unversionedProperties.add(property.getName()); - } - } + /** + * This object is filled with information read from annotations and returned by the getVersioningData + * method. + */ + private final PersistentClassVersioningData versioningData; + + public AnnotationsMetadataReader(GlobalConfiguration globalCfg, YReflectionManager reflectionManager, + PersistentClass pc) { + this.globalCfg = globalCfg; + this.reflectionManager = reflectionManager; + this.pc = pc; + + versioningData = new PersistentClassVersioningData(); + } + + private void addPropertyVersioned(YProperty property) { + Versioned ver = property.getAnnotation(Versioned.class); + if (ver != null) { + versioningData.propertyStoreInfo.propertyStores.put(property.getName(), ver.modStore()); + } + } + + private void addPropertyUnversioned(YProperty property) { + // 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()); + } else { + // if the optimistic locking field has to be unversioned and the current property + // is the optimistic locking field, add it to the unversioned properties list + if (globalCfg.isUnversionedOptimisticLockingField()) { + Version jpaVer = property.getAnnotation(Version.class); + if (jpaVer != null) { + versioningData.unversionedProperties.add(property.getName()); + } } } } - private void addPropertiesFromClass(YClass clazz, PersistentClassVersioningData versioningData, - GlobalConfiguration globalCfg) { + private void addPropertyJoinTables(YProperty property) { + VersionsJoinTable joinTable = property.getAnnotation(VersionsJoinTable.class); + if (joinTable != null) { + versioningData.versionsJoinTables.put(property.getName(), joinTable); + } + } + + private void addFromProperties(Iterable properties) { + for (YProperty property : properties) { + addPropertyVersioned(property); + addPropertyUnversioned(property); + addPropertyJoinTables(property); + } + } + + private void addPropertiesFromClass(YClass clazz) { YClass superclazz = clazz.getSuperclass(); if (!"java.lang.Object".equals(superclazz.getName())) { - addPropertiesFromClass(superclazz, versioningData, globalCfg); + addPropertiesFromClass(superclazz); } - addFromProperties(clazz.getDeclaredProperties("field"), versioningData, globalCfg); - addFromProperties(clazz.getDeclaredProperties("property"), versioningData, globalCfg); + addFromProperties(clazz.getDeclaredProperties("field")); + addFromProperties(clazz.getDeclaredProperties("property")); } - public PersistentClassVersioningData getVersioningData(PersistentClass pc, YReflectionManager reflectionManager, - GlobalConfiguration globalCfg) { - PersistentClassVersioningData versioningData = new PersistentClassVersioningData(); + private void addDefaultVersioned(YClass clazz) { + Versioned defaultVersioned = clazz.getAnnotation(Versioned.class); + if (defaultVersioned != null) { + versioningData.propertyStoreInfo.defaultStore = defaultVersioned.modStore(); + } + } + + private void addVersionsTable(YClass clazz) { + VersionsTable versionsTable = clazz.getAnnotation(VersionsTable.class); + if (versionsTable != null) { + versioningData.versionsTableName = versionsTable.value(); + versioningData.schema = versionsTable.schema(); + versioningData.catalog = versionsTable.catalog(); + } + } + + private void addVersionsSecondaryTables(YClass clazz) { + // Getting information on secondary tables + SecondaryVersionsTable secondaryVersionsTable1 = clazz.getAnnotation(SecondaryVersionsTable.class); + if (secondaryVersionsTable1 != null) { + versioningData.secondaryTableDictionary.put(secondaryVersionsTable1.secondaryTableName(), + secondaryVersionsTable1.secondaryVersionsTableName()); + } + + SecondaryVersionsTables secondaryVersionsTables = clazz.getAnnotation(SecondaryVersionsTables.class); + if (secondaryVersionsTables != null) { + for (SecondaryVersionsTable secondaryVersionsTable2 : secondaryVersionsTables.value()) { + versioningData.secondaryTableDictionary.put(secondaryVersionsTable2.secondaryTableName(), + secondaryVersionsTable2.secondaryVersionsTableName()); + } + } + } + + public PersistentClassVersioningData getVersioningData() { if (pc.getClassName() == null) { return versioningData; } try { YClass clazz = reflectionManager.classForName(pc.getClassName(), this.getClass()); - Versioned defaultVersioned = clazz.getAnnotation(Versioned.class); - if (defaultVersioned != null) { - versioningData.propertyStoreInfo.defaultStore = defaultVersioned.modStore(); - } - - addPropertiesFromClass(clazz, versioningData, globalCfg); - - VersionsTable versionsTable = clazz.getAnnotation(VersionsTable.class); - if (versionsTable != null) { - versioningData.versionsTableName = versionsTable.value(); - versioningData.schema = versionsTable.schema(); - versioningData.catalog = versionsTable.catalog(); - } - - // Getting information on secondary tables - SecondaryVersionsTable secondaryVersionsTable1 = clazz.getAnnotation(SecondaryVersionsTable.class); - if (secondaryVersionsTable1 != null) { - versioningData.secondaryTableDictionary.put(secondaryVersionsTable1.secondaryTableName(), - secondaryVersionsTable1.secondaryVersionsTableName()); - } - - SecondaryVersionsTables secondaryVersionsTables = clazz.getAnnotation(SecondaryVersionsTables.class); - if (secondaryVersionsTables != null) { - for (SecondaryVersionsTable secondaryVersionsTable2 : secondaryVersionsTables.value()) { - versioningData.secondaryTableDictionary.put(secondaryVersionsTable2.secondaryTableName(), - secondaryVersionsTable2.secondaryVersionsTableName()); - } - } + addDefaultVersioned(clazz); + addPropertiesFromClass(clazz); + addVersionsTable(clazz); + addVersionsSecondaryTables(clazz); } catch (ClassNotFoundException e) { throw new MappingException(e); } Modified: trunk/src/main/org/jboss/envers/configuration/metadata/PersistentClassVersioningData.java =================================================================== --- trunk/src/main/org/jboss/envers/configuration/metadata/PersistentClassVersioningData.java 2008-09-22 09:12:36 UTC (rev 157) +++ trunk/src/main/org/jboss/envers/configuration/metadata/PersistentClassVersioningData.java 2008-09-22 09:48:53 UTC (rev 158) @@ -22,6 +22,7 @@ package org.jboss.envers.configuration.metadata; import org.jboss.envers.ModificationStore; +import org.jboss.envers.VersionsJoinTable; import java.util.HashMap; import java.util.Map; @@ -37,6 +38,7 @@ propertyStoreInfo = new PropertyStoreInfo(new HashMap()); secondaryTableDictionary = new HashMap(); unversionedProperties = new ArrayList(); + versionsJoinTables = new HashMap(); } public PropertyStoreInfo propertyStoreInfo; @@ -45,6 +47,10 @@ public String catalog; public Map secondaryTableDictionary; public List unversionedProperties; + /** + * A map from property names to custom join tables definitions. + */ + public Map versionsJoinTables; public boolean isVersioned() { if (propertyStoreInfo.propertyStores.size() > 0) { return true; } From jboss-envers-commits at lists.jboss.org Mon Sep 22 07:26:24 2008 From: jboss-envers-commits at lists.jboss.org (jboss-envers-commits at lists.jboss.org) Date: Mon, 22 Sep 2008 07:26:24 -0400 Subject: [jboss-envers-commits] JBoss Envers SVN: r159 - in trunk/src: main/org/jboss/envers/configuration and 4 other directories. Message-ID: Author: adamw Date: 2008-09-22 07:26:24 -0400 (Mon, 22 Sep 2008) New Revision: 159 Added: trunk/src/test/org/jboss/envers/test/entities/onetomany/detached/SetJoinColumnRefCollEntity.java trunk/src/test/org/jboss/envers/test/integration/naming/VersionsJoinTableNaming.java trunk/src/test/org/jboss/envers/test/integration/naming/VersionsJoinTableTestEntity.java trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicJoinColumnSet.java Modified: trunk/src/main/org/jboss/envers/VersionsJoinTable.java trunk/src/main/org/jboss/envers/configuration/EntitiesConfigurator.java trunk/src/main/org/jboss/envers/configuration/metadata/AnnotationsMetadataReader.java trunk/src/main/org/jboss/envers/configuration/metadata/BasicMetadataGenerator.java trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java trunk/src/main/org/jboss/envers/configuration/metadata/MetadataTools.java trunk/src/main/org/jboss/envers/configuration/metadata/PersistentClassVersioningData.java trunk/src/main/org/jboss/envers/configuration/metadata/ToOneRelationMetadataGenerator.java trunk/src/main/org/jboss/envers/configuration/metadata/VersionsMetadataGenerator.java trunk/src/test/org/jboss/envers/test/integration/naming/OneToManyUnidirectionalNaming.java Log: ENVERS-26: support for @VersionsJoinTable, and unidirectional one-to-many associations mapped with @OneToMany+ at JoinColumn Modified: trunk/src/main/org/jboss/envers/VersionsJoinTable.java =================================================================== --- trunk/src/main/org/jboss/envers/VersionsJoinTable.java 2008-09-22 09:48:53 UTC (rev 158) +++ trunk/src/main/org/jboss/envers/VersionsJoinTable.java 2008-09-22 11:26:24 UTC (rev 159) @@ -50,8 +50,8 @@ String catalog() default ""; /** - * @return The foreign key columns of the join table which reference the primary table of the entity owning the - * association (i.e. the owning side of the association). + * @return The foreign key columns of the join table which reference the primary table of the entity that does not + * own the association (i.e. the inverse side of the association). */ - JoinColumn[] joinColumns() default {}; + JoinColumn[] inverseJoinColumns() default {}; } \ No newline at end of file Modified: trunk/src/main/org/jboss/envers/configuration/EntitiesConfigurator.java =================================================================== --- trunk/src/main/org/jboss/envers/configuration/EntitiesConfigurator.java 2008-09-22 09:48:53 UTC (rev 158) +++ trunk/src/main/org/jboss/envers/configuration/EntitiesConfigurator.java 2008-09-22 11:26:24 UTC (rev 159) @@ -28,6 +28,7 @@ import org.jboss.envers.configuration.metadata.EntityXmlMappingData; import org.jboss.envers.tools.graph.GraphTopologicalSort; import org.jboss.envers.tools.reflection.YReflectionManager; +import org.jboss.envers.tools.StringTools; import org.dom4j.io.DOMWriter; import org.dom4j.io.XMLWriter; import org.dom4j.io.OutputFormat; @@ -75,8 +76,8 @@ if (versioningData.isVersioned()) { pcDatas.put(pc, versioningData); - if (versioningData.versionsTableName != null) { - verEntCfg.addCustomVersionsTableName(pc.getEntityName(), versioningData.versionsTableName); + if (!StringTools.isEmpty(versioningData.versionsTable.value())) { + verEntCfg.addCustomVersionsTableName(pc.getEntityName(), versioningData.versionsTable.value()); } EntityXmlMappingData xmlMappingData = new EntityXmlMappingData(); Modified: trunk/src/main/org/jboss/envers/configuration/metadata/AnnotationsMetadataReader.java =================================================================== --- trunk/src/main/org/jboss/envers/configuration/metadata/AnnotationsMetadataReader.java 2008-09-22 09:48:53 UTC (rev 158) +++ trunk/src/main/org/jboss/envers/configuration/metadata/AnnotationsMetadataReader.java 2008-09-22 11:26:24 UTC (rev 159) @@ -31,6 +31,8 @@ import org.hibernate.mapping.PersistentClass; import org.hibernate.MappingException; +import java.lang.annotation.Annotation; + /** * A helper class to read versioning meta-data from annotations on a persistent class. * @author Adam Warski (adam at warski dot org) @@ -117,9 +119,9 @@ private void addVersionsTable(YClass clazz) { VersionsTable versionsTable = clazz.getAnnotation(VersionsTable.class); if (versionsTable != null) { - versioningData.versionsTableName = versionsTable.value(); - versioningData.schema = versionsTable.schema(); - versioningData.catalog = versionsTable.catalog(); + versioningData.versionsTable = versionsTable; + } else { + versioningData.versionsTable = getDefaultVersionsTable(); } } @@ -158,4 +160,13 @@ return versioningData; } + + private VersionsTable getDefaultVersionsTable() { + return new VersionsTable() { + public String value() { return ""; } + public String schema() { return ""; } + public String catalog() { return ""; } + public Class annotationType() { return this.getClass(); } + }; + } } Modified: trunk/src/main/org/jboss/envers/configuration/metadata/BasicMetadataGenerator.java =================================================================== --- trunk/src/main/org/jboss/envers/configuration/metadata/BasicMetadataGenerator.java 2008-09-22 09:48:53 UTC (rev 158) +++ trunk/src/main/org/jboss/envers/configuration/metadata/BasicMetadataGenerator.java 2008-09-22 11:26:24 UTC (rev 159) @@ -108,7 +108,7 @@ while (properties.hasNext()) { Property property = properties.next(); mainGenerator.addValue(component_mapping, property.getName(), property.getValue(), - componentMapper, ModificationStore.FULL, entityName, xmlMappingData, firstPass); + componentMapper, ModificationStore.FULL, entityName, xmlMappingData, null, firstPass); } } } Modified: trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java =================================================================== --- trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java 2008-09-22 09:48:53 UTC (rev 158) +++ trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java 2008-09-22 11:26:24 UTC (rev 159) @@ -17,12 +17,15 @@ import org.jboss.envers.tools.Tools; import org.jboss.envers.tools.StringTools; import org.jboss.envers.ModificationStore; +import org.jboss.envers.VersionsJoinTable; import org.dom4j.Element; +import javax.persistence.JoinColumn; import java.util.*; import java.util.Set; import java.util.List; import java.util.Map; +import java.lang.annotation.Annotation; /** * Generates metadata for collection-valued properties. @@ -36,17 +39,29 @@ } void addCollection(String name, Collection value, CompositeMapperBuilder currentMapper, - String entityName, EntityXmlMappingData xmlMappingData) { + String entityName, EntityXmlMappingData xmlMappingData, + VersionsJoinTable joinTable) { Type type = value.getType(); if ((type instanceof BagType || type instanceof SetType) && (value.getElement() instanceof OneToMany) && (value.isInverse())) { addOneToManyAttached(name, value, currentMapper, entityName); } else { - addWithMiddleTable(name, value, currentMapper, entityName, xmlMappingData); + addWithMiddleTable(name, value, currentMapper, entityName, xmlMappingData, + joinTable == null ? getDefaultVersionsJoinTable() : joinTable); } } + private VersionsJoinTable getDefaultVersionsJoinTable() { + return new VersionsJoinTable() { + public String name() { return ""; } + public String schema() { return ""; } + public String catalog() { return ""; } + public JoinColumn[] inverseJoinColumns() { return new JoinColumn[0]; } + public Class annotationType() { return this.getClass(); } + }; + } + @SuppressWarnings({"unchecked"}) private String getMappedBy(Collection collectionValue) { Iterator assocClassProps = @@ -113,7 +128,7 @@ private String getReferencedEntityName(Value value) { if (value instanceof ToOne) { return ((ToOne) value).getReferencedEntityName(); - } if (value instanceof OneToMany) { + } else if (value instanceof OneToMany) { return ((OneToMany) value).getReferencedEntityName(); } else { return null; @@ -121,9 +136,11 @@ } @SuppressWarnings({"unchecked"}) - private void addRelatedToXmlMapping(Element xmlMapping, String prefix, Value relatedValue, IdMappingData relatedIdMapping) { + private void addRelatedToXmlMapping(Element xmlMapping, String prefix, + MetadataTools.ColumnNameIterator columnNameIterator, + IdMappingData relatedIdMapping) { Element properties = (Element) relatedIdMapping.getXmlRelationMapping().clone(); - MetadataTools.prefixNamesInPropertyElement(properties, prefix, relatedValue.getColumnIterator(), true); + MetadataTools.prefixNamesInPropertyElement(properties, prefix, columnNameIterator, true); for (Element idProperty : (java.util.List) properties.elements()) { xmlMapping.add((Element) idProperty.clone()); } @@ -131,10 +148,9 @@ private String getMiddleTableName(Collection value, String entityName) { // We check how Hibernate maps the collection. - if (value.getKey().getTable().equals(value.getCollectionTable())) { - // The table of the element is the same as the collection table - this must be a @JoinColumn+ at OneToMany - // mapping. Generating the table name ourselves, as Hibernate doesn't use a middle table for mapping - // this relation. + if (value.getElement() instanceof OneToMany && !value.isInverse()) { + // This must be a @JoinColumn+ at OneToMany mapping. Generating the table name, as Hibernate doesn't use a + // middle table for mapping this relation. return StringTools.getLastComponent(entityName) + "_" + StringTools.getLastComponent(getReferencedEntityName(value.getElement())); } else { // Hibernate uses a middle table for mapping this relation, so we get it's name directly. @@ -144,21 +160,28 @@ @SuppressWarnings({"unchecked"}) private void addWithMiddleTable(String name, Collection value, CompositeMapperBuilder currentMapper, - String entityName, EntityXmlMappingData xmlMappingData) { - // Generating the name of the middle table, its schema and catalog - // TODO: add support for @VersionsJoinTable - String middleTableName = getMiddleTableName(value, entityName);//value.getCollectionTable().getName(); - String versionsMiddleTableName = mainGenerator.getVerEntCfg().getVersionsTableName(null, middleTableName); - String versionsMiddleEntityName = mainGenerator.getVerEntCfg().getVersionsEntityName(middleTableName); + String entityName, EntityXmlMappingData xmlMappingData, VersionsJoinTable joinTable) { + // Generating the name of the middle table - String schema = value.getCollectionTable().getSchema(); - String catalog = value.getCollectionTable().getCatalog(); + String versionsMiddleTableName; + String versionsMiddleEntityName; + if (!StringTools.isEmpty(joinTable.name())) { + versionsMiddleTableName = joinTable.name(); + versionsMiddleEntityName = joinTable.name(); + } else { + String middleTableName = getMiddleTableName(value, entityName); + versionsMiddleTableName = mainGenerator.getVerEntCfg().getVersionsTableName(null, middleTableName); + versionsMiddleEntityName = mainGenerator.getVerEntCfg().getVersionsEntityName(middleTableName); + } // Generating the XML mapping for the middle entity, only if the relation isn't inverse. // If the relation is inverse, will be later checked by comparing middleEntityXml with null. Element middleEntityXml; Element middleEntityXmlId; if (!value.isInverse()) { + String schema = StringTools.isEmpty(joinTable.schema()) ? value.getCollectionTable().getSchema() : joinTable.schema(); + String catalog = StringTools.isEmpty(joinTable.catalog()) ? value.getCollectionTable().getCatalog() : joinTable.catalog(); + middleEntityXml = MetadataTools.createEntity(xmlMappingData.newAdditionalMapping(), versionsMiddleEntityName, versionsMiddleTableName, schema, catalog, null); middleEntityXmlId = middleEntityXml.addElement("composite-id"); @@ -221,7 +244,9 @@ middleEntityXmlId.addAttribute("name", mainGenerator.getVerEntCfg().getOriginalIdPropName()); // Adding related-entity (in this case: the referencing's entity id) id mapping to the xml. - addRelatedToXmlMapping(middleEntityXmlId, referencingPrefixRelated, value.getKey(), referencingIdMapping); + addRelatedToXmlMapping(middleEntityXmlId, referencingPrefixRelated, + MetadataTools.getColumnNameIterator(value.getKey().getColumnIterator()), + referencingIdMapping); // Adding the revision number as a foreign key to the revision info entity to the composite id of the // middle table. @@ -235,7 +260,7 @@ // Generating the element mapping. // ****** MiddleComponentData elementComponentData = addValueToMiddleTable(value.getElement(), middleEntityXmlId, - queryGeneratorBuilder, referencedPrefix); + queryGeneratorBuilder, referencedPrefix, joinTable.inverseJoinColumns()); // ****** // Optionally, generating the index mapping. @@ -244,8 +269,8 @@ if (value instanceof IndexedCollection) { IndexedCollection indexedValue = (IndexedCollection) value; indexComponentData = addValueToMiddleTable(indexedValue.getIndex(), middleEntityXmlId, - queryGeneratorBuilder, "mapkey"); - // TODO + queryGeneratorBuilder, "mapkey", null); + // TODO: @MapKey support, @MapKeyManyToMany } else { // No index - creating a dummy mapper. indexComponentData = new MiddleComponentData(new MiddleDummyComponentMapper(), @@ -289,7 +314,7 @@ // ****** // Storing information about this relation. // ****** - + // Only if this is a relation (when there is a referenced entity). if (referencedEntityName != null) { if (value.isInverse()) { @@ -310,11 +335,13 @@ * @param queryGeneratorBuilder In case value is a relation to another entity, information about it * should be added to the given. * @param prefix Prefix for proeprty names of related entities identifiers. + * @param joinColumns Names of columns to use in the xml mapping, if this array isn't null and has any elements. * @return Data for mapping this component. */ + @SuppressWarnings({"unchecked"}) private MiddleComponentData addValueToMiddleTable(Value value, Element middleEntityXml, QueryGeneratorBuilder queryGeneratorBuilder, - String prefix) { + String prefix, JoinColumn[] joinColumns) { Type type = value.getType(); if (type instanceof ManyToOneType) { String prefixRelated = prefix + "_"; @@ -326,7 +353,11 @@ // Adding related-entity (in this case: the referenced entities id) id mapping to the xml only if the // relation isn't inverse (so when middleEntityXml is not null). if (middleEntityXml != null) { - addRelatedToXmlMapping(middleEntityXml, prefixRelated, value, referencedIdMapping); + addRelatedToXmlMapping(middleEntityXml, prefixRelated, + joinColumns != null && joinColumns.length > 0 + ? MetadataTools.getColumnNameIterator(joinColumns) + : MetadataTools.getColumnNameIterator(value.getColumnIterator()), + referencedIdMapping); } // Storing the id data of the referenced entity: original mapper, prefixed mapper and entity name. Modified: trunk/src/main/org/jboss/envers/configuration/metadata/MetadataTools.java =================================================================== --- trunk/src/main/org/jboss/envers/configuration/metadata/MetadataTools.java 2008-09-22 09:48:53 UTC (rev 158) +++ trunk/src/main/org/jboss/envers/configuration/metadata/MetadataTools.java 2008-09-22 11:26:24 UTC (rev 159) @@ -27,6 +27,7 @@ import org.jboss.envers.tools.StringTools; import org.hibernate.mapping.Column; +import javax.persistence.JoinColumn; import java.util.Iterator; /** @@ -104,8 +105,8 @@ } public static Element createSubclassEntity(Document document, String entityName, String tableName, - String schema, String catalog, String extendsEntityName, - String discriminatorValue) { + String schema, String catalog, String extendsEntityName, + String discriminatorValue) { Element class_mapping = createEntityCommon(document, "subclass", entityName, tableName, schema, catalog, discriminatorValue); @@ -137,9 +138,9 @@ addColumn(any_mapping, column.getName(), column.getLength()); } } - + @SuppressWarnings({"unchecked"}) - private static void changeNamesInColumnElement(Element element, Iterator columnIterator) { + private static void changeNamesInColumnElement(Element element, ColumnNameIterator columnNameIterator) { Iterator properties = element.elementIterator(); while (properties.hasNext()) { Element property = properties.next(); @@ -147,15 +148,15 @@ if ("column".equals(property.getName())) { Attribute nameAttr = property.attribute("name"); if (nameAttr != null) { - nameAttr.setText(columnIterator.next().getName()); + nameAttr.setText(columnNameIterator.next()); } } } } @SuppressWarnings({"unchecked"}) - public static void prefixNamesInPropertyElement(Element element, String prefix, Iterator columnIterator, - boolean changeToKey) { + public static void prefixNamesInPropertyElement(Element element, String prefix, ColumnNameIterator columnNameIterator, + boolean changeToKey) { Iterator properties = element.elementIterator(); while (properties.hasNext()) { Element property = properties.next(); @@ -166,7 +167,7 @@ nameAttr.setText(prefix + nameAttr.getText()); } - changeNamesInColumnElement(property, columnIterator); + changeNamesInColumnElement(property, columnNameIterator); if (changeToKey) { property.setName("key-property"); @@ -174,4 +175,26 @@ } } } + + /** + * An iterator over column names. + */ + public static abstract class ColumnNameIterator implements Iterator { } + + public static ColumnNameIterator getColumnNameIterator(final Iterator columnIterator) { + return new ColumnNameIterator() { + public boolean hasNext() { return columnIterator.hasNext(); } + public String next() { return columnIterator.next().getName(); } + public void remove() { columnIterator.remove(); } + }; + } + + public static ColumnNameIterator getColumnNameIterator(final JoinColumn[] joinColumns) { + return new ColumnNameIterator() { + int counter = 0; + public boolean hasNext() { return counter < joinColumns.length; } + public String next() { return joinColumns[counter++].name(); } + public void remove() { throw new UnsupportedOperationException(); } + }; + } } Modified: trunk/src/main/org/jboss/envers/configuration/metadata/PersistentClassVersioningData.java =================================================================== --- trunk/src/main/org/jboss/envers/configuration/metadata/PersistentClassVersioningData.java 2008-09-22 09:48:53 UTC (rev 158) +++ trunk/src/main/org/jboss/envers/configuration/metadata/PersistentClassVersioningData.java 2008-09-22 11:26:24 UTC (rev 159) @@ -23,6 +23,7 @@ import org.jboss.envers.ModificationStore; import org.jboss.envers.VersionsJoinTable; +import org.jboss.envers.VersionsTable; import java.util.HashMap; import java.util.Map; @@ -42,9 +43,7 @@ } public PropertyStoreInfo propertyStoreInfo; - public String versionsTableName; - public String schema; - public String catalog; + public VersionsTable versionsTable; public Map secondaryTableDictionary; public List unversionedProperties; /** Modified: trunk/src/main/org/jboss/envers/configuration/metadata/ToOneRelationMetadataGenerator.java =================================================================== --- trunk/src/main/org/jboss/envers/configuration/metadata/ToOneRelationMetadataGenerator.java 2008-09-22 09:48:53 UTC (rev 158) +++ trunk/src/main/org/jboss/envers/configuration/metadata/ToOneRelationMetadataGenerator.java 2008-09-22 11:26:24 UTC (rev 159) @@ -46,7 +46,8 @@ Element properties = (Element) idMapping.getXmlRelationMapping().clone(); properties.addAttribute("name", name); - MetadataTools.prefixNamesInPropertyElement(properties, lastPropertyPrefix, value.getColumnIterator(), false); + MetadataTools.prefixNamesInPropertyElement(properties, lastPropertyPrefix, + MetadataTools.getColumnNameIterator(value.getColumnIterator()), false); parent.add(properties); // Adding mapper for the id Modified: trunk/src/main/org/jboss/envers/configuration/metadata/VersionsMetadataGenerator.java =================================================================== --- trunk/src/main/org/jboss/envers/configuration/metadata/VersionsMetadataGenerator.java 2008-09-22 09:48:53 UTC (rev 158) +++ trunk/src/main/org/jboss/envers/configuration/metadata/VersionsMetadataGenerator.java 2008-09-22 11:26:24 UTC (rev 159) @@ -35,6 +35,7 @@ import org.jboss.envers.tools.StringTools; import org.jboss.envers.tools.HibernateVersion; import org.jboss.envers.ModificationStore; +import org.jboss.envers.VersionsJoinTable; import org.jboss.envers.tools.log.YLog; import org.jboss.envers.tools.log.YLogManager; @@ -120,7 +121,7 @@ @SuppressWarnings({"unchecked"}) void addValue(Element parent, String name, Value value, CompositeMapperBuilder currentMapper, ModificationStore store, String entityName, EntityXmlMappingData xmlMappingData, - boolean firstPass) { + VersionsJoinTable joinTable, boolean firstPass) { Type type = value.getType(); if (type instanceof ComponentType) { @@ -166,7 +167,7 @@ // only second pass if (!firstPass) { collectionMetadataGenerator.addCollection(name, (Collection) value, currentMapper, entityName, - xmlMappingData); + xmlMappingData, joinTable); } } else { String message = "Type not supported for versioning: " + type.getClass().getName() + @@ -181,16 +182,17 @@ @SuppressWarnings({"unchecked"}) private void addProperties(Element parent, Iterator properties, CompositeMapperBuilder currentMapper, - PropertyStoreInfo propertyStoreInfo, String entityName, EntityXmlMappingData xmlMappingData, - List unversionedProperties, boolean firstPass) { + PersistentClassVersioningData versioningData, String entityName, EntityXmlMappingData xmlMappingData, + boolean firstPass) { while (properties.hasNext()) { Property property = properties.next(); if (!"_identifierMapper".equals(property.getName())) { - ModificationStore store = getStoreForProperty(property, propertyStoreInfo, unversionedProperties); + ModificationStore store = getStoreForProperty(property, versioningData.propertyStoreInfo, + versioningData.unversionedProperties); if (store != null) { addValue(parent, property.getName(), property.getValue(), currentMapper, store, entityName, - xmlMappingData, firstPass); + xmlMappingData, versioningData.versionsJoinTables.get(property.getName()), firstPass); } } } @@ -214,12 +216,12 @@ versionedTableName = verEntCfg.getVersionsEntityName(originalTableName); } - String schema = versioningData.schema; + String schema = versioningData.versionsTable.schema(); if (StringTools.isEmpty(schema)) { schema = join.getTable().getSchema(); } - String catalog = versioningData.catalog; + String catalog = versioningData.versionsTable.catalog(); if (StringTools.isEmpty(catalog)) { catalog = join.getTable().getCatalog(); } @@ -234,17 +236,16 @@ } @SuppressWarnings({"unchecked"}) - private void addJoins(PersistentClass pc, CompositeMapperBuilder currentMapper, PropertyStoreInfo propertyStoreInfo, - String entityName, EntityXmlMappingData xmlMappingData, List unversionedProperties, - boolean firstPass) { + private void addJoins(PersistentClass pc, CompositeMapperBuilder currentMapper, PersistentClassVersioningData versioningData, + String entityName, EntityXmlMappingData xmlMappingData,boolean firstPass) { Iterator joins = pc.getJoinIterator(); while (joins.hasNext()) { Join join = joins.next(); Element joinElement = entitiesJoins.get(entityName).get(join); - addProperties(joinElement, join.getPropertyIterator(), currentMapper, propertyStoreInfo, entityName, - xmlMappingData, unversionedProperties, firstPass); + addProperties(joinElement, join.getPropertyIterator(), currentMapper, versioningData, entityName, + xmlMappingData, firstPass); } } @@ -264,12 +265,12 @@ @SuppressWarnings({"unchecked"}) public void generateFirstPass(PersistentClass pc, PersistentClassVersioningData versioningData, EntityXmlMappingData xmlMappingData) { - String schema = versioningData.schema; + String schema = versioningData.versionsTable.schema(); if (StringTools.isEmpty(schema)) { schema = pc.getTable().getSchema(); } - String catalog = versioningData.catalog; + String catalog = versioningData.versionsTable.catalog(); if (StringTools.isEmpty(catalog)) { catalog = pc.getTable().getCatalog(); } @@ -335,13 +336,12 @@ // Mapping unjoined properties addProperties(class_mapping, (Iterator) pc.getUnjoinedPropertyIterator(), propertyMapper, - versioningData.propertyStoreInfo, pc.getEntityName(), xmlMappingData, versioningData.unversionedProperties, + versioningData, pc.getEntityName(), xmlMappingData, true); // Creating and mapping joins (first pass) createJoins(pc, class_mapping, versioningData); - addJoins(pc, propertyMapper, versioningData.propertyStoreInfo, pc.getEntityName(), xmlMappingData, - versioningData.unversionedProperties, true); + addJoins(pc, propertyMapper, versioningData, pc.getEntityName(), xmlMappingData, true); // Storing the generated configuration EntityConfiguration entityCfg = new EntityConfiguration(entityName, versionsEntityName, idMapper, @@ -363,12 +363,10 @@ } addProperties(parent, (Iterator) pc.getUnjoinedPropertyIterator(), - propertyMapper, versioningData.propertyStoreInfo, entityName, xmlMappingData, - versioningData.unversionedProperties, false); + propertyMapper, versioningData, entityName, xmlMappingData, false); // Mapping joins (second pass) - addJoins(pc, propertyMapper, versioningData.propertyStoreInfo, entityName, xmlMappingData, - versioningData.unversionedProperties, false); + addJoins(pc, propertyMapper, versioningData, entityName, xmlMappingData, false); } public Map getEntitiesConfigurations() { Copied: trunk/src/test/org/jboss/envers/test/entities/onetomany/detached/SetJoinColumnRefCollEntity.java (from rev 148, trunk/src/test/org/jboss/envers/test/entities/onetomany/detached/SetRefCollEntity.java) =================================================================== --- trunk/src/test/org/jboss/envers/test/entities/onetomany/detached/SetJoinColumnRefCollEntity.java (rev 0) +++ trunk/src/test/org/jboss/envers/test/entities/onetomany/detached/SetJoinColumnRefCollEntity.java 2008-09-22 11:26:24 UTC (rev 159) @@ -0,0 +1,87 @@ +package org.jboss.envers.test.entities.onetomany.detached; + +import org.jboss.envers.Versioned; +import org.jboss.envers.test.entities.StrTestEntity; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.OneToMany; +import javax.persistence.JoinColumn; +import java.util.Set; + +/** + * A detached relation to another entity, with a @OneToMany+ at JoinColumn mapping. + * @author Adam Warski (adam at warski dot org) + */ + at Entity +public class SetJoinColumnRefCollEntity { + @Id + private Integer id; + + @Versioned + private String data; + + @Versioned + @OneToMany + @JoinColumn(name = "SJCR_ID") + private Set collection; + + public SetJoinColumnRefCollEntity() { + } + + public SetJoinColumnRefCollEntity(Integer id, String data) { + this.id = id; + this.data = data; + } + + public SetJoinColumnRefCollEntity(String data) { + this.data = data; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getData() { + return data; + } + + public void setData(String data) { + this.data = data; + } + + public Set getCollection() { + return collection; + } + + public void setCollection(Set collection) { + this.collection = collection; + } + + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof SetJoinColumnRefCollEntity)) return false; + + SetJoinColumnRefCollEntity that = (SetJoinColumnRefCollEntity) o; + + if (data != null ? !data.equals(that.data) : that.data != null) return false; + if (id != null ? !id.equals(that.id) : that.id != null) return false; + + return true; + } + + public int hashCode() { + int result; + result = (id != null ? id.hashCode() : 0); + result = 31 * result + (data != null ? data.hashCode() : 0); + return result; + } + + public String toString() { + return "SetJoinColumnRefCollEntity(id = " + id + ", data = " + data + ")"; + } +} \ No newline at end of file Property changes on: trunk/src/test/org/jboss/envers/test/entities/onetomany/detached/SetJoinColumnRefCollEntity.java ___________________________________________________________________ Name: svn:mergeinfo + Modified: trunk/src/test/org/jboss/envers/test/integration/naming/OneToManyUnidirectionalNaming.java =================================================================== --- trunk/src/test/org/jboss/envers/test/integration/naming/OneToManyUnidirectionalNaming.java 2008-09-22 09:48:53 UTC (rev 158) +++ trunk/src/test/org/jboss/envers/test/integration/naming/OneToManyUnidirectionalNaming.java 2008-09-22 11:26:24 UTC (rev 159) @@ -75,11 +75,10 @@ assert "data1".equals(rev2.getData()); } - private final static String MIDDLE_VERSIONS_ENTITY_NAME = - "UNI_NAMING_TEST_versions"; + private final static String MIDDLE_VERSIONS_ENTITY_NAME = "UNI_NAMING_TEST_versions"; @Test public void testTableName() { - assert "UNI_NAMING_TEST_versions".equals( + assert MIDDLE_VERSIONS_ENTITY_NAME.equals( getCfg().getClassMapping(MIDDLE_VERSIONS_ENTITY_NAME).getTable().getName()); } Copied: trunk/src/test/org/jboss/envers/test/integration/naming/VersionsJoinTableNaming.java (from rev 152, trunk/src/test/org/jboss/envers/test/integration/naming/OneToManyUnidirectionalNaming.java) =================================================================== --- trunk/src/test/org/jboss/envers/test/integration/naming/VersionsJoinTableNaming.java (rev 0) +++ trunk/src/test/org/jboss/envers/test/integration/naming/VersionsJoinTableNaming.java 2008-09-22 11:26:24 UTC (rev 159) @@ -0,0 +1,108 @@ +package org.jboss.envers.test.integration.naming; + +import org.jboss.envers.test.integration.AbstractEntityTest; +import org.jboss.envers.test.entities.StrTestEntity; +import org.jboss.envers.test.tools.TestTools; +import org.hibernate.ejb.Ejb3Configuration; +import org.hibernate.mapping.Column; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +import javax.persistence.EntityManager; +import java.util.Arrays; +import java.util.Iterator; +import java.util.HashSet; + +/** + * @author Adam Warski (adam at warski dot org) + */ +public class VersionsJoinTableNaming extends AbstractEntityTest { + private Integer uni1_id; + private Integer str1_id; + + public void configure(Ejb3Configuration cfg) { + cfg.addAnnotatedClass(VersionsJoinTableTestEntity.class); + cfg.addAnnotatedClass(StrTestEntity.class); + } + + @BeforeClass(dependsOnMethods = "init") + public void initData() { + VersionsJoinTableTestEntity uni1 = new VersionsJoinTableTestEntity(1, "data1"); + StrTestEntity str1 = new StrTestEntity("str1"); + + // Revision 1 + EntityManager em = getEntityManager(); + em.getTransaction().begin(); + + uni1.setCollection(new HashSet()); + em.persist(uni1); + em.persist(str1); + + em.getTransaction().commit(); + + // Revision 2 + em.getTransaction().begin(); + + uni1 = em.find(VersionsJoinTableTestEntity.class, uni1.getId()); + str1 = em.find(StrTestEntity.class, str1.getId()); + uni1.getCollection().add(str1); + + em.getTransaction().commit(); + + // + + uni1_id = uni1.getId(); + str1_id = str1.getId(); + } + + @Test + public void testRevisionsCounts() { + assert Arrays.asList(1, 2).equals(getVersionsReader().getRevisions(VersionsJoinTableTestEntity.class, uni1_id)); + assert Arrays.asList(1).equals(getVersionsReader().getRevisions(StrTestEntity.class, str1_id)); + } + + @Test + public void testHistoryOfUniId1() { + StrTestEntity str1 = getEntityManager().find(StrTestEntity.class, str1_id); + + VersionsJoinTableTestEntity rev1 = getVersionsReader().find(VersionsJoinTableTestEntity.class, uni1_id, 1); + VersionsJoinTableTestEntity rev2 = getVersionsReader().find(VersionsJoinTableTestEntity.class, uni1_id, 2); + + assert rev1.getCollection().equals(TestTools.makeSet()); + assert rev2.getCollection().equals(TestTools.makeSet(str1)); + + assert "data1".equals(rev1.getData()); + assert "data1".equals(rev2.getData()); + } + + private final static String MIDDLE_VERSIONS_ENTITY_NAME = "VERSIONS_JOIN_TABLE_TEST"; + + @Test + public void testTableName() { + assert MIDDLE_VERSIONS_ENTITY_NAME.equals( + getCfg().getClassMapping(MIDDLE_VERSIONS_ENTITY_NAME).getTable().getName()); + } + + @SuppressWarnings({"unchecked"}) + @Test + public void testJoinColumnName() { + Iterator columns = + getCfg().getClassMapping(MIDDLE_VERSIONS_ENTITY_NAME).getTable().getColumnIterator(); + + boolean id1Found = false; + boolean id2Found = false; + + while (columns.hasNext()) { + Column column = columns.next(); + if ("VJT_ID".equals(column.getName())) { + id1Found = true; + } + + if ("STR_ID".equals(column.getName())) { + id2Found = true; + } + } + + assert id1Found && id2Found; + } +} \ No newline at end of file Property changes on: trunk/src/test/org/jboss/envers/test/integration/naming/VersionsJoinTableNaming.java ___________________________________________________________________ Name: svn:mergeinfo + Copied: trunk/src/test/org/jboss/envers/test/integration/naming/VersionsJoinTableTestEntity.java (from rev 148, trunk/src/test/org/jboss/envers/test/integration/naming/DetachedNamingTestEntity.java) =================================================================== --- trunk/src/test/org/jboss/envers/test/integration/naming/VersionsJoinTableTestEntity.java (rev 0) +++ trunk/src/test/org/jboss/envers/test/integration/naming/VersionsJoinTableTestEntity.java 2008-09-22 11:26:24 UTC (rev 159) @@ -0,0 +1,85 @@ +package org.jboss.envers.test.integration.naming; + +import org.jboss.envers.Versioned; +import org.jboss.envers.VersionsJoinTable; +import org.jboss.envers.test.entities.StrTestEntity; + +import javax.persistence.*; +import java.util.Set; + +/** + * @author Adam Warski (adam at warski dot org) + */ + at Entity +public class VersionsJoinTableTestEntity { + @Id + private Integer id; + + @Versioned + private String data; + + @Versioned + @OneToMany + @JoinColumn(name = "VJT_ID") + @VersionsJoinTable(name = "VERSIONS_JOIN_TABLE_TEST", inverseJoinColumns = @JoinColumn(name = "STR_ID")) + private Set collection; + + public VersionsJoinTableTestEntity() { + } + + public VersionsJoinTableTestEntity(Integer id, String data) { + this.id = id; + this.data = data; + } + + public VersionsJoinTableTestEntity(String data) { + this.data = data; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getData() { + return data; + } + + public void setData(String data) { + this.data = data; + } + + public Set getCollection() { + return collection; + } + + public void setCollection(Set collection) { + this.collection = collection; + } + + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof VersionsJoinTableTestEntity)) return false; + + VersionsJoinTableTestEntity that = (VersionsJoinTableTestEntity) o; + + if (data != null ? !data.equals(that.data) : that.data != null) return false; + if (id != null ? !id.equals(that.id) : that.id != null) return false; + + return true; + } + + public int hashCode() { + int result; + result = (id != null ? id.hashCode() : 0); + result = 31 * result + (data != null ? data.hashCode() : 0); + return result; + } + + public String toString() { + return "VersionsJoinTableTestEntity(id = " + id + ", data = " + data + ")"; + } +} \ No newline at end of file Property changes on: trunk/src/test/org/jboss/envers/test/integration/naming/VersionsJoinTableTestEntity.java ___________________________________________________________________ Name: svn:mergeinfo + Copied: trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicJoinColumnSet.java (from rev 156, trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicDetachedSet.java) =================================================================== --- trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicJoinColumnSet.java (rev 0) +++ trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicJoinColumnSet.java 2008-09-22 11:26:24 UTC (rev 159) @@ -0,0 +1,115 @@ +package org.jboss.envers.test.integration.onetomany.detached; + +import org.jboss.envers.test.integration.AbstractEntityTest; +import org.jboss.envers.test.entities.StrTestEntity; +import org.jboss.envers.test.entities.onetomany.detached.SetJoinColumnRefCollEntity; +import org.jboss.envers.test.tools.TestTools; +import org.hibernate.ejb.Ejb3Configuration; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +import javax.persistence.EntityManager; +import java.util.Arrays; +import java.util.HashSet; + +/** + * @author Adam Warski (adam at warski dot org) + */ +public class BasicJoinColumnSet extends AbstractEntityTest { + private Integer str1_id; + private Integer str2_id; + + private Integer coll1_id; + + public void configure(Ejb3Configuration cfg) { + cfg.addAnnotatedClass(StrTestEntity.class); + cfg.addAnnotatedClass(SetJoinColumnRefCollEntity.class); + } + + @BeforeClass(dependsOnMethods = "init") + public void initData() { + EntityManager em = getEntityManager(); + + StrTestEntity str1 = new StrTestEntity("str1"); + StrTestEntity str2 = new StrTestEntity("str2"); + + SetJoinColumnRefCollEntity coll1 = new SetJoinColumnRefCollEntity(3, "coll1"); + + // Revision 1 + em.getTransaction().begin(); + + em.persist(str1); + em.persist(str2); + + coll1.setCollection(new HashSet()); + coll1.getCollection().add(str1); + em.persist(coll1); + + em.getTransaction().commit(); + + // Revision 2 + em.getTransaction().begin(); + + str2 = em.find(StrTestEntity.class, str2.getId()); + coll1 = em.find(SetJoinColumnRefCollEntity.class, coll1.getId()); + + coll1.getCollection().add(str2); + + em.getTransaction().commit(); + + // Revision 3 + em.getTransaction().begin(); + + str1 = em.find(StrTestEntity.class, str1.getId()); + coll1 = em.find(SetJoinColumnRefCollEntity.class, coll1.getId()); + + coll1.getCollection().remove(str1); + + em.getTransaction().commit(); + + // Revision 4 + em.getTransaction().begin(); + + coll1 = em.find(SetJoinColumnRefCollEntity.class, coll1.getId()); + + coll1.getCollection().clear(); + + em.getTransaction().commit(); + + // + + str1_id = str1.getId(); + str2_id = str2.getId(); + + coll1_id = coll1.getId(); + } + + @Test + public void testRevisionsCounts() { + assert Arrays.asList(1, 2, 3, 4).equals(getVersionsReader().getRevisions(SetJoinColumnRefCollEntity.class, coll1_id)); + + assert Arrays.asList(1).equals(getVersionsReader().getRevisions(StrTestEntity.class, str1_id)); + assert Arrays.asList(1).equals(getVersionsReader().getRevisions(StrTestEntity.class, str2_id)); + } + + @Test + public void testHistoryOfColl1() { + StrTestEntity str1 = getEntityManager().find(StrTestEntity.class, str1_id); + StrTestEntity str2 = getEntityManager().find(StrTestEntity.class, str2_id); + + SetJoinColumnRefCollEntity rev1 = getVersionsReader().find(SetJoinColumnRefCollEntity.class, coll1_id, 1); + SetJoinColumnRefCollEntity rev2 = getVersionsReader().find(SetJoinColumnRefCollEntity.class, coll1_id, 2); + SetJoinColumnRefCollEntity rev3 = getVersionsReader().find(SetJoinColumnRefCollEntity.class, coll1_id, 3); + SetJoinColumnRefCollEntity rev4 = getVersionsReader().find(SetJoinColumnRefCollEntity.class, coll1_id, 4); + + assert rev1.getCollection().equals(TestTools.makeSet(str1)); + assert rev2.getCollection().equals(TestTools.makeSet(str1, str2)); + assert rev3.getCollection().equals(TestTools.makeSet(str2)); + assert rev4.getCollection().equals(TestTools.makeSet()); + + assert "coll1".equals(rev1.getData()); + assert "coll1".equals(rev2.getData()); + assert "coll1".equals(rev3.getData()); + assert "coll1".equals(rev4.getData()); + } +} \ No newline at end of file Property changes on: trunk/src/test/org/jboss/envers/test/integration/onetomany/detached/BasicJoinColumnSet.java ___________________________________________________________________ Name: svn:mergeinfo + From jboss-envers-commits at lists.jboss.org Mon Sep 22 11:33:45 2008 From: jboss-envers-commits at lists.jboss.org (jboss-envers-commits at lists.jboss.org) Date: Mon, 22 Sep 2008 11:33:45 -0400 Subject: [jboss-envers-commits] JBoss Envers SVN: r160 - in trunk/src/main/org/jboss/envers: entities/mapper/relation and 1 other directory. Message-ID: Author: adamw Date: 2008-09-22 11:33:44 -0400 (Mon, 22 Sep 2008) New Revision: 160 Removed: trunk/src/main/org/jboss/envers/entities/mapper/relation/AbstractOneToManyMapper.java Modified: trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java trunk/src/main/org/jboss/envers/configuration/metadata/VersionsMetadataGenerator.java trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyAttachedMapper.java Log: ENVERS-44: restructuring the collections mapper, making it statefull Modified: trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java =================================================================== --- trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java 2008-09-22 11:26:24 UTC (rev 159) +++ trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java 2008-09-22 15:33:44 UTC (rev 160) @@ -28,113 +28,104 @@ import java.lang.annotation.Annotation; /** - * Generates metadata for collection-valued properties. + * Generates metadata for a collection-valued property. * @author Adam Warski (adam at warski dot org) */ public final class CollectionMetadataGenerator { private final VersionsMetadataGenerator mainGenerator; + private final String propertyName; + private final Collection propertyValue; + private final CompositeMapperBuilder currentMapper; + private final String referencingEntityName; + private final EntityXmlMappingData xmlMappingData; + private final VersionsJoinTable joinTable; - CollectionMetadataGenerator(VersionsMetadataGenerator versionsMetadataGenerator) { - mainGenerator = versionsMetadataGenerator; - } + private final EntityConfiguration referencingEntityConfiguration; + /** + * Null if this collection isn't a relation to another entity. + */ + private final String referencedEntityName; - void addCollection(String name, Collection value, CompositeMapperBuilder currentMapper, - String entityName, EntityXmlMappingData xmlMappingData, - VersionsJoinTable joinTable) { - Type type = value.getType(); + /** + * @param mainGenerator Main generator, giving access to configuration and the basic mapper. + * @param propertyName Name of the property that references the collection in the referencing entity. + * @param propertyValue Value of the collection, as mapped by Hibernate. + * @param currentMapper Mapper, to which the appropriate {@link org.jboss.envers.entities.mapper.PropertyMapper} + * will be added. + * @param referencingEntityName Name of the entity that owns this collection. + * @param xmlMappingData In case this collection requires a middle table, additional mapping documents will + * be created using this object. + * @param joinTable User data for the middle (join) table. null if the user didn't specify it. + */ + public CollectionMetadataGenerator(VersionsMetadataGenerator mainGenerator, String propertyName, + Collection propertyValue, CompositeMapperBuilder currentMapper, + String referencingEntityName, EntityXmlMappingData xmlMappingData, + VersionsJoinTable joinTable) { + this.mainGenerator = mainGenerator; + this.propertyName = propertyName; + this.propertyValue = propertyValue; + this.currentMapper = currentMapper; + this.referencingEntityName = referencingEntityName; + this.xmlMappingData = xmlMappingData; + this.joinTable = joinTable == null ? getDefaultVersionsJoinTable() : joinTable; - if ((type instanceof BagType || type instanceof SetType) && - (value.getElement() instanceof OneToMany) && (value.isInverse())) { - addOneToManyAttached(name, value, currentMapper, entityName); - } else { - addWithMiddleTable(name, value, currentMapper, entityName, xmlMappingData, - joinTable == null ? getDefaultVersionsJoinTable() : joinTable); + referencingEntityConfiguration = mainGenerator.getEntitiesConfigurations().get(referencingEntityName); + if (referencingEntityConfiguration == null) { + throw new MappingException("Unable to read versioning configuration for " + referencingEntityName + "!"); } - } - private VersionsJoinTable getDefaultVersionsJoinTable() { - return new VersionsJoinTable() { - public String name() { return ""; } - public String schema() { return ""; } - public String catalog() { return ""; } - public JoinColumn[] inverseJoinColumns() { return new JoinColumn[0]; } - public Class annotationType() { return this.getClass(); } - }; + referencedEntityName = getReferencedEntityName(propertyValue.getElement()); } - @SuppressWarnings({"unchecked"}) - private String getMappedBy(Collection collectionValue) { - Iterator assocClassProps = - ((OneToMany) collectionValue.getElement()).getAssociatedClass().getPropertyIterator(); - - while (assocClassProps.hasNext()) { - Property property = assocClassProps.next(); - - if (Tools.iteratorsContentEqual(property.getValue().getColumnIterator(), - collectionValue.getKey().getColumnIterator())) { - return property.getName(); - } + private String getReferencedEntityName(Value value) { + if (value instanceof ToOne) { + return ((ToOne) value).getReferencedEntityName(); + } else if (value instanceof OneToMany) { + return ((OneToMany) value).getReferencedEntityName(); + } else { + return null; } - - return null; } - @SuppressWarnings({"unchecked"}) - private String getMappedBy(Table collectionTable, PersistentClass referencedClass) { - Iterator properties = referencedClass.getPropertyIterator(); - while (properties.hasNext()) { - Property property = properties.next(); - if (property.getValue() instanceof Collection) { - // The equality is intentional. We want to find a collection property with the same collection table. - //noinspection ObjectEquality - if (((Collection) property.getValue()).getCollectionTable() == collectionTable) { - return property.getName(); - } - } + void addCollection() { + Type type = propertyValue.getType(); + + if ((type instanceof BagType || type instanceof SetType) && + (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). + addWithMiddleTable(); } - - return null; } @SuppressWarnings({"unchecked"}) - private void addOneToManyAttached(String name, Collection value, CompositeMapperBuilder mapper, String entityName) { - String owningReferencePropertyName = getMappedBy(value); - if (owningReferencePropertyName == null) { - throw new MappingException("Unable to read the mapped by attribute for " + name + " in " + entityName + "!"); - } + private void addOneToManyAttached() { + String owningReferencePropertyName = getMappedBy(propertyValue); - EntityConfiguration configuration = mainGenerator.getEntitiesConfigurations().get(entityName); - if (configuration == null) { - throw new MappingException("Unable to read versioning configuration for " + entityName + "!"); - } - - IdMappingData referencingIdMapping = configuration.getIdMappingData(); - - String owningEntityName = ((OneToMany) value.getElement()).getReferencedEntityName(); + IdMappingData referencingIdMapping = referencingEntityConfiguration.getIdMappingData(); String lastPropertyPrefix = owningReferencePropertyName + "_"; // Generating the id mapper for the relation IdMapper ownedIdMapper = referencingIdMapping.getIdMapper().prefixMappedProperties(lastPropertyPrefix); // Storing information about this relation - mainGenerator.getEntitiesConfigurations().get(entityName).addToManyNotOwningRelation(name, owningReferencePropertyName, - owningEntityName, ownedIdMapper); + referencingEntityConfiguration.addToManyNotOwningRelation(propertyName, owningReferencePropertyName, + referencedEntityName, ownedIdMapper); // Adding mapper for the id - mapper.addComposite(name, new OneToManyAttachedMapper(owningReferencePropertyName, owningEntityName, - name)); + currentMapper.addComposite(propertyName, new OneToManyAttachedMapper(referencedEntityName, propertyName, + owningReferencePropertyName)); } - private String getReferencedEntityName(Value value) { - if (value instanceof ToOne) { - return ((ToOne) value).getReferencedEntityName(); - } else if (value instanceof OneToMany) { - return ((OneToMany) value).getReferencedEntityName(); - } else { - return null; - } - } - + /** + * Adds mapping of the id of a related entity to the given xml mapping, prefixing the id with the given prefix. + * @param xmlMapping Mapping, to which to add the xml. + * @param prefix Prefix for the names of properties which will be prepended to properties that form the id. + * @param columnNameIterator Iterator over the column names that will be used for properties that form the id. + * @param relatedIdMapping Id mapping data of the related entity. + */ @SuppressWarnings({"unchecked"}) private void addRelatedToXmlMapping(Element xmlMapping, String prefix, MetadataTools.ColumnNameIterator columnNameIterator, @@ -159,17 +150,15 @@ } @SuppressWarnings({"unchecked"}) - private void addWithMiddleTable(String name, Collection value, CompositeMapperBuilder currentMapper, - String entityName, EntityXmlMappingData xmlMappingData, VersionsJoinTable joinTable) { + private void addWithMiddleTable() { // Generating the name of the middle table - String versionsMiddleTableName; String versionsMiddleEntityName; if (!StringTools.isEmpty(joinTable.name())) { versionsMiddleTableName = joinTable.name(); versionsMiddleEntityName = joinTable.name(); } else { - String middleTableName = getMiddleTableName(value, entityName); + String middleTableName = getMiddleTableName(propertyValue, referencingEntityName); versionsMiddleTableName = mainGenerator.getVerEntCfg().getVersionsTableName(null, middleTableName); versionsMiddleEntityName = mainGenerator.getVerEntCfg().getVersionsEntityName(middleTableName); } @@ -177,32 +166,18 @@ // Generating the XML mapping for the middle entity, only if the relation isn't inverse. // If the relation is inverse, will be later checked by comparing middleEntityXml with null. Element middleEntityXml; - Element middleEntityXmlId; - if (!value.isInverse()) { - String schema = StringTools.isEmpty(joinTable.schema()) ? value.getCollectionTable().getSchema() : joinTable.schema(); - String catalog = StringTools.isEmpty(joinTable.catalog()) ? value.getCollectionTable().getCatalog() : joinTable.catalog(); - - middleEntityXml = MetadataTools.createEntity(xmlMappingData.newAdditionalMapping(), - versionsMiddleEntityName, versionsMiddleTableName, schema, catalog, null); - middleEntityXmlId = middleEntityXml.addElement("composite-id"); + if (!propertyValue.isInverse()) { + middleEntityXml = createMiddleEntityXml(versionsMiddleTableName, versionsMiddleEntityName); } else { middleEntityXml = null; - middleEntityXmlId = null; } // ****** // Generating the mapping for the referencing entity (it must be an entity). // ****** - EntityConfiguration referencingConfiguration = mainGenerator.getEntitiesConfigurations().get(entityName); - if (referencingConfiguration == null) { - throw new MappingException("Unable to read versioning configuration for " + entityName + "!"); - } - // Getting the id-mapping data of the referencing entity (the entity that "owns" this collection). - IdMappingData referencingIdMapping = referencingConfiguration.getIdMappingData(); + IdMappingData referencingIdMapping = referencingEntityConfiguration.getIdMappingData(); - // Null if this collection doesn't reference an entity. - String referencedEntityName = getReferencedEntityName(value.getElement()); // Only valid for an inverse relation; null otherwise. String mappedBy; @@ -210,28 +185,25 @@ String referencingPrefixRelated; String referencedPrefix; - if (value.isInverse()) { + if (propertyValue.isInverse()) { // If the relation is inverse, then referencedEntityName is not null. - mappedBy = getMappedBy(value.getCollectionTable(), mainGenerator.getCfg().getClassMapping(referencedEntityName)); - if (mappedBy == null) { - throw new MappingException("Unable to read the mapped by attribute for " + name); - } + mappedBy = getMappedBy(propertyValue.getCollectionTable(), mainGenerator.getCfg().getClassMapping(referencedEntityName)); referencingPrefixRelated = mappedBy + "_"; referencedPrefix = referencedEntityName == null ? "element" : StringTools.getLastComponent(referencedEntityName); } else { mappedBy = null; - referencingPrefixRelated = StringTools.getLastComponent(entityName) + "_"; - referencedPrefix = referencedEntityName == null ? "element" : name; + referencingPrefixRelated = StringTools.getLastComponent(referencingEntityName) + "_"; + referencedPrefix = referencedEntityName == null ? "element" : propertyName; } // 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), - entityName, - mainGenerator.getVerEntCfg().getVersionsEntityName(entityName)); + referencingEntityName, + mainGenerator.getVerEntCfg().getVersionsEntityName(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 @@ -241,34 +213,25 @@ // Adding the XML mapping for the referencing entity, if the relation isn't inverse. if (middleEntityXml != null) { - middleEntityXmlId.addAttribute("name", mainGenerator.getVerEntCfg().getOriginalIdPropName()); - // Adding related-entity (in this case: the referencing's entity id) id mapping to the xml. - addRelatedToXmlMapping(middleEntityXmlId, referencingPrefixRelated, - MetadataTools.getColumnNameIterator(value.getKey().getColumnIterator()), + addRelatedToXmlMapping(middleEntityXml, referencingPrefixRelated, + MetadataTools.getColumnNameIterator(propertyValue.getKey().getColumnIterator()), referencingIdMapping); - - // Adding the revision number as a foreign key to the revision info entity to the composite id of the - // middle table. - mainGenerator.addRevisionInfoRelation(middleEntityXmlId); - - // Adding the revision type property to the entity xml. - mainGenerator.addRevisionType(middleEntityXml); } // ****** // Generating the element mapping. // ****** - MiddleComponentData elementComponentData = addValueToMiddleTable(value.getElement(), middleEntityXmlId, + MiddleComponentData elementComponentData = addValueToMiddleTable(propertyValue.getElement(), middleEntityXml, queryGeneratorBuilder, referencedPrefix, joinTable.inverseJoinColumns()); // ****** // Optionally, generating the index mapping. // ****** MiddleComponentData indexComponentData; - if (value instanceof IndexedCollection) { - IndexedCollection indexedValue = (IndexedCollection) value; - indexComponentData = addValueToMiddleTable(indexedValue.getIndex(), middleEntityXmlId, + if (propertyValue instanceof IndexedCollection) { + IndexedCollection indexedValue = (IndexedCollection) propertyValue; + indexComponentData = addValueToMiddleTable(indexedValue.getIndex(), middleEntityXml, queryGeneratorBuilder, "mapkey", null); // TODO: @MapKey support, @MapKeyManyToMany } else { @@ -280,58 +243,27 @@ // ****** // Generating the property mapper. // ****** - // Building the query generator. MiddleTableQueryGenerator queryGenerator = queryGeneratorBuilder.build(elementComponentData, indexComponentData); // Creating common data CommonCollectionMapperData commonCollectionMapperData = new CommonCollectionMapperData( - mainGenerator.getVerEntCfg(), versionsMiddleEntityName, name, referencingIdData, queryGenerator); + mainGenerator.getVerEntCfg(), versionsMiddleEntityName, propertyName, referencingIdData, queryGenerator); // Checking the type of the collection and adding an appropriate mapper. - Type type = value.getType(); - if (type instanceof SortedSetType) { - currentMapper.addComposite(name, new BasicCollectionMapper(commonCollectionMapperData, - TreeSet.class, SortedSetProxy.class, elementComponentData)); - } else if (type instanceof SetType) { - currentMapper.addComposite(name, new BasicCollectionMapper(commonCollectionMapperData, - HashSet.class, SetProxy.class, elementComponentData)); - } else if (type instanceof SortedMapType) { - // Indexed collection, so indexComponentData is not null. - currentMapper.addComposite(name, new MapCollectionMapper(commonCollectionMapperData, - TreeMap.class, SortedMapProxy.class, elementComponentData, indexComponentData)); - } else if (type instanceof MapType) { - // Indexed collection, so indexComponentData is not null. - currentMapper.addComposite(name, new MapCollectionMapper(commonCollectionMapperData, - HashMap.class, MapProxy.class, elementComponentData, indexComponentData)); - } else if (type instanceof BagType) { - currentMapper.addComposite(name, new BasicCollectionMapper(commonCollectionMapperData, - ArrayList.class, ListProxy.class, elementComponentData)); - } else { - throw new RuntimeException(); - } + addMapper(commonCollectionMapperData, elementComponentData, indexComponentData); // ****** // Storing information about this relation. // ****** - - // Only if this is a relation (when there is a referenced entity). - if (referencedEntityName != null) { - if (value.isInverse()) { - mainGenerator.getEntitiesConfigurations().get(entityName).addToManyMiddleNotOwningRelation(name, mappedBy, - referencedEntityName); - } else { - mainGenerator.getEntitiesConfigurations().get(entityName).addToManyMiddleRelation(name, - referencedEntityName); - } - } + storeMiddleEntityRelationInformation(mappedBy); } /** * * @param value Value, which should be mapped to the middle-table, either as a relation to another entity, * or as a simple value. - * @param middleEntityXml If not null, xml mapping for this value is added to this element. + * @param xmlMapping If not null, xml mapping for this value is added to this element. * @param queryGeneratorBuilder In case value is a relation to another entity, information about it * should be added to the given. * @param prefix Prefix for proeprty names of related entities identifiers. @@ -339,7 +271,7 @@ * @return Data for mapping this component. */ @SuppressWarnings({"unchecked"}) - private MiddleComponentData addValueToMiddleTable(Value value, Element middleEntityXml, + private MiddleComponentData addValueToMiddleTable(Value value, Element xmlMapping, QueryGeneratorBuilder queryGeneratorBuilder, String prefix, JoinColumn[] joinColumns) { Type type = value.getType(); @@ -351,9 +283,9 @@ referencedEntityName).getIdMappingData(); // Adding related-entity (in this case: the referenced entities id) id mapping to the xml only if the - // relation isn't inverse (so when middleEntityXml is not null). - if (middleEntityXml != null) { - addRelatedToXmlMapping(middleEntityXml, prefixRelated, + // relation isn't inverse (so when xmlMapping is not null). + if (xmlMapping != null) { + addRelatedToXmlMapping(xmlMapping, prefixRelated, joinColumns != null && joinColumns.length > 0 ? MetadataTools.getColumnNameIterator(joinColumns) : MetadataTools.getColumnNameIterator(value.getColumnIterator()), @@ -374,7 +306,7 @@ queryGeneratorBuilder.getCurrentIndex()); } else if (type instanceof ImmutableType || type instanceof MutableType) { // TODO: add support for enums, components, custom types - mainGenerator.getBasicMetadataGenerator().addSimpleValue(middleEntityXml, prefix, value, null, ModificationStore.FULL, true); + mainGenerator.getBasicMetadataGenerator().addSimpleValue(xmlMapping, prefix, value, null, ModificationStore.FULL, true); // Simple values are always stored in the first entity read by the query generator. return new MiddleComponentData(new MiddleSimpleComponentMapper(mainGenerator.getVerEntCfg(), prefix), 0); @@ -383,4 +315,107 @@ throw new RuntimeException(); } } + + private void addMapper(CommonCollectionMapperData commonCollectionMapperData, MiddleComponentData elementComponentData, + MiddleComponentData indexComponentData) { + Type type = propertyValue.getType(); + if (type instanceof SortedSetType) { + currentMapper.addComposite(propertyName, new BasicCollectionMapper(commonCollectionMapperData, + TreeSet.class, SortedSetProxy.class, elementComponentData)); + } else if (type instanceof SetType) { + currentMapper.addComposite(propertyName, new BasicCollectionMapper(commonCollectionMapperData, + HashSet.class, SetProxy.class, elementComponentData)); + } else if (type instanceof SortedMapType) { + // Indexed collection, so indexComponentData is not null. + currentMapper.addComposite(propertyName, new MapCollectionMapper(commonCollectionMapperData, + TreeMap.class, SortedMapProxy.class, elementComponentData, indexComponentData)); + } else if (type instanceof MapType) { + // Indexed collection, so indexComponentData is not null. + currentMapper.addComposite(propertyName, new MapCollectionMapper(commonCollectionMapperData, + HashMap.class, MapProxy.class, elementComponentData, indexComponentData)); + } else if (type instanceof BagType) { + currentMapper.addComposite(propertyName, new BasicCollectionMapper(commonCollectionMapperData, + ArrayList.class, ListProxy.class, elementComponentData)); + } else { + mainGenerator.throwUnsupportedTypeException(type, referencingEntityName, propertyName); + } + } + + private void storeMiddleEntityRelationInformation(String mappedBy) { + // Only if this is a relation (when there is a referenced entity). + if (referencedEntityName != null) { + if (propertyValue.isInverse()) { + referencingEntityConfiguration.addToManyMiddleNotOwningRelation(propertyName, mappedBy, referencedEntityName); + } else { + referencingEntityConfiguration.addToManyMiddleRelation(propertyName, referencedEntityName); + } + } + } + + private Element createMiddleEntityXml(String versionsMiddleTableName, String versionsMiddleEntityName) { + String schema = StringTools.isEmpty(joinTable.schema()) ? propertyValue.getCollectionTable().getSchema() : joinTable.schema(); + String catalog = StringTools.isEmpty(joinTable.catalog()) ? propertyValue.getCollectionTable().getCatalog() : joinTable.catalog(); + + Element middleEntityXml = MetadataTools.createEntity(xmlMappingData.newAdditionalMapping(), + versionsMiddleEntityName, versionsMiddleTableName, schema, catalog, null); + Element middleEntityXmlId = middleEntityXml.addElement("composite-id"); + + middleEntityXmlId.addAttribute("name", mainGenerator.getVerEntCfg().getOriginalIdPropName()); + + // Adding the revision number as a foreign key to the revision info entity to the composite id of the + // middle table. + mainGenerator.addRevisionInfoRelation(middleEntityXmlId); + + // Adding the revision type property to the entity xml. + mainGenerator.addRevisionType(middleEntityXml); + + // All other properties should also be part of the primary key of the middle entity. + return middleEntityXmlId; + } + + private VersionsJoinTable getDefaultVersionsJoinTable() { + return new VersionsJoinTable() { + public String name() { return ""; } + public String schema() { return ""; } + public String catalog() { return ""; } + public JoinColumn[] inverseJoinColumns() { return new JoinColumn[0]; } + public Class annotationType() { return this.getClass(); } + }; + } + + @SuppressWarnings({"unchecked"}) + private String getMappedBy(Collection collectionValue) { + Iterator assocClassProps = + ((OneToMany) collectionValue.getElement()).getAssociatedClass().getPropertyIterator(); + + while (assocClassProps.hasNext()) { + Property property = assocClassProps.next(); + + if (Tools.iteratorsContentEqual(property.getValue().getColumnIterator(), + collectionValue.getKey().getColumnIterator())) { + return property.getName(); + } + } + + throw new MappingException("Unable to read the mapped by attribute for " + propertyName + " in " + + referencingEntityName + "!"); + } + + @SuppressWarnings({"unchecked"}) + private String getMappedBy(Table collectionTable, PersistentClass referencedClass) { + Iterator properties = referencedClass.getPropertyIterator(); + while (properties.hasNext()) { + Property property = properties.next(); + if (property.getValue() instanceof Collection) { + // The equality is intentional. We want to find a collection property with the same collection table. + //noinspection ObjectEquality + if (((Collection) property.getValue()).getCollectionTable() == collectionTable) { + return property.getName(); + } + } + } + + throw new MappingException("Unable to read the mapped by attribute for " + propertyName + " in " + + referencingEntityName + "!"); + } } Modified: trunk/src/main/org/jboss/envers/configuration/metadata/VersionsMetadataGenerator.java =================================================================== --- trunk/src/main/org/jboss/envers/configuration/metadata/VersionsMetadataGenerator.java 2008-09-22 11:26:24 UTC (rev 159) +++ trunk/src/main/org/jboss/envers/configuration/metadata/VersionsMetadataGenerator.java 2008-09-22 15:33:44 UTC (rev 160) @@ -55,7 +55,6 @@ private final BasicMetadataGenerator basicMetadataGenerator; private final IdMetadataGenerator idMetadataGenerator; - private final CollectionMetadataGenerator collectionMetadataGenerator; private final ToOneRelationMetadataGenerator toOneRelationMetadataGenerator; private final Map entitiesConfigurations; @@ -75,7 +74,6 @@ this.basicMetadataGenerator = new BasicMetadataGenerator(this); this.idMetadataGenerator = new IdMetadataGenerator(this); - this.collectionMetadataGenerator = new CollectionMetadataGenerator(this); this.toOneRelationMetadataGenerator = new ToOneRelationMetadataGenerator(this); entitiesConfigurations = new HashMap(); @@ -166,17 +164,12 @@ } else if (type instanceof CollectionType) { // only second pass if (!firstPass) { - collectionMetadataGenerator.addCollection(name, (Collection) value, currentMapper, entityName, - xmlMappingData, joinTable); + CollectionMetadataGenerator collectionMetadataGenerator = new CollectionMetadataGenerator(this, + name, (Collection) value, currentMapper, entityName, xmlMappingData, joinTable); + collectionMetadataGenerator.addCollection(); } } else { - String message = "Type not supported for versioning: " + type.getClass().getName() + - ", on entity " + entityName + ", property '" + name + "'."; - if (globalCfg.isWarnOnUnsupportedTypes()) { - log.warn(message); - } else { - throw new MappingException(message); - } + throwUnsupportedTypeException(type, entityName, name); } } @@ -386,4 +379,14 @@ VersionsEntitiesConfiguration getVerEntCfg() { return verEntCfg; } + + void throwUnsupportedTypeException(Type type, String entityName, String propertyName) { + String message = "Type not supported for versioning: " + type.getClass().getName() + + ", on entity " + entityName + ", property '" + propertyName + "'."; + if (globalCfg.isWarnOnUnsupportedTypes()) { + log.warn(message); + } else { + throw new MappingException(message); + } + } } Deleted: trunk/src/main/org/jboss/envers/entities/mapper/relation/AbstractOneToManyMapper.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/AbstractOneToManyMapper.java 2008-09-22 11:26:24 UTC (rev 159) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/AbstractOneToManyMapper.java 2008-09-22 15:33:44 UTC (rev 160) @@ -1,84 +0,0 @@ -/* - * JBoss, Home of Professional Open Source - * - * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated - * by the @authors tag. All rights reserved. - * - * See the copyright.txt in the distribution for a full listing of individual - * contributors. 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.reader.VersionsReaderImplementor; -import org.jboss.envers.tools.reflection.ReflectionTools; -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.initializor.Initializor; -import org.jboss.envers.entities.mapper.relation.lazy.proxy.MapProxy; -import org.jboss.envers.entities.mapper.PropertyMapper; -import org.jboss.envers.exception.VersionsException; -import org.jboss.envers.configuration.VersionsConfiguration; -import org.hibernate.property.Getter; -import org.hibernate.property.Setter; - -import java.util.*; - -/** - * @author Adam Warski (adam at warski dot org) - */ -public abstract class AbstractOneToManyMapper implements PropertyMapper { - private final String owningEntityName; - private final String propertyName; - - protected AbstractOneToManyMapper(String owningEntityName, String propertyName) { - this.owningEntityName = owningEntityName; - this.propertyName = propertyName; - } - - protected abstract Initializor getInitializator(VersionsConfiguration verCfg, - VersionsReaderImplementor versionsReader, - Class entityClass, Object primaryKey, - Number revision, Class collectionClass); - - @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 - Object value; - if (List.class.isAssignableFrom(collectionClass)) { - value = new ListProxy(getInitializator(verCfg, versionsReader, entityClass, primaryKey, revision, ArrayList.class)); - } else if (Set.class.isAssignableFrom(collectionClass) || Collection.class.isAssignableFrom(collectionClass)) { - value = new SetProxy(getInitializator(verCfg, versionsReader, entityClass, primaryKey, revision, HashSet.class)); - } else if (Map.class.isAssignableFrom(collectionClass)) { - value = new MapProxy(getInitializator(verCfg, 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); - } -} Modified: trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyAttachedMapper.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyAttachedMapper.java 2008-09-22 11:26:24 UTC (rev 159) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToManyAttachedMapper.java 2008-09-22 15:33:44 UTC (rev 160) @@ -25,9 +25,16 @@ 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; @@ -35,24 +42,54 @@ /** * @author Adam Warski (adam at warski dot org) */ -public class OneToManyAttachedMapper extends AbstractOneToManyMapper implements PropertyMapper { +public class OneToManyAttachedMapper implements PropertyMapper { + private final String owningEntityName; + private final String propertyName; private final String owningReferencePropertyName; - public OneToManyAttachedMapper(String owningReferencePropertyName, String owningEntityName, String propertyName) { - super(owningEntityName, propertyName); - + 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 data, Object newObj, Object oldObj) { return false; } - protected Initializor getInitializator(VersionsConfiguration verCfg, - VersionsReaderImplementor versionsReader, - Class entityClass, Object primaryKey, - Number revision, - Class collectionClass) { + protected Initializor getInitializator(VersionsReaderImplementor versionsReader, + Class entityClass, Object primaryKey, + Number revision, + Class collectionClass) { return new OneToManyAttachedInitializor(versionsReader, entityClass, owningReferencePropertyName, primaryKey, revision, collectionClass); } From jboss-envers-commits at lists.jboss.org Mon Sep 22 12:45:45 2008 From: jboss-envers-commits at lists.jboss.org (jboss-envers-commits at lists.jboss.org) Date: Mon, 22 Sep 2008 12:45:45 -0400 Subject: [jboss-envers-commits] JBoss Envers SVN: r161 - in trunk/src/main/org/jboss/envers: entities/mapper/relation and 1 other directory. Message-ID: Author: adamw Date: 2008-09-22 12:45:44 -0400 (Mon, 22 Sep 2008) New Revision: 161 Added: trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToOneNotOwningMapper.java Modified: trunk/src/main/org/jboss/envers/configuration/metadata/BasicMetadataGenerator.java trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java trunk/src/main/org/jboss/envers/configuration/metadata/IdMetadataGenerator.java trunk/src/main/org/jboss/envers/configuration/metadata/ToOneRelationMetadataGenerator.java trunk/src/main/org/jboss/envers/configuration/metadata/VersionsMetadataGenerator.java Log: ENVERS-44: restructuring the basic mapper Modified: trunk/src/main/org/jboss/envers/configuration/metadata/BasicMetadataGenerator.java =================================================================== --- trunk/src/main/org/jboss/envers/configuration/metadata/BasicMetadataGenerator.java 2008-09-22 15:33:44 UTC (rev 160) +++ trunk/src/main/org/jboss/envers/configuration/metadata/BasicMetadataGenerator.java 2008-09-22 16:45:44 UTC (rev 161) @@ -5,12 +5,11 @@ import org.hibernate.mapping.Column; import org.hibernate.mapping.Component; import org.hibernate.mapping.Property; -import org.hibernate.type.CustomType; +import org.hibernate.type.*; import org.hibernate.util.StringHelper; import org.jboss.envers.entities.mapper.SimpleMapperBuilder; import org.jboss.envers.entities.mapper.CompositeMapperBuilder; import org.jboss.envers.ModificationStore; -import org.jboss.envers.exception.VersionsException; import java.util.Iterator; @@ -19,14 +18,52 @@ * @author Adam Warski (adam at warski dot org) */ public final class BasicMetadataGenerator { - private final VersionsMetadataGenerator mainGenerator; + boolean addBasic(Element parent, String name, Value value, CompositeMapperBuilder mapper, + ModificationStore store, String entityName, boolean key) { + Type type = value.getType(); - BasicMetadataGenerator(VersionsMetadataGenerator versionsMetadataGenerator) { - mainGenerator = versionsMetadataGenerator; + if (type instanceof ComponentType) { + addComponent(parent, name, value, mapper, entityName, key); + } else if (type instanceof ImmutableType || type instanceof MutableType) { + addSimpleValue(parent, name, value, mapper, store, key); + } else if (type instanceof CustomType && "org.hibernate.type.EnumType".equals(type.getName())) { + addEnumValue(parent, name, value, mapper, store, key); + } else if (type instanceof CustomType && + ("org.hibernate.type.PrimitiveCharacterArrayClobType".equals(type.getName()) || + "org.hibernate.type.StringClobType".equals(type.getName()))) { + addSimpleValue(parent, name, value, mapper, store, key); + } else if ("org.hibernate.type.PrimitiveByteArrayBlobType".equals(type.getClass().getName())) { + addSimpleValue(parent, name, value, mapper, store, key); + } else { + return false; + } + + return true; } + boolean addBasicNoComponent(Element parent, String name, Value value, SimpleMapperBuilder mapper, + ModificationStore store, boolean key) { + Type type = value.getType(); + + if (type instanceof ImmutableType || type instanceof MutableType) { + addSimpleValue(parent, name, value, mapper, store, key); + } else if (type instanceof CustomType && "org.hibernate.type.EnumType".equals(type.getName())) { + addEnumValue(parent, name, value, mapper, store, key); + } else if (type instanceof CustomType && + ("org.hibernate.type.PrimitiveCharacterArrayClobType".equals(type.getName()) || + "org.hibernate.type.StringClobType".equals(type.getName()))) { + addSimpleValue(parent, name, value, mapper, store, key); + } else if ("org.hibernate.type.PrimitiveByteArrayBlobType".equals(type.getClass().getName())) { + addSimpleValue(parent, name, value, mapper, store, key); + } else { + return false; + } + + return true; + } + @SuppressWarnings({"unchecked"}) - void addSimpleValue(Element parent, String name, Value value, SimpleMapperBuilder mapper, + private void addSimpleValue(Element parent, String name, Value value, SimpleMapperBuilder mapper, ModificationStore store, boolean key) { if (parent != null) { Element prop_mapping = MetadataTools.addProperty(parent, name, @@ -41,8 +78,8 @@ } @SuppressWarnings({"unchecked"}) - void addEnumValue(Element parent, String name, Value value, SimpleMapperBuilder mapper, - ModificationStore store) { + private void addEnumValue(Element parent, String name, Value value, SimpleMapperBuilder mapper, + ModificationStore store, boolean key) { if (parent != null) { Element prop_mapping = parent.addElement("property"); prop_mapping.addAttribute("name", name); @@ -56,9 +93,9 @@ type_param1.addAttribute("name", "enumClass"); type_param1.setText(propertyType.getReturnedClass().getName()); - Element type_param2 = type_mapping.addElement("param"); - type_param2.addAttribute("name", "type"); - type_param2.setText(Integer.toString(propertyType.sqlTypes(null)[0])); + //Element type_param2 = type_mapping.addElement("param"); + //type_param2.addAttribute("name", "type"); + //type_param2.setText(Integer.toString(propertyType.sqlTypes(null)[0])); MetadataTools.addColumns(prop_mapping, (Iterator) value.getColumnIterator()); } @@ -73,13 +110,14 @@ } @SuppressWarnings({"unchecked"}) - void addComponent(Element parent, String name, Value value, CompositeMapperBuilder mapper, - String entityName, EntityXmlMappingData xmlMappingData, - boolean firstPass) { + private void addComponent(Element parent, String name, Value value, CompositeMapperBuilder mapper, + String entityName, boolean key) { Element component_mapping = null; Component prop_component = (Component) value; if (parent != null) { + /* + TODO: investigate relations inside components if (!firstPass) { // The required element already exists. Iterator iter = parent.elementIterator("component"); @@ -95,11 +133,12 @@ throw new VersionsException("Element for component not found during second pass!"); } } else { - component_mapping = parent.addElement("component"); - component_mapping.addAttribute("name", name); + */ - addComponentClassName(component_mapping, prop_component); - } + component_mapping = parent.addElement("component"); + component_mapping.addAttribute("name", name); + + addComponentClassName(component_mapping, prop_component); } CompositeMapperBuilder componentMapper = mapper.addComposite(name); @@ -107,8 +146,8 @@ Iterator properties = (Iterator) prop_component.getPropertyIterator(); while (properties.hasNext()) { Property property = properties.next(); - mainGenerator.addValue(component_mapping, property.getName(), property.getValue(), - componentMapper, ModificationStore.FULL, entityName, xmlMappingData, null, firstPass); + addBasic(component_mapping, property.getName(), property.getValue(), componentMapper, + ModificationStore.FULL, entityName, key); } } } Modified: trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java =================================================================== --- trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java 2008-09-22 15:33:44 UTC (rev 160) +++ trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java 2008-09-22 16:45:44 UTC (rev 161) @@ -306,13 +306,15 @@ queryGeneratorBuilder.getCurrentIndex()); } else if (type instanceof ImmutableType || type instanceof MutableType) { // TODO: add support for enums, components, custom types - mainGenerator.getBasicMetadataGenerator().addSimpleValue(xmlMapping, prefix, value, null, ModificationStore.FULL, true); + mainGenerator.getBasicMetadataGenerator().addBasic(xmlMapping, prefix, value, null, ModificationStore.FULL, + referencingEntityName, true); // Simple values are always stored in the first entity read by the query generator. return new MiddleComponentData(new MiddleSimpleComponentMapper(mainGenerator.getVerEntCfg(), prefix), 0); } else { - // TODO: throw an exception - throw new RuntimeException(); + mainGenerator.throwUnsupportedTypeException(type, referencingEntityName, propertyName); + // Impossible to get here. + throw new AssertionError(); } } Modified: trunk/src/main/org/jboss/envers/configuration/metadata/IdMetadataGenerator.java =================================================================== --- trunk/src/main/org/jboss/envers/configuration/metadata/IdMetadataGenerator.java 2008-09-22 15:33:44 UTC (rev 160) +++ trunk/src/main/org/jboss/envers/configuration/metadata/IdMetadataGenerator.java 2008-09-22 16:45:44 UTC (rev 161) @@ -36,7 +36,7 @@ Type propertyType = property.getType(); if (!"_identifierMapper".equals(property.getName())) { if (propertyType instanceof ImmutableType) { - mainGenerator.getBasicMetadataGenerator().addSimpleValue(parent, property.getName(), + mainGenerator.getBasicMetadataGenerator().addBasicNoComponent(parent, property.getName(), property.getValue(), mapper, ModificationStore.FULL, key); } else { throw new MappingException("Type not supported: " + propertyType.getClass().getName()); @@ -57,12 +57,16 @@ SimpleIdMapperBuilder mapper; if (id_mapper != null) { + // Multiple id + mapper = new MultipleIdMapper(((Component) pc.getIdentifier()).getComponentClassName()); addIdProperties(rel_id_mapping, (Iterator) id_mapper.getPropertyIterator(), mapper, false); // null mapper - the mapping where already added the first time, now we only want to generate the xml addIdProperties(orig_id_mapping, (Iterator) id_mapper.getPropertyIterator(), null, true); } else if (id_prop.isComposite()) { + // Embedded id + Component id_component = (Component) id_prop.getValue(); mapper = new EmbeddedIdMapper(id_prop.getName(), id_component.getComponentClassName()); @@ -71,13 +75,15 @@ // null mapper - the mapping where already added the first time, now we only want to generate the xml addIdProperties(orig_id_mapping, (Iterator) id_component.getPropertyIterator(), null, true); } else { + // Single id + mapper = new SingleIdMapper(); - mainGenerator.getBasicMetadataGenerator().addSimpleValue(rel_id_mapping, id_prop.getName(), + mainGenerator.getBasicMetadataGenerator().addBasicNoComponent(rel_id_mapping, id_prop.getName(), id_prop.getValue(), mapper, ModificationStore.FULL, false); // null mapper - the mapping where already added the first time, now we only want to generate the xml - mainGenerator.getBasicMetadataGenerator().addSimpleValue(orig_id_mapping, id_prop.getName(), + mainGenerator.getBasicMetadataGenerator().addBasicNoComponent(orig_id_mapping, id_prop.getName(), id_prop.getValue(), null, ModificationStore.FULL, true); } Modified: trunk/src/main/org/jboss/envers/configuration/metadata/ToOneRelationMetadataGenerator.java =================================================================== --- trunk/src/main/org/jboss/envers/configuration/metadata/ToOneRelationMetadataGenerator.java 2008-09-22 15:33:44 UTC (rev 160) +++ trunk/src/main/org/jboss/envers/configuration/metadata/ToOneRelationMetadataGenerator.java 2008-09-22 16:45:44 UTC (rev 161) @@ -7,7 +7,7 @@ import org.hibernate.MappingException; import org.jboss.envers.entities.mapper.CompositeMapperBuilder; import org.jboss.envers.entities.mapper.relation.ToOneIdMapper; -import org.jboss.envers.entities.mapper.relation.OneToOneNotOwningIdMapper; +import org.jboss.envers.entities.mapper.relation.OneToOneNotOwningMapper; import org.jboss.envers.entities.mapper.id.IdMapper; import org.jboss.envers.entities.EntityConfiguration; import org.jboss.envers.entities.IdMappingData; @@ -82,7 +82,7 @@ referencedEntityName, ownedIdMapper); // Adding mapper for the id - mapper.addComposite(name, new OneToOneNotOwningIdMapper(owningReferencePropertyName, + mapper.addComposite(name, new OneToOneNotOwningMapper(owningReferencePropertyName, referencedEntityName, name)); } } Modified: trunk/src/main/org/jboss/envers/configuration/metadata/VersionsMetadataGenerator.java =================================================================== --- trunk/src/main/org/jboss/envers/configuration/metadata/VersionsMetadataGenerator.java 2008-09-22 15:33:44 UTC (rev 160) +++ trunk/src/main/org/jboss/envers/configuration/metadata/VersionsMetadataGenerator.java 2008-09-22 16:45:44 UTC (rev 161) @@ -72,7 +72,7 @@ this.verEntCfg = verEntCfg; this.revisionInfoRelationMapping = revisionInfoRelationMapping; - this.basicMetadataGenerator = new BasicMetadataGenerator(this); + this.basicMetadataGenerator = new BasicMetadataGenerator(); this.idMetadataGenerator = new IdMetadataGenerator(this); this.toOneRelationMetadataGenerator = new ToOneRelationMetadataGenerator(this); @@ -122,36 +122,15 @@ VersionsJoinTable joinTable, boolean firstPass) { Type type = value.getType(); - if (type instanceof ComponentType) { - // only first pass - if (firstPass) { - basicMetadataGenerator.addComponent(parent, name, value, currentMapper, entityName, xmlMappingData, firstPass); + // only first pass + if (firstPass) { + if (basicMetadataGenerator.addBasic(parent, name, value, currentMapper, store, entityName, false)) { + // The property was mapped by the basic generator. + return; } - } else if (type instanceof ImmutableType || type instanceof MutableType) { - // only first pass - if (firstPass) { - basicMetadataGenerator.addSimpleValue(parent, name, value, currentMapper, store, false); - } - } else if (type instanceof CustomType && - "org.hibernate.type.EnumType".equals(type.getName())) { - // only first pass - if (firstPass) { - basicMetadataGenerator.addEnumValue(parent, name, value, currentMapper, store); - } - } else if (type instanceof CustomType && - ("org.hibernate.type.PrimitiveCharacterArrayClobType".equals(type.getName()) || - "org.hibernate.type.StringClobType".equals(type.getName()))) { - // only first pass - if (firstPass) { - basicMetadataGenerator.addSimpleValue(parent, name, value, currentMapper, store, false); - } - } else if ("org.hibernate.type.PrimitiveByteArrayBlobType".equals( - type.getClass().getName())) { - // only first pass - if (firstPass) { - basicMetadataGenerator.addSimpleValue(parent, name, value, currentMapper, store, false); - } - } else if (type instanceof ManyToOneType) { + } + + if (type instanceof ManyToOneType) { // only second pass if (!firstPass) { toOneRelationMetadataGenerator.addToOne(parent, name, value, currentMapper, entityName); @@ -169,7 +148,11 @@ collectionMetadataGenerator.addCollection(); } } else { - throwUnsupportedTypeException(type, entityName, name); + if (firstPass) { + // If we got here in the first pass, it means the basic mapper didn't map it, and none of the + // above branches either. + throwUnsupportedTypeException(type, entityName, name); + } } } Copied: trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToOneNotOwningMapper.java (from rev 148, trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToOneNotOwningIdMapper.java) =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToOneNotOwningMapper.java (rev 0) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToOneNotOwningMapper.java 2008-09-22 16:45:44 UTC (rev 161) @@ -0,0 +1,87 @@ +/* + * 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.tools.reflection.ReflectionTools; +import org.jboss.envers.query.VersionsRestrictions; +import org.jboss.envers.exception.VersionsException; +import org.jboss.envers.configuration.VersionsConfiguration; +import org.hibernate.property.Setter; +import org.hibernate.NonUniqueResultException; +import org.hibernate.collection.PersistentCollection; + +import javax.persistence.NoResultException; +import java.util.Map; +import java.util.List; +import java.io.Serializable; + +/** + * @author Adam Warski (adam at warski dot org) + */ +public class OneToOneNotOwningMapper implements PropertyMapper { + private String owningReferencePropertyName; + private String owningEntityName; + private String propertyName; + + public OneToOneNotOwningMapper(String owningReferencePropertyName, String owningEntityName, String propertyName) { + this.owningReferencePropertyName = owningReferencePropertyName; + this.owningEntityName = owningEntityName; + this.propertyName = propertyName; + } + + public boolean mapToMapFromEntity(Map data, Object newObj, Object oldObj) { + return false; + } + + public void mapToEntityFromMap(VersionsConfiguration verCfg, Object obj, Map data, Object primaryKey, VersionsReaderImplementor versionsReader, Number revision) { + if (obj == null) { + return; + } + + Class entityClass = ReflectionTools.loadClass(owningEntityName); + + Object value; + + try { + value = versionsReader.createQuery().forEntitiesAtRevision(entityClass, revision) + .add(VersionsRestrictions.relatedIdEq(owningReferencePropertyName, primaryKey)).getSingleResult(); + } catch (NoResultException e) { + value = null; + } catch (NonUniqueResultException e) { + throw new VersionsException("Many versions results for one-to-one relationship: (" + owningEntityName + + ", " + owningReferencePropertyName + ")"); + } + + Setter setter = ReflectionTools.getSetter(obj.getClass(), propertyName); + setter.set(obj, value, null); + } + + public List mapCollectionChanges(String referencingPropertyName, + PersistentCollection newColl, + Serializable oldColl, + Serializable id) { + return null; + } +} From jboss-envers-commits at lists.jboss.org Mon Sep 22 14:07:02 2008 From: jboss-envers-commits at lists.jboss.org (jboss-envers-commits at lists.jboss.org) Date: Mon, 22 Sep 2008 14:07:02 -0400 Subject: [jboss-envers-commits] JBoss Envers SVN: r162 - in trunk/src: test/org/jboss/envers/test/entities/collection and 1 other directories. Message-ID: Author: adamw Date: 2008-09-22 14:07:02 -0400 (Mon, 22 Sep 2008) New Revision: 162 Added: trunk/src/test/org/jboss/envers/test/entities/collection/EnumSetEntity.java trunk/src/test/org/jboss/envers/test/integration/collection/EnumSet.java Modified: trunk/src/main/org/jboss/envers/configuration/metadata/BasicMetadataGenerator.java trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java trunk/src/main/org/jboss/envers/configuration/metadata/MetadataTools.java Log: ENVERS-24, ENVERS-42: enum support in collections Modified: trunk/src/main/org/jboss/envers/configuration/metadata/BasicMetadataGenerator.java =================================================================== --- trunk/src/main/org/jboss/envers/configuration/metadata/BasicMetadataGenerator.java 2008-09-22 16:45:44 UTC (rev 161) +++ trunk/src/main/org/jboss/envers/configuration/metadata/BasicMetadataGenerator.java 2008-09-22 18:07:02 UTC (rev 162) @@ -71,7 +71,7 @@ MetadataTools.addColumns(prop_mapping, (Iterator) value.getColumnIterator()); } - // A null mapper means that we only want to add xml mappings (while building the id mapping) + // A null mapper means that we only want to add xml mappings if (mapper != null) { mapper.add(name, store); } @@ -81,8 +81,8 @@ private void addEnumValue(Element parent, String name, Value value, SimpleMapperBuilder mapper, ModificationStore store, boolean key) { if (parent != null) { - Element prop_mapping = parent.addElement("property"); - prop_mapping.addAttribute("name", name); + Element prop_mapping = MetadataTools.addProperty(parent, name, + null, key); CustomType propertyType = (CustomType) value.getType(); @@ -93,14 +93,16 @@ type_param1.addAttribute("name", "enumClass"); type_param1.setText(propertyType.getReturnedClass().getName()); - //Element type_param2 = type_mapping.addElement("param"); - //type_param2.addAttribute("name", "type"); - //type_param2.setText(Integer.toString(propertyType.sqlTypes(null)[0])); + Element type_param2 = type_mapping.addElement("param"); + type_param2.addAttribute("name", "type"); + type_param2.setText(Integer.toString(propertyType.sqlTypes(null)[0])); MetadataTools.addColumns(prop_mapping, (Iterator) value.getColumnIterator()); } - mapper.add(name, store); + if (mapper != null) { + mapper.add(name, store); + } } private void addComponentClassName(Element any_mapping, Component comp) { Modified: trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java =================================================================== --- trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java 2008-09-22 16:45:44 UTC (rev 161) +++ trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java 2008-09-22 18:07:02 UTC (rev 162) @@ -304,17 +304,18 @@ return new MiddleComponentData(new MiddleRelatedComponentMapper(referencedIdData), queryGeneratorBuilder.getCurrentIndex()); - } else if (type instanceof ImmutableType || type instanceof MutableType) { - // TODO: add support for enums, components, custom types - mainGenerator.getBasicMetadataGenerator().addBasic(xmlMapping, prefix, value, null, ModificationStore.FULL, - referencingEntityName, true); + } else { + boolean mapped = mainGenerator.getBasicMetadataGenerator().addBasic(xmlMapping, prefix, value, null, + ModificationStore.FULL, referencingEntityName, true); - // Simple values are always stored in the first entity read by the query generator. - return new MiddleComponentData(new MiddleSimpleComponentMapper(mainGenerator.getVerEntCfg(), prefix), 0); - } else { - mainGenerator.throwUnsupportedTypeException(type, referencingEntityName, propertyName); - // Impossible to get here. - throw new AssertionError(); + if (mapped) { + // Simple values are always stored in the first item of the array returned by the query generator. + return new MiddleComponentData(new MiddleSimpleComponentMapper(mainGenerator.getVerEntCfg(), prefix), 0); + } else { + mainGenerator.throwUnsupportedTypeException(type, referencingEntityName, propertyName); + // Impossible to get here. + throw new AssertionError(); + } } } Modified: trunk/src/main/org/jboss/envers/configuration/metadata/MetadataTools.java =================================================================== --- trunk/src/main/org/jboss/envers/configuration/metadata/MetadataTools.java 2008-09-22 16:45:44 UTC (rev 161) +++ trunk/src/main/org/jboss/envers/configuration/metadata/MetadataTools.java 2008-09-22 18:07:02 UTC (rev 162) @@ -52,8 +52,12 @@ prop_mapping = parent.addElement("property"); } - prop_mapping.addAttribute("name", name).addAttribute("type", type); + prop_mapping.addAttribute("name", name); + if (type != null) { + prop_mapping.addAttribute("type", type); + } + return prop_mapping; } Copied: trunk/src/test/org/jboss/envers/test/entities/collection/EnumSetEntity.java (from rev 153, trunk/src/test/org/jboss/envers/test/entities/collection/StringSetEntity.java) =================================================================== --- trunk/src/test/org/jboss/envers/test/entities/collection/EnumSetEntity.java (rev 0) +++ trunk/src/test/org/jboss/envers/test/entities/collection/EnumSetEntity.java 2008-09-22 18:07:02 UTC (rev 162) @@ -0,0 +1,79 @@ +package org.jboss.envers.test.entities.collection; + +import org.jboss.envers.Versioned; +import org.hibernate.annotations.CollectionOfElements; + +import javax.persistence.*; +import java.util.Set; +import java.util.HashSet; + +/** + * @author Adam Warski (adam at warski dot org) + */ + at Entity +public class EnumSetEntity { + public static enum E1 { X, Y } + public static enum E2 { A, B } + + @Id + @GeneratedValue + private Integer id; + + @Versioned + @CollectionOfElements + @Enumerated(EnumType.STRING) + private Set enums1; + + @Versioned + @CollectionOfElements + @Enumerated(EnumType.ORDINAL) + private Set enums2; + + public EnumSetEntity() { + enums1 = new HashSet(); + enums2 = new HashSet(); + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public Set getEnums1() { + return enums1; + } + + public void setEnums1(Set enums1) { + this.enums1 = enums1; + } + + public Set getEnums2() { + return enums2; + } + + public void setEnums2(Set enums2) { + this.enums2 = enums2; + } + + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof EnumSetEntity)) return false; + + EnumSetEntity that = (EnumSetEntity) 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 "ESE(id = " + id + ", enums1 = " + enums1 + ", enums2 = " + enums2 + ")"; + } +} \ No newline at end of file Property changes on: trunk/src/test/org/jboss/envers/test/entities/collection/EnumSetEntity.java ___________________________________________________________________ Name: svn:mergeinfo + Copied: trunk/src/test/org/jboss/envers/test/integration/collection/EnumSet.java (from rev 154, trunk/src/test/org/jboss/envers/test/integration/collection/StringSet.java) =================================================================== --- trunk/src/test/org/jboss/envers/test/integration/collection/EnumSet.java (rev 0) +++ trunk/src/test/org/jboss/envers/test/integration/collection/EnumSet.java 2008-09-22 18:07:02 UTC (rev 162) @@ -0,0 +1,86 @@ +package org.jboss.envers.test.integration.collection; + +import org.jboss.envers.test.integration.AbstractEntityTest; +import org.jboss.envers.test.tools.TestTools; +import org.jboss.envers.test.entities.collection.EnumSetEntity; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; +import org.hibernate.ejb.Ejb3Configuration; + +import javax.persistence.EntityManager; +import java.util.Arrays; +import java.util.Collections; + +import static org.jboss.envers.test.entities.collection.EnumSetEntity.*; + +/** + * @author Adam Warski (adam at warski dot org) + */ +public class EnumSet extends AbstractEntityTest { + private Integer sse1_id; + + public void configure(Ejb3Configuration cfg) { + cfg.addAnnotatedClass(EnumSetEntity.class); + } + + @BeforeClass(dependsOnMethods = "init") + public void initData() { + EntityManager em = getEntityManager(); + + EnumSetEntity sse1 = new EnumSetEntity(); + + // Revision 1 (sse1: initialy 1 element) + em.getTransaction().begin(); + + sse1.getEnums1().add(E1.X); + sse1.getEnums2().add(E2.A); + + em.persist(sse1); + + em.getTransaction().commit(); + + // Revision 2 (sse1: adding 1 element/removing a non-existing element) + em.getTransaction().begin(); + + sse1 = em.find(EnumSetEntity.class, sse1.getId()); + + sse1.getEnums1().add(E1.Y); + sse1.getEnums2().remove(E2.B); + + em.getTransaction().commit(); + + // Revision 3 (sse1: removing 1 element/adding an exisiting element) + em.getTransaction().begin(); + + sse1 = em.find(EnumSetEntity.class, sse1.getId()); + + sse1.getEnums1().remove(E1.X); + sse1.getEnums2().add(E2.A); + + em.getTransaction().commit(); + + // + + sse1_id = sse1.getId(); + } + + @Test + public void testRevisionsCounts() { + assert Arrays.asList(1, 2, 3).equals(getVersionsReader().getRevisions(EnumSetEntity.class, sse1_id)); + } + + @Test + public void testHistoryOfSse1() { + EnumSetEntity rev1 = getVersionsReader().find(EnumSetEntity.class, sse1_id, 1); + EnumSetEntity rev2 = getVersionsReader().find(EnumSetEntity.class, sse1_id, 2); + EnumSetEntity rev3 = getVersionsReader().find(EnumSetEntity.class, sse1_id, 3); + + assert rev1.getEnums1().equals(TestTools.makeSet(E1.X)); + assert rev2.getEnums1().equals(TestTools.makeSet(E1.X, E1.Y)); + assert rev3.getEnums1().equals(TestTools.makeSet(E1.Y)); + + assert rev1.getEnums2().equals(TestTools.makeSet(E2.A)); + assert rev2.getEnums2().equals(TestTools.makeSet(E2.A)); + assert rev3.getEnums2().equals(TestTools.makeSet(E2.A)); + } +} \ No newline at end of file From jboss-envers-commits at lists.jboss.org Mon Sep 22 16:30:29 2008 From: jboss-envers-commits at lists.jboss.org (jboss-envers-commits at lists.jboss.org) Date: Mon, 22 Sep 2008 16:30:29 -0400 Subject: [jboss-envers-commits] JBoss Envers SVN: r163 - in trunk: src/main/org/jboss/envers/configuration/metadata and 4 other directories. Message-ID: Author: adamw Date: 2008-09-22 16:30:29 -0400 (Mon, 22 Sep 2008) New Revision: 163 Added: trunk/src/test/org/jboss/envers/test/entities/customtype/ trunk/src/test/org/jboss/envers/test/entities/customtype/Component.java trunk/src/test/org/jboss/envers/test/entities/customtype/CompositeCustomTypeEntity.java trunk/src/test/org/jboss/envers/test/entities/customtype/CompositeTestUserType.java trunk/src/test/org/jboss/envers/test/entities/customtype/ParametrizedCustomTypeEntity.java trunk/src/test/org/jboss/envers/test/entities/customtype/ParametrizedTestUserType.java trunk/src/test/org/jboss/envers/test/integration/customtype/ trunk/src/test/org/jboss/envers/test/integration/customtype/CompositeCustom.java trunk/src/test/org/jboss/envers/test/integration/customtype/ParametrizedCustom.java Modified: trunk/resources/test/testng.xml trunk/src/main/org/jboss/envers/configuration/metadata/BasicMetadataGenerator.java Log: ENVERS-49: custom user type support Modified: trunk/resources/test/testng.xml =================================================================== --- trunk/resources/test/testng.xml 2008-09-22 18:07:02 UTC (rev 162) +++ trunk/resources/test/testng.xml 2008-09-22 20:30:29 UTC (rev 163) @@ -6,6 +6,7 @@ + Modified: trunk/src/main/org/jboss/envers/configuration/metadata/BasicMetadataGenerator.java =================================================================== --- trunk/src/main/org/jboss/envers/configuration/metadata/BasicMetadataGenerator.java 2008-09-22 18:07:02 UTC (rev 162) +++ trunk/src/main/org/jboss/envers/configuration/metadata/BasicMetadataGenerator.java 2008-09-22 20:30:29 UTC (rev 163) @@ -1,10 +1,7 @@ package org.jboss.envers.configuration.metadata; import org.dom4j.Element; -import org.hibernate.mapping.Value; -import org.hibernate.mapping.Column; -import org.hibernate.mapping.Component; -import org.hibernate.mapping.Property; +import org.hibernate.mapping.*; import org.hibernate.type.*; import org.hibernate.util.StringHelper; import org.jboss.envers.entities.mapper.SimpleMapperBuilder; @@ -12,6 +9,7 @@ import org.jboss.envers.ModificationStore; import java.util.Iterator; +import java.util.Properties; /** * Generates metadata for basic properties: immutable types (including enums) and components @@ -19,40 +17,25 @@ */ public final class BasicMetadataGenerator { boolean addBasic(Element parent, String name, Value value, CompositeMapperBuilder mapper, - ModificationStore store, String entityName, boolean key) { + ModificationStore store, String entityName, boolean key) { Type type = value.getType(); if (type instanceof ComponentType) { addComponent(parent, name, value, mapper, entityName, key); - } else if (type instanceof ImmutableType || type instanceof MutableType) { - addSimpleValue(parent, name, value, mapper, store, key); - } else if (type instanceof CustomType && "org.hibernate.type.EnumType".equals(type.getName())) { - addEnumValue(parent, name, value, mapper, store, key); - } else if (type instanceof CustomType && - ("org.hibernate.type.PrimitiveCharacterArrayClobType".equals(type.getName()) || - "org.hibernate.type.StringClobType".equals(type.getName()))) { - addSimpleValue(parent, name, value, mapper, store, key); - } else if ("org.hibernate.type.PrimitiveByteArrayBlobType".equals(type.getClass().getName())) { - addSimpleValue(parent, name, value, mapper, store, key); + return true; } else { - return false; + return addBasicNoComponent(parent, name, value, mapper, store, key); } - - return true; } boolean addBasicNoComponent(Element parent, String name, Value value, SimpleMapperBuilder mapper, - ModificationStore store, boolean key) { + ModificationStore store, boolean key) { Type type = value.getType(); if (type instanceof ImmutableType || type instanceof MutableType) { addSimpleValue(parent, name, value, mapper, store, key); - } else if (type instanceof CustomType && "org.hibernate.type.EnumType".equals(type.getName())) { - addEnumValue(parent, name, value, mapper, store, key); - } else if (type instanceof CustomType && - ("org.hibernate.type.PrimitiveCharacterArrayClobType".equals(type.getName()) || - "org.hibernate.type.StringClobType".equals(type.getName()))) { - addSimpleValue(parent, name, value, mapper, store, key); + } else if (type instanceof CustomType || type instanceof CompositeCustomType) { + addCustomValue(parent, name, value, mapper, store, key); } else if ("org.hibernate.type.PrimitiveByteArrayBlobType".equals(type.getClass().getName())) { addSimpleValue(parent, name, value, mapper, store, key); } else { @@ -64,7 +47,7 @@ @SuppressWarnings({"unchecked"}) private void addSimpleValue(Element parent, String name, Value value, SimpleMapperBuilder mapper, - ModificationStore store, boolean key) { + ModificationStore store, boolean key) { if (parent != null) { Element prop_mapping = MetadataTools.addProperty(parent, name, value.getType().getName(), key); @@ -78,25 +61,28 @@ } @SuppressWarnings({"unchecked"}) - private void addEnumValue(Element parent, String name, Value value, SimpleMapperBuilder mapper, - ModificationStore store, boolean key) { + private void addCustomValue(Element parent, String name, Value value, SimpleMapperBuilder mapper, + ModificationStore store, boolean key) { if (parent != null) { Element prop_mapping = MetadataTools.addProperty(parent, name, null, key); - CustomType propertyType = (CustomType) value.getType(); + //CustomType propertyType = (CustomType) value.getType(); Element type_mapping = prop_mapping.addElement("type"); - type_mapping.addAttribute("name", propertyType.getName()); + type_mapping.addAttribute("name", value.getType().getName()); - Element type_param1 = type_mapping.addElement("param"); - type_param1.addAttribute("name", "enumClass"); - type_param1.setText(propertyType.getReturnedClass().getName()); + if (value instanceof SimpleValue) { + Properties typeParameters = ((SimpleValue) value).getTypeParameters(); + if (typeParameters != null) { + for (java.util.Map.Entry paramKeyValue : typeParameters.entrySet()) { + Element type_param = type_mapping.addElement("param"); + type_param.addAttribute("name", (String) paramKeyValue.getKey()); + type_param.setText((String) paramKeyValue.getValue()); + } + } + } - Element type_param2 = type_mapping.addElement("param"); - type_param2.addAttribute("name", "type"); - type_param2.setText(Integer.toString(propertyType.sqlTypes(null)[0])); - MetadataTools.addColumns(prop_mapping, (Iterator) value.getColumnIterator()); } @@ -113,7 +99,7 @@ @SuppressWarnings({"unchecked"}) private void addComponent(Element parent, String name, Value value, CompositeMapperBuilder mapper, - String entityName, boolean key) { + String entityName, boolean key) { Element component_mapping = null; Component prop_component = (Component) value; Added: trunk/src/test/org/jboss/envers/test/entities/customtype/Component.java =================================================================== --- trunk/src/test/org/jboss/envers/test/entities/customtype/Component.java (rev 0) +++ trunk/src/test/org/jboss/envers/test/entities/customtype/Component.java 2008-09-22 20:30:29 UTC (rev 163) @@ -0,0 +1,54 @@ +package org.jboss.envers.test.entities.customtype; + +import java.io.Serializable; + +/** + * @author Adam Warski (adam at warski dot org) + */ +public class Component implements Serializable { + private String prop1; + private int prop2; + + public Component(String prop1, int prop2) { + this.prop1 = prop1; + this.prop2 = prop2; + } + + public Component() { + } + + public String getProp1() { + return prop1; + } + + public void setProp1(String prop1) { + this.prop1 = prop1; + } + + public int getProp2() { + return prop2; + } + + public void setProp2(int prop2) { + this.prop2 = prop2; + } + + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Component)) return false; + + Component that = (Component) o; + + if (prop2 != that.prop2) return false; + if (prop1 != null ? !prop1.equals(that.prop1) : that.prop1 != null) return false; + + return true; + } + + public int hashCode() { + int result; + result = (prop1 != null ? prop1.hashCode() : 0); + result = 31 * result + prop2; + return result; + } +} Added: trunk/src/test/org/jboss/envers/test/entities/customtype/CompositeCustomTypeEntity.java =================================================================== --- trunk/src/test/org/jboss/envers/test/entities/customtype/CompositeCustomTypeEntity.java (rev 0) +++ trunk/src/test/org/jboss/envers/test/entities/customtype/CompositeCustomTypeEntity.java 2008-09-22 20:30:29 UTC (rev 163) @@ -0,0 +1,74 @@ +package org.jboss.envers.test.entities.customtype; + +import org.jboss.envers.Versioned; +import org.hibernate.annotations.TypeDef; +import org.hibernate.annotations.Type; +import org.hibernate.annotations.Columns; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.GeneratedValue; +import javax.persistence.Column; + +/** + * @author Adam Warski (adam at warski dot org) + */ + at Entity + at TypeDef(name = "comp", typeClass = CompositeTestUserType.class) +public class CompositeCustomTypeEntity { + @Id + @GeneratedValue + private Integer id; + + @Versioned + @Type(type = "comp") + @Columns(columns = { @Column(name = "str"), @Column(name = "num") }) + private Component component; + + public CompositeCustomTypeEntity() { + } + + public CompositeCustomTypeEntity(Integer id, Component component) { + this.id = id; + this.component = component; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public Component getComponent() { + return component; + } + + public void setComponent(Component component) { + this.component = component; + } + + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof CompositeCustomTypeEntity)) return false; + + CompositeCustomTypeEntity that = (CompositeCustomTypeEntity) o; + + if (component != null ? !component.equals(that.component) : that.component != 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 + (component != null ? component.hashCode() : 0); + return result; + } + + public String toString() { + return "CCTE(id = " + id + ", component = " + component + ")"; + } +} Added: trunk/src/test/org/jboss/envers/test/entities/customtype/CompositeTestUserType.java =================================================================== --- trunk/src/test/org/jboss/envers/test/entities/customtype/CompositeTestUserType.java (rev 0) +++ trunk/src/test/org/jboss/envers/test/entities/customtype/CompositeTestUserType.java 2008-09-22 20:30:29 UTC (rev 163) @@ -0,0 +1,117 @@ +package org.jboss.envers.test.entities.customtype; + +import org.hibernate.HibernateException; +import org.hibernate.Hibernate; +import org.hibernate.usertype.CompositeUserType; +import org.hibernate.engine.SessionImplementor; +import org.hibernate.type.Type; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.PreparedStatement; +import java.io.Serializable; + +/** + * @author Andrew DePue + * @author Adam Warski (adam at warski dot org) + */ +public class CompositeTestUserType implements CompositeUserType { + public String[] getPropertyNames() { + return new String[] { "prop1", "prop2" }; + } + + public Type[] getPropertyTypes() { + return new Type[] { Hibernate.STRING, Hibernate.INTEGER }; + } + + public Object getPropertyValue(final Object component, final int property) throws HibernateException { + Component comp = (Component) component; + if (property == 0) { + return comp.getProp1(); + } else { + return comp.getProp2(); + } + } + + public void setPropertyValue(final Object component, final int property, final Object value) throws HibernateException { + Component comp = (Component) component; + if (property == 0) { + comp.setProp1((String) value); + } else { + comp.setProp2((Integer) value); + } + } + + public Class returnedClass() { + return Component.class; + } + + public boolean equals(final Object x, final Object y) throws HibernateException { + //noinspection ObjectEquality + if (x == y) { + return true; + } + + if (x == null || y == null) { + return false; + } + + return x.equals(y); + } + + public int hashCode(final Object x) throws HibernateException { + return x.hashCode(); + } + + public Object nullSafeGet(final ResultSet rs, final String[] names, + final SessionImplementor session, + final Object owner) throws HibernateException, SQLException { + if (rs.wasNull()) { + return null; + } + final String prop1 = rs.getString(names[0]); + if (prop1 == null) { + return null; + } + final int prop2 = rs.getInt(names[1]); + + return new Component(prop1, prop2); + } + + public void nullSafeSet(final PreparedStatement st, final Object value, + final int index, final SessionImplementor session) + throws HibernateException, SQLException + { + if (value == null) { + st.setNull(index, Hibernate.STRING.sqlType()); + st.setNull(index + 1, Hibernate.INTEGER.sqlType()); + } else { + final Component comp = (Component) value; + st.setString(index, comp.getProp1()); + st.setInt(index + 1, comp.getProp2()); + } + } + + public Object deepCopy(final Object value) throws HibernateException { + Component comp = (Component) value; + return new Component(comp.getProp1(), comp.getProp2()); + } + + public boolean isMutable() { + return true; + } + + public Serializable disassemble(final Object value, final SessionImplementor session) throws HibernateException { + return (Serializable) value; + } + + public Object assemble(final Serializable cached, final SessionImplementor session, + final Object owner) throws HibernateException { + return cached; + } + + public Object replace(Object original, Object target, + SessionImplementor session, Object owner) throws HibernateException { + return original; + } +} \ No newline at end of file Added: trunk/src/test/org/jboss/envers/test/entities/customtype/ParametrizedCustomTypeEntity.java =================================================================== --- trunk/src/test/org/jboss/envers/test/entities/customtype/ParametrizedCustomTypeEntity.java (rev 0) +++ trunk/src/test/org/jboss/envers/test/entities/customtype/ParametrizedCustomTypeEntity.java 2008-09-22 20:30:29 UTC (rev 163) @@ -0,0 +1,73 @@ +package org.jboss.envers.test.entities.customtype; + +import org.jboss.envers.Versioned; +import org.hibernate.annotations.TypeDef; +import org.hibernate.annotations.Type; +import org.hibernate.annotations.Parameter; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.GeneratedValue; + +/** + * @author Adam Warski (adam at warski dot org) + */ + at Entity + at TypeDef(name = "param", typeClass = ParametrizedTestUserType.class, + parameters = { @Parameter(name="param1", value = "x"), @Parameter(name="param2", value = "y") }) +public class ParametrizedCustomTypeEntity { + @Id + @GeneratedValue + private Integer id; + + @Versioned + @Type(type = "param") + private String str; + + public ParametrizedCustomTypeEntity() { + } + + public ParametrizedCustomTypeEntity(Integer id, String str) { + this.id = id; + this.str = str; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getStr() { + return str; + } + + public void setStr(String str) { + this.str = str; + } + + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof ParametrizedCustomTypeEntity)) return false; + + ParametrizedCustomTypeEntity that = (ParametrizedCustomTypeEntity) o; + + if (id != null ? !id.equals(that.id) : that.id != null) return false; + if (str != null ? !str.equals(that.str) : that.str != null) return false; + + return true; + } + + public int hashCode() { + int result; + result = (id != null ? id.hashCode() : 0); + result = 31 * result + (str != null ? str.hashCode() : 0); + return result; + } + + public String toString() { + return "PCTE(id = " + id + ", str = " + str + ")"; + } +} \ No newline at end of file Added: trunk/src/test/org/jboss/envers/test/entities/customtype/ParametrizedTestUserType.java =================================================================== --- trunk/src/test/org/jboss/envers/test/entities/customtype/ParametrizedTestUserType.java (rev 0) +++ trunk/src/test/org/jboss/envers/test/entities/customtype/ParametrizedTestUserType.java 2008-09-22 20:30:29 UTC (rev 163) @@ -0,0 +1,92 @@ +package org.jboss.envers.test.entities.customtype; + +import java.io.Serializable; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Properties; + +import org.hibernate.HibernateException; +import org.hibernate.Hibernate; +import org.hibernate.usertype.ParameterizedType; +import org.hibernate.usertype.UserType; +import org.hsqldb.Types; + +/** + * @author Adam Warski (adam at warski dot org) + */ +public class ParametrizedTestUserType implements UserType, ParameterizedType { + private static final int[] TYPES = new int[] { Types.VARCHAR }; + + private String param1; + private String param2; + + public void setParameterValues(Properties parameters) { + param1 = parameters.getProperty("param1"); + param2 = parameters.getProperty("param2"); + } + + public Class returnedClass() { + return String.class; + } + + public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException { + return Hibernate.STRING.nullSafeGet(rs, names[0]); + } + + public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException { + if (value != null) { + String v = (String) value; + if (!v.startsWith(param1)) { + v = param1 + v; + } + if (!v.endsWith(param2)) { + v = v + param2; + } + Hibernate.STRING.nullSafeSet(st, v, index); + } else { + Hibernate.STRING.nullSafeSet(st, value, index); + } + } + + public int[] sqlTypes() { + return TYPES; + } + + public Object assemble(Serializable cached, Object owner) throws HibernateException { + return cached; + } + + public Object deepCopy(Object value) throws HibernateException { + return value; + } + + public Serializable disassemble(Object value) throws HibernateException { + return (Serializable) value; + } + + public boolean equals(Object x, Object y) throws HibernateException { + //noinspection ObjectEquality + if (x == y) { + return true; + } + + if (x == null || y == null) { + return false; + } + + return x.equals(y); + } + + public int hashCode(Object x) throws HibernateException { + return x.hashCode(); + } + + public boolean isMutable() { + return false; + } + + public Object replace(Object original, Object target, Object owner) throws HibernateException { + return original; + } +} Copied: trunk/src/test/org/jboss/envers/test/integration/customtype/CompositeCustom.java (from rev 154, trunk/src/test/org/jboss/envers/test/integration/collection/StringSet.java) =================================================================== --- trunk/src/test/org/jboss/envers/test/integration/customtype/CompositeCustom.java (rev 0) +++ trunk/src/test/org/jboss/envers/test/integration/customtype/CompositeCustom.java 2008-09-22 20:30:29 UTC (rev 163) @@ -0,0 +1,76 @@ +package org.jboss.envers.test.integration.customtype; + +import org.jboss.envers.test.integration.AbstractEntityTest; +import org.jboss.envers.test.entities.customtype.CompositeCustomTypeEntity; +import org.jboss.envers.test.entities.customtype.Component; +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 CompositeCustom extends AbstractEntityTest { + private Integer ccte_id; + + public void configure(Ejb3Configuration cfg) { + cfg.addAnnotatedClass(CompositeCustomTypeEntity.class); + } + + @BeforeClass(dependsOnMethods = "init") + public void initData() { + EntityManager em = getEntityManager(); + + CompositeCustomTypeEntity ccte = new CompositeCustomTypeEntity(); + + // Revision 1 (persisting 1 entity) + em.getTransaction().begin(); + + ccte.setComponent(new Component("a", 1)); + + em.persist(ccte); + + em.getTransaction().commit(); + + // Revision 2 (changing the component) + em.getTransaction().begin(); + + ccte = em.find(CompositeCustomTypeEntity.class, ccte.getId()); + + ccte.getComponent().setProp1("b"); + + em.getTransaction().commit(); + + // Revision 3 (replacing the component) + em.getTransaction().begin(); + + ccte = em.find(CompositeCustomTypeEntity.class, ccte.getId()); + + ccte.setComponent(new Component("c", 3)); + + em.getTransaction().commit(); + + // + + ccte_id = ccte.getId(); + } + + @Test + public void testRevisionsCounts() { + assert Arrays.asList(1, 2, 3).equals(getVersionsReader().getRevisions(CompositeCustomTypeEntity.class, ccte_id)); + } + + @Test + public void testHistoryOfCcte() { + CompositeCustomTypeEntity rev1 = getVersionsReader().find(CompositeCustomTypeEntity.class, ccte_id, 1); + CompositeCustomTypeEntity rev2 = getVersionsReader().find(CompositeCustomTypeEntity.class, ccte_id, 2); + CompositeCustomTypeEntity rev3 = getVersionsReader().find(CompositeCustomTypeEntity.class, ccte_id, 3); + + assert rev1.getComponent().equals(new Component("a", 1)); + assert rev2.getComponent().equals(new Component("b", 1)); + assert rev3.getComponent().equals(new Component("c", 3)); + } +} \ No newline at end of file Added: trunk/src/test/org/jboss/envers/test/integration/customtype/ParametrizedCustom.java =================================================================== --- trunk/src/test/org/jboss/envers/test/integration/customtype/ParametrizedCustom.java (rev 0) +++ trunk/src/test/org/jboss/envers/test/integration/customtype/ParametrizedCustom.java 2008-09-22 20:30:29 UTC (rev 163) @@ -0,0 +1,64 @@ +package org.jboss.envers.test.integration.customtype; + +import org.jboss.envers.test.integration.AbstractEntityTest; +import org.jboss.envers.test.entities.customtype.ParametrizedCustomTypeEntity; +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 ParametrizedCustom extends AbstractEntityTest { + private Integer pcte_id; + + public void configure(Ejb3Configuration cfg) { + cfg.addAnnotatedClass(ParametrizedCustomTypeEntity.class); + } + + @BeforeClass(dependsOnMethods = "init") + public void initData() { + EntityManager em = getEntityManager(); + + ParametrizedCustomTypeEntity pcte = new ParametrizedCustomTypeEntity(); + + // Revision 1 (persisting 1 entity) + em.getTransaction().begin(); + + pcte.setStr("U"); + + em.persist(pcte); + + em.getTransaction().commit(); + + // Revision 2 (changing the value) + em.getTransaction().begin(); + + pcte = em.find(ParametrizedCustomTypeEntity.class, pcte.getId()); + + pcte.setStr("V"); + + em.getTransaction().commit(); + + // + + pcte_id = pcte.getId(); + } + + @Test + public void testRevisionsCounts() { + assert Arrays.asList(1, 2).equals(getVersionsReader().getRevisions(ParametrizedCustomTypeEntity.class, pcte_id)); + } + + @Test + public void testHistoryOfCcte() { + ParametrizedCustomTypeEntity rev1 = getVersionsReader().find(ParametrizedCustomTypeEntity.class, pcte_id, 1); + ParametrizedCustomTypeEntity rev2 = getVersionsReader().find(ParametrizedCustomTypeEntity.class, pcte_id, 2); + + assert "xUy".equals(rev1.getStr()); + assert "xVy".equals(rev2.getStr()); + } +} \ No newline at end of file From jboss-envers-commits at lists.jboss.org Mon Sep 22 17:20:21 2008 From: jboss-envers-commits at lists.jboss.org (jboss-envers-commits at lists.jboss.org) Date: Mon, 22 Sep 2008 17:20:21 -0400 Subject: [jboss-envers-commits] JBoss Envers SVN: r164 - in trunk: src/main/org/jboss/envers/configuration and 5 other directories. Message-ID: Author: adamw Date: 2008-09-22 17:20:21 -0400 (Mon, 22 Sep 2008) New Revision: 164 Removed: trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToOneNotOwningIdMapper.java Modified: trunk/envers.iml trunk/src/main/org/jboss/envers/configuration/EntitiesConfigurator.java trunk/src/main/org/jboss/envers/configuration/VersionsEntitiesConfiguration.java trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java trunk/src/main/org/jboss/envers/entities/EntityConfiguration.java trunk/src/main/org/jboss/envers/entities/mapper/SinglePropertyMapper.java trunk/src/main/org/jboss/envers/tools/ArraysTools.java trunk/src/main/org/jboss/envers/tools/MutableBoolean.java trunk/src/main/org/jboss/envers/tools/MutableInteger.java trunk/src/main/org/jboss/envers/tools/Pair.java trunk/src/main/org/jboss/envers/tools/Triple.java Log: Removing dead code Modified: trunk/envers.iml =================================================================== --- trunk/envers.iml 2008-09-22 20:30:29 UTC (rev 163) +++ trunk/envers.iml 2008-09-22 21:20:21 UTC (rev 164) @@ -51,6 +51,7 @@ + Modified: trunk/src/main/org/jboss/envers/configuration/EntitiesConfigurator.java =================================================================== --- trunk/src/main/org/jboss/envers/configuration/EntitiesConfigurator.java 2008-09-22 20:30:29 UTC (rev 163) +++ trunk/src/main/org/jboss/envers/configuration/EntitiesConfigurator.java 2008-09-22 21:20:21 UTC (rev 164) @@ -95,12 +95,12 @@ try { cfg.addDocument(writer.write(xmlMappingData.getMainXmlMapping())); // TODO - writeDocument(xmlMappingData.getMainXmlMapping()); + //writeDocument(xmlMappingData.getMainXmlMapping()); for (Document additionalMapping : xmlMappingData.getAdditionalXmlMappings()) { cfg.addDocument(writer.write(additionalMapping)); // TODO - writeDocument(additionalMapping); + //writeDocument(additionalMapping); } } catch (DocumentException e) { throw new MappingException(e); Modified: trunk/src/main/org/jboss/envers/configuration/VersionsEntitiesConfiguration.java =================================================================== --- trunk/src/main/org/jboss/envers/configuration/VersionsEntitiesConfiguration.java 2008-09-22 20:30:29 UTC (rev 163) +++ trunk/src/main/org/jboss/envers/configuration/VersionsEntitiesConfiguration.java 2008-09-22 21:20:21 UTC (rev 164) @@ -97,15 +97,6 @@ return versionsTablePrefix + entityName + versionsTableSuffix; } - public boolean isVersionsEntityName(String entityName) { - if (entityName == null) { - return false; - } - - return entityName.endsWith(versionsTableSuffix) && - entityName.startsWith(versionsTablePrefix); - } - public String getVersionsTableName(String entityName, String tableName) { String customHistoryTableName = customVersionsTablesNames.get(entityName); if (customHistoryTableName == null) { Modified: trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java =================================================================== --- trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java 2008-09-22 20:30:29 UTC (rev 163) +++ trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java 2008-09-22 21:20:21 UTC (rev 164) @@ -190,7 +190,7 @@ mappedBy = getMappedBy(propertyValue.getCollectionTable(), mainGenerator.getCfg().getClassMapping(referencedEntityName)); referencingPrefixRelated = mappedBy + "_"; - referencedPrefix = referencedEntityName == null ? "element" : StringTools.getLastComponent(referencedEntityName); + referencedPrefix = StringTools.getLastComponent(referencedEntityName); } else { mappedBy = null; Modified: trunk/src/main/org/jboss/envers/entities/EntityConfiguration.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/EntityConfiguration.java 2008-09-22 20:30:29 UTC (rev 163) +++ trunk/src/main/org/jboss/envers/entities/EntityConfiguration.java 2008-09-22 21:20:21 UTC (rev 164) @@ -97,10 +97,6 @@ return propertyMapper; } - public String getEntityName() { - return entityName; - } - // For use by EntitiesConfigurations String getParentEntityName() { Modified: trunk/src/main/org/jboss/envers/entities/mapper/SinglePropertyMapper.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/SinglePropertyMapper.java 2008-09-22 20:30:29 UTC (rev 163) +++ trunk/src/main/org/jboss/envers/entities/mapper/SinglePropertyMapper.java 2008-09-22 21:20:21 UTC (rev 164) @@ -41,10 +41,6 @@ public class SinglePropertyMapper implements PropertyMapper, SimpleMapperBuilder { private String propertyName; - public SinglePropertyMapper(String propertyName, ModificationStore modStore) { - this.propertyName = propertyName; - } - public SinglePropertyMapper() { } public void add(String propertyName, ModificationStore modStore) { Deleted: trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToOneNotOwningIdMapper.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToOneNotOwningIdMapper.java 2008-09-22 20:30:29 UTC (rev 163) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/OneToOneNotOwningIdMapper.java 2008-09-22 21:20:21 UTC (rev 164) @@ -1,87 +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.tools.reflection.ReflectionTools; -import org.jboss.envers.query.VersionsRestrictions; -import org.jboss.envers.exception.VersionsException; -import org.jboss.envers.configuration.VersionsConfiguration; -import org.hibernate.property.Setter; -import org.hibernate.NonUniqueResultException; -import org.hibernate.collection.PersistentCollection; - -import javax.persistence.NoResultException; -import java.util.Map; -import java.util.List; -import java.io.Serializable; - -/** - * @author Adam Warski (adam at warski dot org) - */ -public class OneToOneNotOwningIdMapper implements PropertyMapper { - private String owningReferencePropertyName; - private String owningEntityName; - private String propertyName; - - public OneToOneNotOwningIdMapper(String owningReferencePropertyName, String owningEntityName, String propertyName) { - this.owningReferencePropertyName = owningReferencePropertyName; - this.owningEntityName = owningEntityName; - this.propertyName = propertyName; - } - - public boolean mapToMapFromEntity(Map data, Object newObj, Object oldObj) { - return false; - } - - public void mapToEntityFromMap(VersionsConfiguration verCfg, Object obj, Map data, Object primaryKey, VersionsReaderImplementor versionsReader, Number revision) { - if (obj == null) { - return; - } - - Class entityClass = ReflectionTools.loadClass(owningEntityName); - - Object value; - - try { - value = versionsReader.createQuery().forEntitiesAtRevision(entityClass, revision) - .add(VersionsRestrictions.relatedIdEq(owningReferencePropertyName, primaryKey)).getSingleResult(); - } catch (NoResultException e) { - value = null; - } catch (NonUniqueResultException e) { - throw new VersionsException("Many versions results for one-to-one relationship: (" + owningEntityName + - ", " + owningReferencePropertyName + ")"); - } - - Setter setter = ReflectionTools.getSetter(obj.getClass(), propertyName); - setter.set(obj, value, null); - } - - public List mapCollectionChanges(String referencingPropertyName, - PersistentCollection newColl, - Serializable oldColl, - Serializable id) { - return null; - } -} Modified: trunk/src/main/org/jboss/envers/tools/ArraysTools.java =================================================================== --- trunk/src/main/org/jboss/envers/tools/ArraysTools.java 2008-09-22 20:30:29 UTC (rev 163) +++ trunk/src/main/org/jboss/envers/tools/ArraysTools.java 2008-09-22 21:20:21 UTC (rev 164) @@ -34,16 +34,4 @@ return false; } - - public static int indexInArray(T[] array, T object) { - if (array != null) { - for (int i=0; i Author: adamw Date: 2008-09-23 13:05:36 -0400 (Tue, 23 Sep 2008) New Revision: 165 Added: trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleMapKeyIdComponentMapper.java trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleMapKeyPropertyComponentMapper.java trunk/src/test/org/jboss/envers/test/entities/components/ trunk/src/test/org/jboss/envers/test/entities/components/Component1.java trunk/src/test/org/jboss/envers/test/entities/components/Component2.java trunk/src/test/org/jboss/envers/test/entities/components/ComponentTestEntity.java trunk/src/test/org/jboss/envers/test/integration/collection/mapkey/ trunk/src/test/org/jboss/envers/test/integration/collection/mapkey/ComponentMapKey.java trunk/src/test/org/jboss/envers/test/integration/collection/mapkey/ComponentMapKeyEntity.java trunk/src/test/org/jboss/envers/test/integration/collection/mapkey/IdMapKey.java trunk/src/test/org/jboss/envers/test/integration/collection/mapkey/IdMapKeyEntity.java Modified: trunk/resources/test/testng.xml trunk/src/main/org/jboss/envers/configuration/metadata/AnnotationsMetadataReader.java trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java trunk/src/main/org/jboss/envers/configuration/metadata/PersistentClassVersioningData.java trunk/src/main/org/jboss/envers/configuration/metadata/VersionsMetadataGenerator.java trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleComponentMapper.java trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleDummyComponentMapper.java trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleRelatedComponentMapper.java trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleSimpleComponentMapper.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/test/org/jboss/envers/test/integration/components/Components.java Log: ENVERS-25: support for @MapKey Modified: trunk/resources/test/testng.xml =================================================================== --- trunk/resources/test/testng.xml 2008-09-22 21:20:21 UTC (rev 164) +++ trunk/resources/test/testng.xml 2008-09-23 17:05:36 UTC (rev 165) @@ -4,7 +4,8 @@ - + + Modified: trunk/src/main/org/jboss/envers/configuration/metadata/AnnotationsMetadataReader.java =================================================================== --- trunk/src/main/org/jboss/envers/configuration/metadata/AnnotationsMetadataReader.java 2008-09-22 21:20:21 UTC (rev 164) +++ trunk/src/main/org/jboss/envers/configuration/metadata/AnnotationsMetadataReader.java 2008-09-23 17:05:36 UTC (rev 165) @@ -22,6 +22,7 @@ package org.jboss.envers.configuration.metadata; import javax.persistence.Version; +import javax.persistence.MapKey; import org.jboss.envers.configuration.GlobalConfiguration; import org.jboss.envers.tools.reflection.YClass; @@ -65,6 +66,13 @@ } } + private void addPropertyMapKey(YProperty property) { + MapKey mapKey = property.getAnnotation(MapKey.class); + if (mapKey != null) { + versioningData.mapKeys.put(property.getName(), mapKey.name()); + } + } + private void addPropertyUnversioned(YProperty property) { // check if a property is declared as unversioned to exclude it // useful if a class is versioned but some properties should be excluded @@ -95,6 +103,7 @@ addPropertyVersioned(property); addPropertyUnversioned(property); addPropertyJoinTables(property); + addPropertyMapKey(property); } } Modified: trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java =================================================================== --- trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java 2008-09-22 21:20:21 UTC (rev 164) +++ trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java 2008-09-23 17:05:36 UTC (rev 165) @@ -8,9 +8,7 @@ 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.component.MiddleRelatedComponentMapper; -import org.jboss.envers.entities.mapper.relation.component.MiddleSimpleComponentMapper; -import org.jboss.envers.entities.mapper.relation.component.MiddleDummyComponentMapper; +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; @@ -39,6 +37,7 @@ private final String referencingEntityName; private final EntityXmlMappingData xmlMappingData; private final VersionsJoinTable joinTable; + private final String mapKey; private final EntityConfiguration referencingEntityConfiguration; /** @@ -56,11 +55,13 @@ * @param xmlMappingData In case this collection requires a middle table, additional mapping documents will * be created using this object. * @param joinTable User data for the middle (join) table. null if the user didn't specify it. + * @param mapKey The value of the name() property of the MapKey annotation on this property. Null, if this + * property isn't annotated with this annotation. */ public CollectionMetadataGenerator(VersionsMetadataGenerator mainGenerator, String propertyName, Collection propertyValue, CompositeMapperBuilder currentMapper, String referencingEntityName, EntityXmlMappingData xmlMappingData, - VersionsJoinTable joinTable) { + VersionsJoinTable joinTable, String mapKey) { this.mainGenerator = mainGenerator; this.propertyName = propertyName; this.propertyValue = propertyValue; @@ -68,6 +69,7 @@ this.referencingEntityName = referencingEntityName; this.xmlMappingData = xmlMappingData; this.joinTable = joinTable == null ? getDefaultVersionsJoinTable() : joinTable; + this.mapKey = mapKey; referencingEntityConfiguration = mainGenerator.getEntitiesConfigurations().get(referencingEntityName); if (referencingEntityConfiguration == null) { @@ -226,19 +228,9 @@ queryGeneratorBuilder, referencedPrefix, joinTable.inverseJoinColumns()); // ****** - // Optionally, generating the index mapping. + // Generating the index mapping, if an index exists. // ****** - MiddleComponentData indexComponentData; - if (propertyValue instanceof IndexedCollection) { - IndexedCollection indexedValue = (IndexedCollection) propertyValue; - indexComponentData = addValueToMiddleTable(indexedValue.getIndex(), middleEntityXml, - queryGeneratorBuilder, "mapkey", null); - // TODO: @MapKey support, @MapKeyManyToMany - } else { - // No index - creating a dummy mapper. - indexComponentData = new MiddleComponentData(new MiddleDummyComponentMapper(), - queryGeneratorBuilder.getCurrentIndex()); - } + MiddleComponentData indexComponentData = addIndex(middleEntityXml, queryGeneratorBuilder); // ****** // Generating the property mapper. @@ -259,6 +251,34 @@ storeMiddleEntityRelationInformation(mappedBy); } + private MiddleComponentData addIndex(Element middleEntityXml, QueryGeneratorBuilder queryGeneratorBuilder) { + if (propertyValue instanceof IndexedCollection) { + IndexedCollection indexedValue = (IndexedCollection) propertyValue; + if (mapKey == null) { + // This entity doesn't specify a javax.persistence.MapKey. Mapping it to the middle entity. + return addValueToMiddleTable(indexedValue.getIndex(), middleEntityXml, + queryGeneratorBuilder, "mapkey", null); + } else { + IdMappingData referencedIdMapping = mainGenerator.getEntitiesConfigurations() + .get(referencedEntityName).getIdMappingData(); + 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()); + } else { + // The key of the map is a property of the entity. + return new MiddleComponentData(new MiddleMapKeyPropertyComponentMapper(mapKey), + queryGeneratorBuilder.getCurrentIndex()); + } + } + // TODO: @MapKeyManyToMany + } else { + // No index - creating a dummy mapper. + return new MiddleComponentData(new MiddleDummyComponentMapper(), + queryGeneratorBuilder.getCurrentIndex()); + } + } + /** * * @param value Value, which should be mapped to the middle-table, either as a relation to another entity, @@ -279,8 +299,8 @@ String prefixRelated = prefix + "_"; String referencedEntityName = getReferencedEntityName(value); - IdMappingData referencedIdMapping = mainGenerator.getEntitiesConfigurations().get( - referencedEntityName).getIdMappingData(); + IdMappingData referencedIdMapping = mainGenerator.getEntitiesConfigurations() + .get(referencedEntityName).getIdMappingData(); // Adding related-entity (in this case: the referenced entities id) id mapping to the xml only if the // relation isn't inverse (so when xmlMapping is not null). @@ -305,8 +325,8 @@ return new MiddleComponentData(new MiddleRelatedComponentMapper(referencedIdData), queryGeneratorBuilder.getCurrentIndex()); } else { - boolean mapped = mainGenerator.getBasicMetadataGenerator().addBasic(xmlMapping, prefix, value, null, - ModificationStore.FULL, referencingEntityName, true); + boolean mapped = mainGenerator.getBasicMetadataGenerator().addBasicNoComponent(xmlMapping, prefix, value, null, + ModificationStore.FULL, true); if (mapped) { // Simple values are always stored in the first item of the array returned by the query generator. @@ -314,7 +334,7 @@ } else { mainGenerator.throwUnsupportedTypeException(type, referencingEntityName, propertyName); // Impossible to get here. - throw new AssertionError(); + throw new AssertionError(); } } } Modified: trunk/src/main/org/jboss/envers/configuration/metadata/PersistentClassVersioningData.java =================================================================== --- trunk/src/main/org/jboss/envers/configuration/metadata/PersistentClassVersioningData.java 2008-09-22 21:20:21 UTC (rev 164) +++ trunk/src/main/org/jboss/envers/configuration/metadata/PersistentClassVersioningData.java 2008-09-23 17:05:36 UTC (rev 165) @@ -40,6 +40,7 @@ secondaryTableDictionary = new HashMap(); unversionedProperties = new ArrayList(); versionsJoinTables = new HashMap(); + mapKeys = new HashMap(); } public PropertyStoreInfo propertyStoreInfo; @@ -50,6 +51,11 @@ * A map from property names to custom join tables definitions. */ public Map versionsJoinTables; + /** + * A map from property names to the value of the related property names in a map key annotation. An empty string, + * if the property name is not specified in the mapkey annotation. + */ + public Map mapKeys; 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-09-22 21:20:21 UTC (rev 164) +++ trunk/src/main/org/jboss/envers/configuration/metadata/VersionsMetadataGenerator.java 2008-09-23 17:05:36 UTC (rev 165) @@ -119,7 +119,7 @@ @SuppressWarnings({"unchecked"}) void addValue(Element parent, String name, Value value, CompositeMapperBuilder currentMapper, ModificationStore store, String entityName, EntityXmlMappingData xmlMappingData, - VersionsJoinTable joinTable, boolean firstPass) { + VersionsJoinTable joinTable, String mapKey, boolean firstPass) { Type type = value.getType(); // only first pass @@ -144,7 +144,7 @@ // only second pass if (!firstPass) { CollectionMetadataGenerator collectionMetadataGenerator = new CollectionMetadataGenerator(this, - name, (Collection) value, currentMapper, entityName, xmlMappingData, joinTable); + name, (Collection) value, currentMapper, entityName, xmlMappingData, joinTable, mapKey); collectionMetadataGenerator.addCollection(); } } else { @@ -168,7 +168,8 @@ if (store != null) { addValue(parent, property.getName(), property.getValue(), currentMapper, store, entityName, - xmlMappingData, versioningData.versionsJoinTables.get(property.getName()), firstPass); + xmlMappingData, versioningData.versionsJoinTables.get(property.getName()), + versioningData.mapKeys.get(property.getName()), firstPass); } } } Modified: trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleComponentMapper.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleComponentMapper.java 2008-09-22 21:20:21 UTC (rev 164) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleComponentMapper.java 2008-09-23 17:05:36 UTC (rev 165) @@ -10,14 +10,16 @@ */ public interface MiddleComponentMapper { /** - * Maps from full object data, contained in the given map, to an object. + * Maps from full object data, contained in the given map (or object representation of the map, if + * available), to an object. * @param entityInstantiator An entity instatiator bound with an open versions reader. * @param data Full object data. + * @param dataObject An optional object representation of the data. * @param revision Revision at which the data is read. * @return An object with data corresponding to the one found in the given map. */ Object mapToObjectFromFullMap(EntityInstantiator entityInstantiator, Map data, - Number revision); + Object dataObject, Number revision); /** * Maps from an object to the object's map representation (for an entity - only its id). Modified: trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleDummyComponentMapper.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleDummyComponentMapper.java 2008-09-22 21:20:21 UTC (rev 164) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleDummyComponentMapper.java 2008-09-23 17:05:36 UTC (rev 165) @@ -9,7 +9,8 @@ * @author Adam Warski (adam at warski dot org) */ public final class MiddleDummyComponentMapper implements MiddleComponentMapper { - public Object mapToObjectFromFullMap(EntityInstantiator entityInstantiator, Map data, Number revision) { + public Object mapToObjectFromFullMap(EntityInstantiator entityInstantiator, Map data, + Object dataObject, Number revision) { return null; } Copied: trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleMapKeyIdComponentMapper.java (from rev 154, trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleMapKeyComponentMapper.java) =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleMapKeyIdComponentMapper.java (rev 0) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleMapKeyIdComponentMapper.java 2008-09-23 17:05:36 UTC (rev 165) @@ -0,0 +1,37 @@ +package org.jboss.envers.entities.mapper.relation.component; + +import org.jboss.envers.entities.EntityInstantiator; +import org.jboss.envers.entities.mapper.id.IdMapper; +import org.jboss.envers.tools.query.Parameters; +import org.jboss.envers.configuration.VersionsEntitiesConfiguration; + +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 mapToMapFromObject is + * empty. + * @author Adam Warski (adam at warski dot org) + */ +public final class MiddleMapKeyIdComponentMapper implements MiddleComponentMapper { + private final VersionsEntitiesConfiguration verEntCfg; + private final IdMapper relatedIdMapper; + + public MiddleMapKeyIdComponentMapper(VersionsEntitiesConfiguration verEntCfg, IdMapper relatedIdMapper) { + this.verEntCfg = verEntCfg; + this.relatedIdMapper = relatedIdMapper; + } + + public Object mapToObjectFromFullMap(EntityInstantiator entityInstantiator, Map data, + Object dataObject, Number revision) { + return relatedIdMapper.mapToIdFromMap((Map) data.get(verEntCfg.getOriginalIdPropName())); + } + + public void mapToMapFromObject(Map data, Object obj) { + // Doing nothing. + } + + public void addMiddleEqualToQuery(Parameters parameters, String prefix1, String prefix2) { + // Doing nothing. + } +} \ No newline at end of file Added: trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleMapKeyPropertyComponentMapper.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleMapKeyPropertyComponentMapper.java (rev 0) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleMapKeyPropertyComponentMapper.java 2008-09-23 17:05:36 UTC (rev 165) @@ -0,0 +1,35 @@ +package org.jboss.envers.entities.mapper.relation.component; + +import org.jboss.envers.entities.EntityInstantiator; +import org.jboss.envers.tools.query.Parameters; +import org.jboss.envers.tools.reflection.ReflectionTools; + +import java.util.Map; + +/** + * A component mapper for the @MapKey mapping with the name parameter specified: the value of the map's key + * is a property of the entity. This doesn't have an effect on the data stored in the versions tables, + * so mapToMapFromObject is empty. + * @author Adam Warski (adam at warski dot org) + */ +public final class MiddleMapKeyPropertyComponentMapper implements MiddleComponentMapper { + private final String propertyName; + + public MiddleMapKeyPropertyComponentMapper(String propertyName) { + this.propertyName = propertyName; + } + + public Object mapToObjectFromFullMap(EntityInstantiator entityInstantiator, Map data, + Object dataObject, Number revision) { + // dataObject is not null, as this mapper can only be used in an index. + return ReflectionTools.getGetter(dataObject.getClass(), propertyName).get(dataObject); + } + + public void mapToMapFromObject(Map data, Object obj) { + // Doing nothing. + } + + public void addMiddleEqualToQuery(Parameters parameters, String prefix1, String prefix2) { + // Doing nothing. + } +} \ No newline at end of file Modified: trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleRelatedComponentMapper.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleRelatedComponentMapper.java 2008-09-22 21:20:21 UTC (rev 164) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleRelatedComponentMapper.java 2008-09-23 17:05:36 UTC (rev 165) @@ -17,7 +17,7 @@ } public Object mapToObjectFromFullMap(EntityInstantiator entityInstantiator, Map data, - Number revision) { + Object dataObject, Number revision) { return entityInstantiator.createInstanceFromVersionsEntity(relatedIdData.getEntityName(), data, revision); } Modified: trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleSimpleComponentMapper.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleSimpleComponentMapper.java 2008-09-22 21:20:21 UTC (rev 164) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleSimpleComponentMapper.java 2008-09-23 17:05:36 UTC (rev 165) @@ -19,7 +19,8 @@ } @SuppressWarnings({"unchecked"}) - public Object mapToObjectFromFullMap(EntityInstantiator entityInstantiator, Map data, Number revision) { + public Object mapToObjectFromFullMap(EntityInstantiator entityInstantiator, Map data, + Object dataObject, Number revision) { return ((Map) data.get(verEntCfg.getOriginalIdPropName())).get(propertyName); } 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-22 21:20:21 UTC (rev 164) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/ArrayCollectionInitializor.java 2008-09-23 17:05:36 UTC (rev 165) @@ -35,11 +35,11 @@ protected void addToCollection(Object[] collection, Object collectionRow) { Object elementData = ((List) collectionRow).get(elementComponentData.getComponentIndex()); Object element = elementComponentData.getComponentMapper().mapToObjectFromFullMap(entityInstantiator, - (Map) elementData, revision); + (Map) elementData, null, revision); Object indexData = ((List) collectionRow).get(indexComponentData.getComponentIndex()); Object indexObj = indexComponentData.getComponentMapper().mapToObjectFromFullMap(entityInstantiator, - (Map) indexData, revision); + (Map) indexData, element, revision); int index = ((Number) indexObj).intValue(); collection[index] = element; 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-22 21:20:21 UTC (rev 164) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/BasicCollectionInitializor.java 2008-09-23 17:05:36 UTC (rev 165) @@ -42,7 +42,7 @@ protected void addToCollection(T collection, Object collectionRow) { Object elementData = ((List) collectionRow).get(elementComponentData.getComponentIndex()); Object element = elementComponentData.getComponentMapper().mapToObjectFromFullMap(entityInstantiator, - (Map) elementData, revision); + (Map) elementData, null, revision); collection.add(element); } } 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-22 21:20:21 UTC (rev 164) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/ListCollectionInitializor.java 2008-09-23 17:05:36 UTC (rev 165) @@ -35,11 +35,11 @@ protected void addToCollection(List collection, Object collectionRow) { Object elementData = ((List) collectionRow).get(elementComponentData.getComponentIndex()); Object element = elementComponentData.getComponentMapper().mapToObjectFromFullMap(entityInstantiator, - (Map) elementData, revision); + (Map) elementData, null, revision); Object indexData = ((List) collectionRow).get(indexComponentData.getComponentIndex()); Object indexObj = indexComponentData.getComponentMapper().mapToObjectFromFullMap(entityInstantiator, - (Map) indexData, revision); + (Map) indexData, element, revision); int index = ((Number) indexObj).intValue(); collection.set(index, element); 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-22 21:20:21 UTC (rev 164) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/MapCollectionInitializor.java 2008-09-23 17:05:36 UTC (rev 165) @@ -45,11 +45,11 @@ protected void addToCollection(T collection, Object collectionRow) { Object elementData = ((List) collectionRow).get(elementComponentData.getComponentIndex()); Object element = elementComponentData.getComponentMapper().mapToObjectFromFullMap(entityInstantiator, - (Map) elementData, revision); + (Map) elementData, null, revision); Object indexData = ((List) collectionRow).get(indexComponentData.getComponentIndex()); Object index = indexComponentData.getComponentMapper().mapToObjectFromFullMap(entityInstantiator, - (Map) indexData, revision); + (Map) indexData, element, revision); collection.put(index, element); } Copied: trunk/src/test/org/jboss/envers/test/entities/components/Component1.java (from rev 148, trunk/src/test/org/jboss/envers/test/integration/components/Component1.java) =================================================================== --- trunk/src/test/org/jboss/envers/test/entities/components/Component1.java (rev 0) +++ trunk/src/test/org/jboss/envers/test/entities/components/Component1.java 2008-09-23 17:05:36 UTC (rev 165) @@ -0,0 +1,57 @@ +package org.jboss.envers.test.entities.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; + } + + public String toString() { + return "Comp1(str1 = " + str1 + ", " + str2 + ")"; + } +} Copied: trunk/src/test/org/jboss/envers/test/entities/components/Component2.java (from rev 148, trunk/src/test/org/jboss/envers/test/integration/components/Component2.java) =================================================================== --- trunk/src/test/org/jboss/envers/test/entities/components/Component2.java (rev 0) +++ trunk/src/test/org/jboss/envers/test/entities/components/Component2.java 2008-09-23 17:05:36 UTC (rev 165) @@ -0,0 +1,57 @@ +package org.jboss.envers.test.entities.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; + } + + public String toString() { + return "Comp2(str1 = " + str5 + ", " + str6 + ")"; + } +} Copied: trunk/src/test/org/jboss/envers/test/entities/components/ComponentTestEntity.java (from rev 148, trunk/src/test/org/jboss/envers/test/integration/components/ComponentTestEntity.java) =================================================================== --- trunk/src/test/org/jboss/envers/test/entities/components/ComponentTestEntity.java (rev 0) +++ trunk/src/test/org/jboss/envers/test/entities/components/ComponentTestEntity.java 2008-09-23 17:05:36 UTC (rev 165) @@ -0,0 +1,89 @@ +package org.jboss.envers.test.entities.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) + */ + at 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; + } + + public String toString() { + return "CTE(id = " + id + ", comp1 = " + comp1 + ", comp2 = " + comp2 + ")"; + } +} + Added: trunk/src/test/org/jboss/envers/test/integration/collection/mapkey/ComponentMapKey.java =================================================================== --- trunk/src/test/org/jboss/envers/test/integration/collection/mapkey/ComponentMapKey.java (rev 0) +++ trunk/src/test/org/jboss/envers/test/integration/collection/mapkey/ComponentMapKey.java 2008-09-23 17:05:36 UTC (rev 165) @@ -0,0 +1,88 @@ +package org.jboss.envers.test.integration.collection.mapkey; + +import org.jboss.envers.test.integration.AbstractEntityTest; +import org.jboss.envers.test.tools.TestTools; +import org.jboss.envers.test.entities.components.ComponentTestEntity; +import org.jboss.envers.test.entities.components.Component1; +import org.jboss.envers.test.entities.components.Component2; +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 ComponentMapKey extends AbstractEntityTest { + private Integer cmke_id; + + private Integer cte1_id; + private Integer cte2_id; + + public void configure(Ejb3Configuration cfg) { + cfg.addAnnotatedClass(ComponentMapKeyEntity.class); + cfg.addAnnotatedClass(ComponentTestEntity.class); + } + + @BeforeClass(dependsOnMethods = "init") + public void initData() { + EntityManager em = getEntityManager(); + + ComponentMapKeyEntity imke = new ComponentMapKeyEntity(); + + // Revision 1 (intialy 1 mapping) + em.getTransaction().begin(); + + ComponentTestEntity cte1 = new ComponentTestEntity(new Component1("x1", "y2"), new Component2("a1", "b2")); + ComponentTestEntity cte2 = new ComponentTestEntity(new Component1("x1", "y2"), new Component2("a1", "b2")); + + em.persist(cte1); + em.persist(cte2); + + imke.getIdmap().put(cte1.getComp1(), cte1); + + em.persist(imke); + + em.getTransaction().commit(); + + // Revision 2 (sse1: adding 1 mapping) + em.getTransaction().begin(); + + cte2 = em.find(ComponentTestEntity.class, cte2.getId()); + imke = em.find(ComponentMapKeyEntity.class, imke.getId()); + + imke.getIdmap().put(cte2.getComp1(), cte2); + + em.getTransaction().commit(); + + // + + cmke_id = imke.getId(); + + cte1_id = cte1.getId(); + cte2_id = cte2.getId(); + } + + @Test + public void testRevisionsCounts() { + assert Arrays.asList(1, 2).equals(getVersionsReader().getRevisions(ComponentMapKeyEntity.class, cmke_id)); + } + + @Test + public void testHistoryOfImke() { + ComponentTestEntity cte1 = getEntityManager().find(ComponentTestEntity.class, cte1_id); + ComponentTestEntity cte2 = getEntityManager().find(ComponentTestEntity.class, cte2_id); + + // These fields are unversioned. + cte1.setComp2(null); + cte2.setComp2(null); + + ComponentMapKeyEntity rev1 = getVersionsReader().find(ComponentMapKeyEntity.class, cmke_id, 1); + ComponentMapKeyEntity rev2 = getVersionsReader().find(ComponentMapKeyEntity.class, cmke_id, 2); + + assert rev1.getIdmap().equals(TestTools.makeMap(cte1.getComp1(), cte1)); + assert rev2.getIdmap().equals(TestTools.makeMap(cte1.getComp1(), cte1, cte2.getComp1(), cte2)); + } +} \ No newline at end of file Added: trunk/src/test/org/jboss/envers/test/integration/collection/mapkey/ComponentMapKeyEntity.java =================================================================== --- trunk/src/test/org/jboss/envers/test/integration/collection/mapkey/ComponentMapKeyEntity.java (rev 0) +++ trunk/src/test/org/jboss/envers/test/integration/collection/mapkey/ComponentMapKeyEntity.java 2008-09-23 17:05:36 UTC (rev 165) @@ -0,0 +1,63 @@ +package org.jboss.envers.test.integration.collection.mapkey; + +import org.jboss.envers.Versioned; +import org.jboss.envers.test.entities.components.Component1; +import org.jboss.envers.test.entities.components.ComponentTestEntity; + +import javax.persistence.*; +import java.util.Map; +import java.util.HashMap; + +/** + * @author Adam Warski (adam at warski dot org) + */ + at Entity +public class ComponentMapKeyEntity { + @Id + @GeneratedValue + private Integer id; + + @Versioned + @ManyToMany + @MapKey(name = "comp1") + private Map idmap; + + public ComponentMapKeyEntity() { + idmap = new HashMap(); + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public Map getIdmap() { + return idmap; + } + + public void setIdmap(Map idmap) { + this.idmap = idmap; + } + + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof ComponentMapKeyEntity)) return false; + + ComponentMapKeyEntity that = (ComponentMapKeyEntity) 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 "CMKE(id = " + id + ", idmap = " + idmap + ")"; + } +} \ No newline at end of file Copied: trunk/src/test/org/jboss/envers/test/integration/collection/mapkey/IdMapKey.java (from rev 154, trunk/src/test/org/jboss/envers/test/integration/collection/StringSet.java) =================================================================== --- trunk/src/test/org/jboss/envers/test/integration/collection/mapkey/IdMapKey.java (rev 0) +++ trunk/src/test/org/jboss/envers/test/integration/collection/mapkey/IdMapKey.java 2008-09-23 17:05:36 UTC (rev 165) @@ -0,0 +1,83 @@ +package org.jboss.envers.test.integration.collection.mapkey; + +import org.jboss.envers.test.integration.AbstractEntityTest; +import org.jboss.envers.test.tools.TestTools; +import org.jboss.envers.test.entities.StrTestEntity; +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 IdMapKey extends AbstractEntityTest { + private Integer imke_id; + + private Integer ste1_id; + private Integer ste2_id; + + public void configure(Ejb3Configuration cfg) { + cfg.addAnnotatedClass(IdMapKeyEntity.class); + cfg.addAnnotatedClass(StrTestEntity.class); + } + + @BeforeClass(dependsOnMethods = "init") + public void initData() { + EntityManager em = getEntityManager(); + + IdMapKeyEntity imke = new IdMapKeyEntity(); + + // Revision 1 (intialy 1 mapping) + em.getTransaction().begin(); + + StrTestEntity ste1 = new StrTestEntity("x"); + StrTestEntity ste2 = new StrTestEntity("y"); + + em.persist(ste1); + em.persist(ste2); + + imke.getIdmap().put(ste1.getId(), ste1); + + em.persist(imke); + + em.getTransaction().commit(); + + // Revision 2 (sse1: adding 1 mapping) + em.getTransaction().begin(); + + ste2 = em.find(StrTestEntity.class, ste2.getId()); + imke = em.find(IdMapKeyEntity.class, imke.getId()); + + imke.getIdmap().put(ste2.getId(), ste2); + + em.getTransaction().commit(); + + // + + imke_id = imke.getId(); + + ste1_id = ste1.getId(); + ste2_id = ste2.getId(); + } + + @Test + public void testRevisionsCounts() { + assert Arrays.asList(1, 2).equals(getVersionsReader().getRevisions(IdMapKeyEntity.class, imke_id)); + } + + @Test + public void testHistoryOfImke() { + StrTestEntity ste1 = getEntityManager().find(StrTestEntity.class, ste1_id); + StrTestEntity ste2 = getEntityManager().find(StrTestEntity.class, ste2_id); + + 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)); + } +} \ No newline at end of file Copied: trunk/src/test/org/jboss/envers/test/integration/collection/mapkey/IdMapKeyEntity.java (from rev 148, trunk/src/test/org/jboss/envers/test/entities/StrIntTestEntity.java) =================================================================== --- trunk/src/test/org/jboss/envers/test/integration/collection/mapkey/IdMapKeyEntity.java (rev 0) +++ trunk/src/test/org/jboss/envers/test/integration/collection/mapkey/IdMapKeyEntity.java 2008-09-23 17:05:36 UTC (rev 165) @@ -0,0 +1,62 @@ +package org.jboss.envers.test.integration.collection.mapkey; + +import org.jboss.envers.Versioned; +import org.jboss.envers.test.entities.StrTestEntity; + +import javax.persistence.*; +import java.util.Map; +import java.util.HashMap; + +/** + * @author Adam Warski (adam at warski dot org) + */ + at Entity +public class IdMapKeyEntity { + @Id + @GeneratedValue + private Integer id; + + @Versioned + @ManyToMany + @MapKey + private Map idmap; + + public IdMapKeyEntity() { + idmap = new HashMap(); + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public Map getIdmap() { + return idmap; + } + + public void setIdmap(Map idmap) { + this.idmap = idmap; + } + + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof IdMapKeyEntity)) return false; + + IdMapKeyEntity that = (IdMapKeyEntity) 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 "IMKE(id = " + id + ", idmap = " + idmap + ")"; + } +} \ No newline at end of file Property changes on: trunk/src/test/org/jboss/envers/test/integration/collection/mapkey/IdMapKeyEntity.java ___________________________________________________________________ Name: svn:mergeinfo + Modified: trunk/src/test/org/jboss/envers/test/integration/components/Components.java =================================================================== --- trunk/src/test/org/jboss/envers/test/integration/components/Components.java 2008-09-22 21:20:21 UTC (rev 164) +++ trunk/src/test/org/jboss/envers/test/integration/components/Components.java 2008-09-23 17:05:36 UTC (rev 165) @@ -1,6 +1,9 @@ package org.jboss.envers.test.integration.components; import org.jboss.envers.test.integration.AbstractEntityTest; +import org.jboss.envers.test.entities.components.Component1; +import org.jboss.envers.test.entities.components.Component2; +import org.jboss.envers.test.entities.components.ComponentTestEntity; import org.hibernate.ejb.Ejb3Configuration; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; From jboss-envers-commits at lists.jboss.org Wed Sep 24 06:42:17 2008 From: jboss-envers-commits at lists.jboss.org (jboss-envers-commits at lists.jboss.org) Date: Wed, 24 Sep 2008 06:42:17 -0400 Subject: [jboss-envers-commits] JBoss Envers SVN: r166 - in trunk/src: main/org/jboss/envers/entities/mapper/relation and 7 other directories. Message-ID: 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 data, Object newObj, Object oldObj) { - return false; - } - - protected Initializor getInitializator(VersionsReaderImplementor versionsReader, - Class entityClass, Object primaryKey, - Number revision, - Class collectionClass) { - return new OneToManyAttachedInitializor(versionsReader, entityClass, owningReferencePropertyName, primaryKey, - revision, collectionClass); - } - - public List 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 mapToMapFromObject 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 data, - Number revision) { - return relatedIdData.getOriginalMapper().mapToIdFromMap(data); - } - - public void mapToMapFromObject(Map 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 implements Initializor { - private VersionsReaderImplementor versionsReader; - private Class entityClass; - private String owningReferencePropertyName; - private Object primaryKey; - private Number revision; - private Class collectionClass; - - public OneToManyAttachedInitializor(VersionsReaderImplementor versionsReader, Class entityClass, - String owningReferencePropertyName, Object primaryKey, Number revision, - Class 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 implements Initializor { 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 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 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.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) - */ - at 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) + */ + at Entity +public class RefEdMapKeyEntity { + @Id + @GeneratedValue + private Integer id; + + @Versioned + @OneToMany(mappedBy="reference") + @MapKey(name = "data") + private Map idmap; + + public RefEdMapKeyEntity() { + idmap = new HashMap(); + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public Map getIdmap() { + return idmap; + } + + public void setIdmap(Map 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) + */ + at 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 + From jboss-envers-commits at lists.jboss.org Wed Sep 24 07:17:25 2008 From: jboss-envers-commits at lists.jboss.org (jboss-envers-commits at lists.jboss.org) Date: Wed, 24 Sep 2008 07:17:25 -0400 Subject: [jboss-envers-commits] JBoss Envers SVN: r167 - in trunk/src: main/org/jboss/envers/entities/mapper/relation and 5 other directories. Message-ID: Author: adamw Date: 2008-09-24 07:17:25 -0400 (Wed, 24 Sep 2008) New Revision: 167 Added: trunk/src/main/org/jboss/envers/entities/mapper/relation/ListCollectionMapper.java trunk/src/test/org/jboss/envers/test/entities/collection/StringListEntity.java trunk/src/test/org/jboss/envers/test/integration/collection/StringList.java Modified: trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/ListCollectionInitializor.java trunk/src/main/org/jboss/envers/tools/Tools.java trunk/src/test/org/jboss/envers/test/tools/TestTools.java Log: ENVERS-42: support for (indexed) lists Modified: trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java =================================================================== --- trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java 2008-09-24 10:42:17 UTC (rev 166) +++ trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java 2008-09-24 11:17:25 UTC (rev 167) @@ -377,6 +377,10 @@ } else if (type instanceof BagType) { currentMapper.addComposite(propertyName, new BasicCollectionMapper(commonCollectionMapperData, ArrayList.class, ListProxy.class, elementComponentData)); + } else if (type instanceof ListType) { + // Indexed collection, so indexComponentData is not null. + currentMapper.addComposite(propertyName, new ListCollectionMapper(commonCollectionMapperData, + elementComponentData, indexComponentData)); } else { mainGenerator.throwUnsupportedTypeException(type, referencingEntityName, propertyName); } Copied: trunk/src/main/org/jboss/envers/entities/mapper/relation/ListCollectionMapper.java (from rev 156, trunk/src/main/org/jboss/envers/entities/mapper/relation/MapCollectionMapper.java) =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/ListCollectionMapper.java (rev 0) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/ListCollectionMapper.java 2008-09-24 11:17:25 UTC (rev 167) @@ -0,0 +1,67 @@ +package org.jboss.envers.entities.mapper.relation; + +import org.jboss.envers.entities.mapper.PropertyMapper; +import org.jboss.envers.entities.mapper.relation.lazy.initializor.Initializor; +import org.jboss.envers.entities.mapper.relation.lazy.initializor.ListCollectionInitializor; +import org.jboss.envers.entities.mapper.relation.lazy.proxy.ListProxy; +import org.jboss.envers.configuration.VersionsConfiguration; +import org.jboss.envers.reader.VersionsReaderImplementor; +import org.jboss.envers.tools.Tools; +import org.jboss.envers.tools.Pair; +import org.hibernate.collection.PersistentCollection; + +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.io.Serializable; + +/** + * @author Adam Warski (adam at warski dot org) + */ +public final class ListCollectionMapper extends AbstractCollectionMapper implements PropertyMapper { + private final MiddleComponentData elementComponentData; + private final MiddleComponentData indexComponentData; + + public ListCollectionMapper(CommonCollectionMapperData commonCollectionMapperData, + MiddleComponentData elementComponentData, MiddleComponentData indexComponentData) { + super(commonCollectionMapperData, List.class, ListProxy.class); + this.elementComponentData = elementComponentData; + this.indexComponentData = indexComponentData; + } + + protected Initializor getInitializor(VersionsConfiguration verCfg, VersionsReaderImplementor versionsReader, + Object primaryKey, Number revision) { + return new ListCollectionInitializor(verCfg, versionsReader, commonCollectionMapperData.getQueryGenerator(), + primaryKey, revision, elementComponentData, indexComponentData); + } + + @SuppressWarnings({"unchecked"}) + protected Collection getNewCollectionContent(PersistentCollection newCollection) { + if (newCollection == null) { + return null; + } else { + return Tools.listToIndexElementPairList((List) newCollection); + } + } + + @SuppressWarnings({"unchecked"}) + protected Collection getOldCollectionContent(Serializable oldCollection) { + if (oldCollection == null) { + return null; + } else { + return Tools.listToIndexElementPairList((List) oldCollection); + } + } + + @SuppressWarnings({"unchecked"}) + protected void mapToMapFromObject(Map data, Object changed) { + Pair indexValuePair = (Pair) changed; + elementComponentData.getComponentMapper().mapToMapFromObject(data, indexValuePair.getSecond()); + indexComponentData.getComponentMapper().mapToMapFromObject(data, indexValuePair.getFirst()); + } + + @SuppressWarnings({"unchecked"}) + protected Object getElement(Object changedObject) { + return ((Pair) changedObject).getFirst(); + } +} \ No newline at end of file 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-24 10:42:17 UTC (rev 166) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/ListCollectionInitializor.java 2008-09-24 11:17:25 UTC (rev 167) @@ -27,8 +27,13 @@ this.indexComponentData = indexComponentData; } + @SuppressWarnings({"unchecked"}) protected List initializeCollection(int size) { - return new ArrayList(); + // Creating a list of the given capacity with all elements null initially. This ensures that we can then + // fill the elements safely using the List.set method. + List list = new ArrayList(size); + for (int i=0; i List> listToIndexElementPairList(List list) { + List> ret = new ArrayList>(); + Iterator listIter = list.iterator(); + for (int i=0; i strings; + + public StringListEntity() { + strings = new ArrayList(); + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public List getStrings() { + return strings; + } + + public void setStrings(List strings) { + this.strings = strings; + } + + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof StringListEntity)) return false; + + StringListEntity that = (StringListEntity) 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 "SLE(id = " + id + ", strings = " + strings + ")"; + } +} \ No newline at end of file Property changes on: trunk/src/test/org/jboss/envers/test/entities/collection/StringListEntity.java ___________________________________________________________________ Name: svn:mergeinfo + Copied: trunk/src/test/org/jboss/envers/test/integration/collection/StringList.java (from rev 154, trunk/src/test/org/jboss/envers/test/integration/collection/StringSet.java) =================================================================== --- trunk/src/test/org/jboss/envers/test/integration/collection/StringList.java (rev 0) +++ trunk/src/test/org/jboss/envers/test/integration/collection/StringList.java 2008-09-24 11:17:25 UTC (rev 167) @@ -0,0 +1,101 @@ +package org.jboss.envers.test.integration.collection; + +import org.jboss.envers.test.integration.AbstractEntityTest; +import org.jboss.envers.test.tools.TestTools; +import org.jboss.envers.test.entities.collection.StringListEntity; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; +import org.hibernate.ejb.Ejb3Configuration; + +import javax.persistence.EntityManager; +import java.util.Arrays; +import java.util.Collections; + +/** + * @author Adam Warski (adam at warski dot org) + */ +public class StringList extends AbstractEntityTest { + private Integer sle1_id; + private Integer sle2_id; + + public void configure(Ejb3Configuration cfg) { + cfg.addAnnotatedClass(StringListEntity.class); + } + + @BeforeClass(dependsOnMethods = "init") + public void initData() { + EntityManager em = getEntityManager(); + + StringListEntity sle1 = new StringListEntity(); + StringListEntity sle2 = new StringListEntity(); + + // Revision 1 (sle1: initialy empty, sle2: initialy 2 elements) + em.getTransaction().begin(); + + sle2.getStrings().add("sle2_string1"); + sle2.getStrings().add("sle2_string2"); + + em.persist(sle1); + em.persist(sle2); + + em.getTransaction().commit(); + + // Revision 2 (sle1: adding 2 elements, sle2: adding an existing element) + em.getTransaction().begin(); + + sle1 = em.find(StringListEntity.class, sle1.getId()); + sle2 = em.find(StringListEntity.class, sle2.getId()); + + sle1.getStrings().add("sle1_string1"); + sle1.getStrings().add("sle1_string2"); + + sle2.getStrings().add("sle2_string1"); + + em.getTransaction().commit(); + + // Revision 3 (sle1: replacing an element at index 0, sle2: removing an element at index 0) + em.getTransaction().begin(); + + sle1 = em.find(StringListEntity.class, sle1.getId()); + sle2 = em.find(StringListEntity.class, sle2.getId()); + + sle1.getStrings().set(0, "sle1_string3"); + + sle2.getStrings().remove(0); + + em.getTransaction().commit(); + + // + + sle1_id = sle1.getId(); + sle2_id = sle2.getId(); + } + + @Test + public void testRevisionsCounts() { + assert Arrays.asList(1, 2, 3).equals(getVersionsReader().getRevisions(StringListEntity.class, sle1_id)); + assert Arrays.asList(1, 2, 3).equals(getVersionsReader().getRevisions(StringListEntity.class, sle2_id)); + } + + @Test + public void testHistoryOfSle1() { + StringListEntity rev1 = getVersionsReader().find(StringListEntity.class, sle1_id, 1); + StringListEntity rev2 = getVersionsReader().find(StringListEntity.class, sle1_id, 2); + StringListEntity rev3 = getVersionsReader().find(StringListEntity.class, sle1_id, 3); + + assert rev1.getStrings().equals(Collections.EMPTY_LIST); + assert rev2.getStrings().equals(TestTools.makeList("sle1_string1", "sle1_string2")); + assert rev3.getStrings().equals(TestTools.makeList("sle1_string3", "sle1_string2")); + } + + @Test + public void testHistoryOfSse2() { + StringListEntity rev1 = getVersionsReader().find(StringListEntity.class, sle2_id, 1); + StringListEntity rev2 = getVersionsReader().find(StringListEntity.class, sle2_id, 2); + StringListEntity rev3 = getVersionsReader().find(StringListEntity.class, sle2_id, 3); + + assert rev1.getStrings().equals(TestTools.makeList("sle2_string1", "sle2_string2")); + assert rev2.getStrings().equals(TestTools.makeList("sle2_string1", "sle2_string2", "sle2_string1")); + assert rev3.getStrings().equals(TestTools.makeList("sle2_string2", "sle2_string1")); + } +} \ No newline at end of file Modified: trunk/src/test/org/jboss/envers/test/tools/TestTools.java =================================================================== --- trunk/src/test/org/jboss/envers/test/tools/TestTools.java 2008-09-24 10:42:17 UTC (rev 166) +++ trunk/src/test/org/jboss/envers/test/tools/TestTools.java 2008-09-24 11:17:25 UTC (rev 167) @@ -16,6 +16,10 @@ return ret; } + public static List makeList(T... objects) { + return Arrays.asList(objects); + } + public static Map makeMap(Object... objects) { Map ret = new HashMap(); // The number of objects must be divisable by 2. From jboss-envers-commits at lists.jboss.org Wed Sep 24 08:06:48 2008 From: jboss-envers-commits at lists.jboss.org (jboss-envers-commits at lists.jboss.org) Date: Wed, 24 Sep 2008 08:06:48 -0400 Subject: [jboss-envers-commits] JBoss Envers SVN: r168 - in trunk/src: main/org/jboss/envers/entities/mapper/relation/query and 2 other directories. Message-ID: Author: adamw Date: 2008-09-24 08:06:48 -0400 (Wed, 24 Sep 2008) New Revision: 168 Added: trunk/src/main/org/jboss/envers/entities/mapper/relation/query/QueryGeneratorTools.java trunk/src/test/org/jboss/envers/test/integration/manytomany/ternary/ trunk/src/test/org/jboss/envers/test/integration/manytomany/ternary/TernaryMap.java trunk/src/test/org/jboss/envers/test/integration/manytomany/ternary/TernaryMapEntity.java Removed: trunk/src/main/org/jboss/envers/entities/mapper/relation/query/MiddleTableQueryGenerator.java Modified: trunk/src/main/org/jboss/envers/configuration/metadata/QueryGeneratorBuilder.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 Log: ENVERS-25: @MapKeyManyToMany support in ternary associations Modified: trunk/src/main/org/jboss/envers/configuration/metadata/QueryGeneratorBuilder.java =================================================================== --- trunk/src/main/org/jboss/envers/configuration/metadata/QueryGeneratorBuilder.java 2008-09-24 11:17:25 UTC (rev 167) +++ trunk/src/main/org/jboss/envers/configuration/metadata/QueryGeneratorBuilder.java 2008-09-24 12:06:48 UTC (rev 168) @@ -4,8 +4,8 @@ import org.jboss.envers.entities.mapper.relation.MiddleComponentData; 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; +import org.jboss.envers.entities.mapper.relation.query.TwoEntityQueryGenerator; import org.jboss.envers.configuration.VersionsEntitiesConfiguration; import java.util.List; @@ -42,10 +42,9 @@ } else if (idDatas.size() == 1) { return new TwoEntityQueryGenerator(verEntCfg, versionsMiddleEntityName, referencingIdData, idDatas.get(0), componentDatas); - } else if (idDatas.size() == 1) { - // TODO: add componentDatas + } else if (idDatas.size() == 2) { return new ThreeEntityQueryGenerator(verEntCfg, versionsMiddleEntityName, referencingIdData, - idDatas.get(0), idDatas.get(1)); + idDatas.get(0), idDatas.get(1), componentDatas); } else { throw new IllegalStateException("Illegal number of related entities."); } Deleted: trunk/src/main/org/jboss/envers/entities/mapper/relation/query/MiddleTableQueryGenerator.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/query/MiddleTableQueryGenerator.java 2008-09-24 11:17:25 UTC (rev 167) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/query/MiddleTableQueryGenerator.java 2008-09-24 12:06:48 UTC (rev 168) @@ -1,14 +0,0 @@ -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 middle-table (a table used - * for mapping relations). The query can select, apart from selecting the middle table, select also other "related" - * entities. - * @author Adam Warski (adam at warski dot org) - */ -public interface MiddleTableQueryGenerator { - Query getQuery(VersionsReaderImplementor versionsReader, Object primaryKey, Number revision); -} Added: trunk/src/main/org/jboss/envers/entities/mapper/relation/query/QueryGeneratorTools.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/query/QueryGeneratorTools.java (rev 0) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/query/QueryGeneratorTools.java 2008-09-24 12:06:48 UTC (rev 168) @@ -0,0 +1,51 @@ +package org.jboss.envers.entities.mapper.relation.query; + +import org.jboss.envers.tools.query.QueryBuilder; +import org.jboss.envers.tools.query.Parameters; +import org.jboss.envers.entities.mapper.relation.MiddleIdData; +import org.jboss.envers.entities.mapper.relation.MiddleComponentData; + +/** + * @author Adam Warski (adam at warski dot org) + */ +public class QueryGeneratorTools { + public static void addEntityAtRevision(QueryBuilder qb, Parameters rootParameters, MiddleIdData idData, + String revisionPropertyPath, String originalIdPropertyName, + String alias1, String alias2) { + // SELECT max(e.revision) FROM versionsReferencedEntity e2 + QueryBuilder maxERevQb = qb.newSubQueryBuilder(idData.getVersionsEntityName(), alias2); + maxERevQb.addProjection("max", revisionPropertyPath, false); + // WHERE + Parameters maxERevQbParameters = maxERevQb.getRootParameters(); + // e2.revision <= :revision + maxERevQbParameters.addWhereWithNamedParam(revisionPropertyPath, "<=", "revision"); + // e2.id_ref_ed = e.id_ref_ed + idData.getOriginalMapper().addIdsEqualToQuery(maxERevQbParameters, + alias1 + "." + originalIdPropertyName, alias2 +"." + originalIdPropertyName); + + // e.revision = (SELECT max(...) ...) + rootParameters.addWhere("e." + revisionPropertyPath, false, "=", maxERevQb); + } + + public static void addAssociationAtRevision(QueryBuilder qb, Parameters rootParameters, + MiddleIdData referencingIdData, String versionsMiddleEntityName, + String eeOriginalIdPropertyPath, String revisionPropertyPath, + String originalIdPropertyName, MiddleComponentData... componentDatas) { + // SELECT max(ee2.revision) FROM middleEntity ee2 + QueryBuilder maxEeRevQb = qb.newSubQueryBuilder(versionsMiddleEntityName, "ee2"); + maxEeRevQb.addProjection("max", revisionPropertyPath, false); + // WHERE + Parameters maxEeRevQbParameters = maxEeRevQb.getRootParameters(); + // ee2.revision <= :revision + maxEeRevQbParameters.addWhereWithNamedParam(revisionPropertyPath, "<=", "revision"); + // ee2.originalId.* = ee.originalId.* + String ee2OriginalIdPropertyPath = "ee2." + originalIdPropertyName; + referencingIdData.getPrefixedMapper().addIdsEqualToQuery(maxEeRevQbParameters, eeOriginalIdPropertyPath, ee2OriginalIdPropertyPath); + for (MiddleComponentData componentData : componentDatas) { + componentData.getComponentMapper().addMiddleEqualToQuery(maxEeRevQbParameters, eeOriginalIdPropertyPath, ee2OriginalIdPropertyPath); + } + + // ee.revision = (SELECT max(...) ...) + rootParameters.addWhere(revisionPropertyPath, "=", maxEeRevQb); + } +} 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-24 11:17:25 UTC (rev 167) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/query/ThreeEntityQueryGenerator.java 2008-09-24 12:06:48 UTC (rev 168) @@ -1,11 +1,19 @@ package org.jboss.envers.entities.mapper.relation.query; -import org.hibernate.Query; -import org.jboss.envers.reader.VersionsReaderImplementor; +import org.jboss.envers.entities.mapper.id.QueryParameterData; import org.jboss.envers.entities.mapper.relation.MiddleIdData; +import org.jboss.envers.entities.mapper.relation.MiddleComponentData; 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 relation middle-table and a two related versions entity. * @author Adam Warski (adam at warski dot org) */ public final class ThreeEntityQueryGenerator implements RelationQueryGenerator { @@ -15,15 +23,88 @@ public ThreeEntityQueryGenerator(VersionsEntitiesConfiguration verEntCfg, String versionsMiddleEntityName, MiddleIdData referencingIdData, - MiddleIdData referencedIdData1, - MiddleIdData referencedIdData2) { + MiddleIdData referencedIdData, + MiddleIdData indexIdData, + MiddleComponentData... componentDatas) { this.referencingIdData = referencingIdData; - // TODO - queryString = ""; + /* + * The query that we need to create: + * SELECT new list(ee, e, f) FROM versionsReferencedEntity e, versionsIndexEntity f, middleEntity ee + * WHERE + * (entities referenced by the middle table; id_ref_ed = id of the referenced entity) + * ee.id_ref_ed = e.id_ref_ed AND + * (entities referenced by the middle table; id_ref_ind = id of the index entity) + * ee.id_ref_ind = f.id_ref_ind AND + * (only entities referenced by the association; id_ref_ing = id of the referencing entity) + * ee.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_ref_ed = e.id_ref_ed) AND + * (selecting f entities at revision :revision) + * f.revision = (SELECT max(f2.revision) FROM versionsIndexEntity f2 + * WHERE f2.revision <= :revision AND f2.id_ref_ed = f.id_ref_ed) AND + * (the association at revision :revision) + * ee.revision = (SELECT max(ee2.revision) FROM middleEntity ee2 + * WHERE ee2.revision <= :revision AND ee2.originalId.* = ee.originalId.*) AND + * (only non-deleted entities and associations) + * ee.revision_type != DEL AND + * e.revision_type != DEL AND + * f.revision_type != DEL + */ + String revisionPropertyPath = verEntCfg.getRevisionPropPath(); + String originalIdPropertyName = verEntCfg.getOriginalIdPropName(); + + String eeOriginalIdPropertyPath = "ee." + originalIdPropertyName; + + // SELECT new list(ee) FROM middleEntity ee + QueryBuilder qb = new QueryBuilder(versionsMiddleEntityName, "ee"); + qb.addFrom(referencedIdData.getVersionsEntityName(), "e"); + qb.addFrom(indexIdData.getVersionsEntityName(), "f"); + qb.addProjection("new list", "ee, e, f", false, false); + // WHERE + Parameters rootParameters = qb.getRootParameters(); + // ee.id_ref_ed = e.id_ref_ed + referencedIdData.getPrefixedMapper().addIdsEqualToQuery(rootParameters, eeOriginalIdPropertyPath, + referencedIdData.getOriginalMapper(), "e." + originalIdPropertyName); + // ee.id_ref_ind = f.id_ref_ind + indexIdData.getPrefixedMapper().addIdsEqualToQuery(rootParameters, eeOriginalIdPropertyPath, + indexIdData.getOriginalMapper(), "f." + originalIdPropertyName); + // ee.originalId.id_ref_ing = :id_ref_ing + referencingIdData.getPrefixedMapper().addNamedIdEqualsToQuery(rootParameters, originalIdPropertyName, true); + + // e.revision = (SELECT max(...) ...) + QueryGeneratorTools.addEntityAtRevision(qb, rootParameters, referencedIdData, revisionPropertyPath, + originalIdPropertyName, "e", "e2"); + + // f.revision = (SELECT max(...) ...) + QueryGeneratorTools.addEntityAtRevision(qb, rootParameters, indexIdData, revisionPropertyPath, + originalIdPropertyName, "f", "f2"); + + // ee.revision = (SELECT max(...) ...) + QueryGeneratorTools.addAssociationAtRevision(qb, rootParameters, referencingIdData, versionsMiddleEntityName, + eeOriginalIdPropertyPath, revisionPropertyPath, originalIdPropertyName, componentDatas); + + // ee.revision_type != DEL + rootParameters.addWhereWithNamedParam(verEntCfg.getRevisionTypePropName(), "!=", "delrevisiontype"); + // e.revision_type != DEL + rootParameters.addWhereWithNamedParam("e." + verEntCfg.getRevisionTypePropName(), false, "!=", "delrevisiontype"); + // f.revision_type != DEL + rootParameters.addWhereWithNamedParam("f." + verEntCfg.getRevisionTypePropName(), false, "!=", "delrevisiontype"); + + StringBuilder sb = new StringBuilder(); + qb.build(sb, Collections.emptyMap()); + queryString = sb.toString(); } public Query getQuery(VersionsReaderImplementor versionsReader, Object primaryKey, Number revision) { - throw new RuntimeException(); + 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; } } 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-24 11:17:25 UTC (rev 167) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/query/TwoEntityQueryGenerator.java 2008-09-24 12:06:48 UTC (rev 168) @@ -62,35 +62,13 @@ // ee.originalId.id_ref_ing = :id_ref_ing referencingIdData.getPrefixedMapper().addNamedIdEqualsToQuery(rootParameters, originalIdPropertyName, true); - // SELECT max(e.revision) FROM versionsReferencedEntity e2 - QueryBuilder maxERevQb = qb.newSubQueryBuilder(referencedIdData.getVersionsEntityName(), "e2"); - maxERevQb.addProjection("max", revisionPropertyPath, false); - // WHERE - Parameters maxERevQbParameters = maxERevQb.getRootParameters(); - // e2.revision <= :revision - maxERevQbParameters.addWhereWithNamedParam(revisionPropertyPath, "<=", "revision"); - // e2.id_ref_ed = e.id_ref_ed - referencedIdData.getOriginalMapper().addIdsEqualToQuery(maxERevQbParameters, "e." + originalIdPropertyName, "e2." + originalIdPropertyName); - // e.revision = (SELECT max(...) ...) - rootParameters.addWhere("e." + revisionPropertyPath, false, "=", maxERevQb); + QueryGeneratorTools.addEntityAtRevision(qb, rootParameters, referencedIdData, revisionPropertyPath, + originalIdPropertyName, "e", "e2"); - // SELECT max(ee2.revision) FROM middleEntity ee2 - QueryBuilder maxEeRevQb = qb.newSubQueryBuilder(versionsMiddleEntityName, "ee2"); - maxEeRevQb.addProjection("max", revisionPropertyPath, false); - // WHERE - Parameters maxEeRevQbParameters = maxEeRevQb.getRootParameters(); - // ee2.revision <= :revision - maxEeRevQbParameters.addWhereWithNamedParam(revisionPropertyPath, "<=", "revision"); - // ee2.originalId.* = ee.originalId.* - String ee2OriginalIdPropertyPath = "ee2." + originalIdPropertyName; - referencingIdData.getPrefixedMapper().addIdsEqualToQuery(maxEeRevQbParameters, eeOriginalIdPropertyPath, ee2OriginalIdPropertyPath); - for (MiddleComponentData componentData : componentDatas) { - componentData.getComponentMapper().addMiddleEqualToQuery(maxEeRevQbParameters, eeOriginalIdPropertyPath, ee2OriginalIdPropertyPath); - } - // ee.revision = (SELECT max(...) ...) - rootParameters.addWhere(revisionPropertyPath, "=", maxEeRevQb); + QueryGeneratorTools.addAssociationAtRevision(qb, rootParameters, referencingIdData, versionsMiddleEntityName, + eeOriginalIdPropertyPath, revisionPropertyPath, originalIdPropertyName, componentDatas); // ee.revision_type != DEL rootParameters.addWhereWithNamedParam(verEntCfg.getRevisionTypePropName(), "!=", "delrevisiontype"); Copied: trunk/src/test/org/jboss/envers/test/integration/manytomany/ternary/TernaryMap.java (from rev 156, trunk/src/test/org/jboss/envers/test/integration/manytomany/BasicMap.java) =================================================================== --- trunk/src/test/org/jboss/envers/test/integration/manytomany/ternary/TernaryMap.java (rev 0) +++ trunk/src/test/org/jboss/envers/test/integration/manytomany/ternary/TernaryMap.java 2008-09-24 12:06:48 UTC (rev 168) @@ -0,0 +1,176 @@ +package org.jboss.envers.test.integration.manytomany.ternary; + +import org.jboss.envers.test.integration.AbstractEntityTest; +import org.jboss.envers.test.entities.StrTestEntity; +import org.jboss.envers.test.entities.IntTestEntity; +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 TernaryMap extends AbstractEntityTest { + private Integer str1_id; + private Integer str2_id; + + private Integer int1_id; + private Integer int2_id; + + private Integer map1_id; + private Integer map2_id; + + public void configure(Ejb3Configuration cfg) { + cfg.addAnnotatedClass(TernaryMapEntity.class); + cfg.addAnnotatedClass(StrTestEntity.class); + cfg.addAnnotatedClass(IntTestEntity.class); + } + + @BeforeClass(dependsOnMethods = "init") + public void initData() { + EntityManager em = getEntityManager(); + + StrTestEntity str1 = new StrTestEntity("a"); + StrTestEntity str2 = new StrTestEntity("b"); + + IntTestEntity int1 = new IntTestEntity(1); + IntTestEntity int2 = new IntTestEntity(2); + + TernaryMapEntity map1 = new TernaryMapEntity(); + TernaryMapEntity map2 = new TernaryMapEntity(); + + // Revision 1 (map1: initialy one mapping int1 -> str1, map2: empty) + em.getTransaction().begin(); + + em.persist(str1); + em.persist(str2); + em.persist(int1); + em.persist(int2); + + map1.getMap().put(int1, str1); + + em.persist(map1); + em.persist(map2); + + em.getTransaction().commit(); + + // Revision 2 (map1: replacing the mapping, map2: adding two mappings) + + em.getTransaction().begin(); + + map1 = em.find(TernaryMapEntity.class, map1.getId()); + map2 = em.find(TernaryMapEntity.class, map2.getId()); + + str1 = em.find(StrTestEntity.class, str1.getId()); + str2 = em.find(StrTestEntity.class, str2.getId()); + + int1 = em.find(IntTestEntity.class, int1.getId()); + int2 = em.find(IntTestEntity.class, int2.getId()); + + map1.getMap().put(int1, str2); + + map2.getMap().put(int1, str1); + map2.getMap().put(int2, str1); + + em.getTransaction().commit(); + + // Revision 3 (map1: removing a non-existing mapping, adding an existing mapping - no changes, map2: removing a mapping) + em.getTransaction().begin(); + + map1 = em.find(TernaryMapEntity.class, map1.getId()); + map2 = em.find(TernaryMapEntity.class, map2.getId()); + + str2 = em.find(StrTestEntity.class, str2.getId()); + + int1 = em.find(IntTestEntity.class, int1.getId()); + int2 = em.find(IntTestEntity.class, int2.getId()); + + map1.getMap().remove(int2); + map1.getMap().put(int1, str2); + + map2.getMap().remove(int1); + + em.getTransaction().commit(); + + // Revision 4 (map1: adding a mapping, map2: adding a mapping) + em.getTransaction().begin(); + + map1 = em.find(TernaryMapEntity.class, map1.getId()); + map2 = em.find(TernaryMapEntity.class, map2.getId()); + + str2 = em.find(StrTestEntity.class, str2.getId()); + + int1 = em.find(IntTestEntity.class, int1.getId()); + int2 = em.find(IntTestEntity.class, int2.getId()); + + map1.getMap().put(int2, str2); + + map2.getMap().put(int1, str2); + + em.getTransaction().commit(); + // + + map1_id = map1.getId(); + map2_id = map2.getId(); + + str1_id = str1.getId(); + str2_id = str2.getId(); + + int1_id = int1.getId(); + int2_id = int2.getId(); + } + + @Test + public void testRevisionsCounts() { + assert Arrays.asList(1, 2, 4).equals(getVersionsReader().getRevisions(TernaryMapEntity.class, map1_id)); + assert Arrays.asList(1, 2, 3, 4).equals(getVersionsReader().getRevisions(TernaryMapEntity.class, map2_id)); + + assert Arrays.asList(1).equals(getVersionsReader().getRevisions(StrTestEntity.class, str1_id)); + assert Arrays.asList(1).equals(getVersionsReader().getRevisions(StrTestEntity.class, str2_id)); + + assert Arrays.asList(1).equals(getVersionsReader().getRevisions(IntTestEntity.class, int1_id)); + assert Arrays.asList(1).equals(getVersionsReader().getRevisions(IntTestEntity.class, int2_id)); + } + + @Test + public void testHistoryOfMap1() { + StrTestEntity str1 = getEntityManager().find(StrTestEntity.class, str1_id); + StrTestEntity str2 = getEntityManager().find(StrTestEntity.class, str2_id); + + IntTestEntity int1 = getEntityManager().find(IntTestEntity.class, int1_id); + IntTestEntity int2 = getEntityManager().find(IntTestEntity.class, int2_id); + + TernaryMapEntity rev1 = getVersionsReader().find(TernaryMapEntity.class, map1_id, 1); + TernaryMapEntity rev2 = getVersionsReader().find(TernaryMapEntity.class, map1_id, 2); + TernaryMapEntity rev3 = getVersionsReader().find(TernaryMapEntity.class, map1_id, 3); + TernaryMapEntity rev4 = getVersionsReader().find(TernaryMapEntity.class, map1_id, 4); + + assert rev1.getMap().equals(TestTools.makeMap(int1, str1)); + assert rev2.getMap().equals(TestTools.makeMap(int1, str2)); + assert rev3.getMap().equals(TestTools.makeMap(int1, str2)); + assert rev4.getMap().equals(TestTools.makeMap(int1, str2, int2, str2)); + } + + @Test + public void testHistoryOfMap2() { + StrTestEntity str1 = getEntityManager().find(StrTestEntity.class, str1_id); + StrTestEntity str2 = getEntityManager().find(StrTestEntity.class, str2_id); + + IntTestEntity int1 = getEntityManager().find(IntTestEntity.class, int1_id); + IntTestEntity int2 = getEntityManager().find(IntTestEntity.class, int2_id); + + TernaryMapEntity rev1 = getVersionsReader().find(TernaryMapEntity.class, map2_id, 1); + TernaryMapEntity rev2 = getVersionsReader().find(TernaryMapEntity.class, map2_id, 2); + TernaryMapEntity rev3 = getVersionsReader().find(TernaryMapEntity.class, map2_id, 3); + TernaryMapEntity rev4 = getVersionsReader().find(TernaryMapEntity.class, map2_id, 4); + + assert rev1.getMap().equals(TestTools.makeMap()); + assert rev2.getMap().equals(TestTools.makeMap(int1, str1, int2, str1)); + assert rev3.getMap().equals(TestTools.makeMap(int2, str1)); + assert rev4.getMap().equals(TestTools.makeMap(int1, str2, int2, str1)); + } +} \ No newline at end of file Copied: trunk/src/test/org/jboss/envers/test/integration/manytomany/ternary/TernaryMapEntity.java (from rev 148, trunk/src/test/org/jboss/envers/test/entities/StrIntTestEntity.java) =================================================================== --- trunk/src/test/org/jboss/envers/test/integration/manytomany/ternary/TernaryMapEntity.java (rev 0) +++ trunk/src/test/org/jboss/envers/test/integration/manytomany/ternary/TernaryMapEntity.java 2008-09-24 12:06:48 UTC (rev 168) @@ -0,0 +1,67 @@ +package org.jboss.envers.test.integration.manytomany.ternary; + +import org.jboss.envers.Versioned; +import org.jboss.envers.test.entities.IntTestEntity; +import org.jboss.envers.test.entities.StrTestEntity; +import org.hibernate.annotations.MapKeyManyToMany; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.GeneratedValue; +import javax.persistence.ManyToMany; +import java.util.Map; +import java.util.HashMap; + +/** + * @author Adam Warski (adam at warski dot org) + */ + at Entity +public class TernaryMapEntity { + @Id + @GeneratedValue + private Integer id; + + @Versioned + @ManyToMany + @MapKeyManyToMany + private Map map; + + public TernaryMapEntity() { + map = new HashMap(); + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public Map getMap() { + return map; + } + + public void setMap(Map map) { + this.map = map; + } + + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof TernaryMapEntity)) return false; + + TernaryMapEntity that = (TernaryMapEntity) 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 "TME(id = " + id + ", map = " + map + ")"; + } +} \ No newline at end of file Property changes on: trunk/src/test/org/jboss/envers/test/integration/manytomany/ternary/TernaryMapEntity.java ___________________________________________________________________ Name: svn:mergeinfo + From jboss-envers-commits at lists.jboss.org Wed Sep 24 09:45:08 2008 From: jboss-envers-commits at lists.jboss.org (jboss-envers-commits at lists.jboss.org) Date: Wed, 24 Sep 2008 09:45:08 -0400 Subject: [jboss-envers-commits] JBoss Envers SVN: r169 - trunk/resources/test. Message-ID: Author: adamw Date: 2008-09-24 09:45:08 -0400 (Wed, 24 Sep 2008) New Revision: 169 Modified: trunk/resources/test/testng.xml Log: ENVERS-25: adding the new test to testng.xml Modified: trunk/resources/test/testng.xml =================================================================== --- trunk/resources/test/testng.xml 2008-09-24 12:06:48 UTC (rev 168) +++ trunk/resources/test/testng.xml 2008-09-24 13:45:08 UTC (rev 169) @@ -15,6 +15,7 @@ + From jboss-envers-commits at lists.jboss.org Wed Sep 24 12:03:11 2008 From: jboss-envers-commits at lists.jboss.org (jboss-envers-commits at lists.jboss.org) Date: Wed, 24 Sep 2008 12:03:11 -0400 Subject: [jboss-envers-commits] JBoss Envers SVN: r170 - in trunk: src/main/org/jboss/envers/configuration and 11 other directories. Message-ID: Author: adamw Date: 2008-09-24 12:03:11 -0400 (Wed, 24 Sep 2008) New Revision: 170 Modified: trunk/envers.iml trunk/src/main/org/jboss/envers/configuration/EntitiesConfigurator.java trunk/src/main/org/jboss/envers/configuration/metadata/BasicMetadataGenerator.java trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java trunk/src/main/org/jboss/envers/configuration/metadata/IdMetadataGenerator.java trunk/src/main/org/jboss/envers/configuration/metadata/QueryGeneratorBuilder.java trunk/src/main/org/jboss/envers/configuration/metadata/ToOneRelationMetadataGenerator.java trunk/src/main/org/jboss/envers/configuration/metadata/VersionsMetadataGenerator.java trunk/src/main/org/jboss/envers/entities/EntityConfiguration.java trunk/src/main/org/jboss/envers/entities/mapper/id/EmbeddedIdMapper.java trunk/src/main/org/jboss/envers/entities/mapper/id/MultipleIdMapper.java trunk/src/main/org/jboss/envers/entities/mapper/relation/AbstractCollectionMapper.java trunk/src/main/org/jboss/envers/entities/mapper/relation/BasicCollectionMapper.java trunk/src/main/org/jboss/envers/entities/mapper/relation/CommonCollectionMapperData.java trunk/src/main/org/jboss/envers/entities/mapper/relation/ListCollectionMapper.java trunk/src/main/org/jboss/envers/entities/mapper/relation/MapCollectionMapper.java trunk/src/main/org/jboss/envers/entities/mapper/relation/MiddleComponentData.java trunk/src/main/org/jboss/envers/entities/mapper/relation/MiddleIdData.java trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleComponentMapper.java trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleDummyComponentMapper.java trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleMapKeyIdComponentMapper.java trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleMapKeyPropertyComponentMapper.java trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleRelatedComponentMapper.java trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleSimpleComponentMapper.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/OneVersionsEntityQueryGenerator.java trunk/src/main/org/jboss/envers/entities/mapper/relation/query/QueryGeneratorTools.java trunk/src/main/org/jboss/envers/entities/mapper/relation/query/RelationQueryGenerator.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/main/org/jboss/envers/exception/NotVersionedException.java trunk/src/main/org/jboss/envers/exception/RevisionDoesNotExistException.java trunk/src/main/org/jboss/envers/reader/VersionsReaderImpl.java trunk/src/test/org/jboss/envers/test/entities/customtype/ParametrizedTestUserType.java trunk/src/test/org/jboss/envers/test/integration/collection/StringMap.java Log: Code cleanup and missing license headers Modified: trunk/envers.iml =================================================================== --- trunk/envers.iml 2008-09-24 13:45:08 UTC (rev 169) +++ trunk/envers.iml 2008-09-24 16:03:11 UTC (rev 170) @@ -10,6 +10,7 @@ + Modified: trunk/src/main/org/jboss/envers/configuration/EntitiesConfigurator.java =================================================================== --- trunk/src/main/org/jboss/envers/configuration/EntitiesConfigurator.java 2008-09-24 13:45:08 UTC (rev 169) +++ trunk/src/main/org/jboss/envers/configuration/EntitiesConfigurator.java 2008-09-24 16:03:11 UTC (rev 170) @@ -87,10 +87,10 @@ } // Second pass - for (PersistentClass pc : pcDatas.keySet()) { - EntityXmlMappingData xmlMappingData = xmlMappings.get(pc); + for (Map.Entry pcDatasEntry : pcDatas.entrySet()) { + EntityXmlMappingData xmlMappingData = xmlMappings.get(pcDatasEntry.getKey()); - versionsMetaGen.generateSecondPass(pc, pcDatas.get(pc), xmlMappingData); + versionsMetaGen.generateSecondPass(pcDatasEntry.getKey(), pcDatasEntry.getValue(), xmlMappingData); try { cfg.addDocument(writer.write(xmlMappingData.getMainXmlMapping())); Modified: trunk/src/main/org/jboss/envers/configuration/metadata/BasicMetadataGenerator.java =================================================================== --- trunk/src/main/org/jboss/envers/configuration/metadata/BasicMetadataGenerator.java 2008-09-24 13:45:08 UTC (rev 169) +++ trunk/src/main/org/jboss/envers/configuration/metadata/BasicMetadataGenerator.java 2008-09-24 16:03:11 UTC (rev 170) @@ -1,3 +1,26 @@ +/* + * JBoss, Home of Professional Open Source + * + * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated + * by the @authors tag. All rights reserved. + * + * See the copyright.txt in the distribution for a full listing of individual + * contributors. 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.configuration.metadata; import org.dom4j.Element; Modified: trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java =================================================================== --- trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java 2008-09-24 13:45:08 UTC (rev 169) +++ trunk/src/main/org/jboss/envers/configuration/metadata/CollectionMetadataGenerator.java 2008-09-24 16:03:11 UTC (rev 170) @@ -1,3 +1,26 @@ +/* + * JBoss, Home of Professional Open Source + * + * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated + * by the @authors tag. All rights reserved. + * + * See the copyright.txt in the distribution for a full listing of individual + * contributors. 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.configuration.metadata; import org.hibernate.mapping.*; Modified: trunk/src/main/org/jboss/envers/configuration/metadata/IdMetadataGenerator.java =================================================================== --- trunk/src/main/org/jboss/envers/configuration/metadata/IdMetadataGenerator.java 2008-09-24 13:45:08 UTC (rev 169) +++ trunk/src/main/org/jboss/envers/configuration/metadata/IdMetadataGenerator.java 2008-09-24 16:03:11 UTC (rev 170) @@ -1,3 +1,26 @@ +/* + * JBoss, Home of Professional Open Source + * + * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated + * by the @authors tag. All rights reserved. + * + * See the copyright.txt in the distribution for a full listing of individual + * contributors. 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.configuration.metadata; import org.dom4j.Element; Modified: trunk/src/main/org/jboss/envers/configuration/metadata/QueryGeneratorBuilder.java =================================================================== --- trunk/src/main/org/jboss/envers/configuration/metadata/QueryGeneratorBuilder.java 2008-09-24 13:45:08 UTC (rev 169) +++ trunk/src/main/org/jboss/envers/configuration/metadata/QueryGeneratorBuilder.java 2008-09-24 16:03:11 UTC (rev 170) @@ -1,3 +1,26 @@ +/* + * JBoss, Home of Professional Open Source + * + * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated + * by the @authors tag. All rights reserved. + * + * See the copyright.txt in the distribution for a full listing of individual + * contributors. 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.configuration.metadata; import org.jboss.envers.entities.mapper.relation.MiddleIdData; Modified: trunk/src/main/org/jboss/envers/configuration/metadata/ToOneRelationMetadataGenerator.java =================================================================== --- trunk/src/main/org/jboss/envers/configuration/metadata/ToOneRelationMetadataGenerator.java 2008-09-24 13:45:08 UTC (rev 169) +++ trunk/src/main/org/jboss/envers/configuration/metadata/ToOneRelationMetadataGenerator.java 2008-09-24 16:03:11 UTC (rev 170) @@ -1,3 +1,26 @@ +/* + * JBoss, Home of Professional Open Source + * + * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated + * by the @authors tag. All rights reserved. + * + * See the copyright.txt in the distribution for a full listing of individual + * contributors. 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.configuration.metadata; import org.dom4j.Element; Modified: trunk/src/main/org/jboss/envers/configuration/metadata/VersionsMetadataGenerator.java =================================================================== --- trunk/src/main/org/jboss/envers/configuration/metadata/VersionsMetadataGenerator.java 2008-09-24 13:45:08 UTC (rev 169) +++ trunk/src/main/org/jboss/envers/configuration/metadata/VersionsMetadataGenerator.java 2008-09-24 16:03:11 UTC (rev 170) @@ -321,7 +321,7 @@ addJoins(pc, propertyMapper, versioningData, pc.getEntityName(), xmlMappingData, true); // Storing the generated configuration - EntityConfiguration entityCfg = new EntityConfiguration(entityName, versionsEntityName, idMapper, + EntityConfiguration entityCfg = new EntityConfiguration(versionsEntityName, idMapper, propertyMapper, parentEntityName); entitiesConfigurations.put(pc.getEntityName(), entityCfg); } Modified: trunk/src/main/org/jboss/envers/entities/EntityConfiguration.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/EntityConfiguration.java 2008-09-24 13:45:08 UTC (rev 169) +++ trunk/src/main/org/jboss/envers/entities/EntityConfiguration.java 2008-09-24 16:03:11 UTC (rev 170) @@ -31,7 +31,6 @@ * @author Adam Warski (adam at warski dot org) */ public class EntityConfiguration { - private String entityName; private String versionsEntityName; private IdMappingData idMappingData; private ExtendedPropertyMapper propertyMapper; @@ -39,9 +38,8 @@ private Map relations; private String parentEntityName; - public EntityConfiguration(String entityName, String versionsEntityName, IdMappingData idMappingData, + public EntityConfiguration(String versionsEntityName, IdMappingData idMappingData, ExtendedPropertyMapper propertyMapper, String parentEntityName) { - this.entityName = entityName; this.versionsEntityName = versionsEntityName; this.idMappingData = idMappingData; this.propertyMapper = propertyMapper; Modified: trunk/src/main/org/jboss/envers/entities/mapper/id/EmbeddedIdMapper.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/id/EmbeddedIdMapper.java 2008-09-24 13:45:08 UTC (rev 169) +++ trunk/src/main/org/jboss/envers/entities/mapper/id/EmbeddedIdMapper.java 2008-09-24 16:03:11 UTC (rev 170) @@ -101,8 +101,8 @@ List ret = new ArrayList(); - for (String propertyName : data.keySet()) { - ret.add(new QueryParameterData(propertyName, data.get(propertyName))); + for (Map.Entry propertyData : data.entrySet()) { + ret.add(new QueryParameterData(propertyData.getKey(), propertyData.getValue())); } return ret; Modified: trunk/src/main/org/jboss/envers/entities/mapper/id/MultipleIdMapper.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/id/MultipleIdMapper.java 2008-09-24 13:45:08 UTC (rev 169) +++ trunk/src/main/org/jboss/envers/entities/mapper/id/MultipleIdMapper.java 2008-09-24 16:03:11 UTC (rev 170) @@ -84,8 +84,8 @@ List ret = new ArrayList(); - for (String propertyName : data.keySet()) { - ret.add(new QueryParameterData(propertyName, data.get(propertyName))); + for (Map.Entry propertyData : data.entrySet()) { + ret.add(new QueryParameterData(propertyData.getKey(), propertyData.getValue())); } return ret; Modified: trunk/src/main/org/jboss/envers/entities/mapper/relation/AbstractCollectionMapper.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/AbstractCollectionMapper.java 2008-09-24 13:45:08 UTC (rev 169) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/AbstractCollectionMapper.java 2008-09-24 16:03:11 UTC (rev 170) @@ -1,3 +1,26 @@ +/* + * JBoss, Home of Professional Open Source + * + * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated + * by the @authors tag. All rights reserved. + * + * See the copyright.txt in the distribution for a full listing of individual + * contributors. 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.PersistentCollectionChangeData; Modified: trunk/src/main/org/jboss/envers/entities/mapper/relation/BasicCollectionMapper.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/BasicCollectionMapper.java 2008-09-24 13:45:08 UTC (rev 169) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/BasicCollectionMapper.java 2008-09-24 16:03:11 UTC (rev 170) @@ -1,3 +1,26 @@ +/* + * JBoss, Home of Professional Open Source + * + * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated + * by the @authors tag. All rights reserved. + * + * See the copyright.txt in the distribution for a full listing of individual + * contributors. 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; 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-24 13:45:08 UTC (rev 169) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/CommonCollectionMapperData.java 2008-09-24 16:03:11 UTC (rev 170) @@ -1,3 +1,26 @@ +/* + * JBoss, Home of Professional Open Source + * + * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated + * by the @authors tag. All rights reserved. + * + * See the copyright.txt in the distribution for a full listing of individual + * contributors. 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.configuration.VersionsEntitiesConfiguration; Modified: trunk/src/main/org/jboss/envers/entities/mapper/relation/ListCollectionMapper.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/ListCollectionMapper.java 2008-09-24 13:45:08 UTC (rev 169) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/ListCollectionMapper.java 2008-09-24 16:03:11 UTC (rev 170) @@ -1,3 +1,26 @@ +/* + * JBoss, Home of Professional Open Source + * + * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated + * by the @authors tag. All rights reserved. + * + * See the copyright.txt in the distribution for a full listing of individual + * contributors. 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; Modified: trunk/src/main/org/jboss/envers/entities/mapper/relation/MapCollectionMapper.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/MapCollectionMapper.java 2008-09-24 13:45:08 UTC (rev 169) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/MapCollectionMapper.java 2008-09-24 16:03:11 UTC (rev 170) @@ -1,3 +1,26 @@ +/* + * JBoss, Home of Professional Open Source + * + * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated + * by the @authors tag. All rights reserved. + * + * See the copyright.txt in the distribution for a full listing of individual + * contributors. 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; Modified: trunk/src/main/org/jboss/envers/entities/mapper/relation/MiddleComponentData.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/MiddleComponentData.java 2008-09-24 13:45:08 UTC (rev 169) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/MiddleComponentData.java 2008-09-24 16:03:11 UTC (rev 170) @@ -1,3 +1,26 @@ +/* + * JBoss, Home of Professional Open Source + * + * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated + * by the @authors tag. All rights reserved. + * + * See the copyright.txt in the distribution for a full listing of individual + * contributors. 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.relation.component.MiddleComponentMapper; 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-24 13:45:08 UTC (rev 169) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/MiddleIdData.java 2008-09-24 16:03:11 UTC (rev 170) @@ -1,3 +1,26 @@ +/* + * JBoss, Home of Professional Open Source + * + * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated + * by the @authors tag. All rights reserved. + * + * See the copyright.txt in the distribution for a full listing of individual + * contributors. 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.id.IdMapper; Modified: trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleComponentMapper.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleComponentMapper.java 2008-09-24 13:45:08 UTC (rev 169) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleComponentMapper.java 2008-09-24 16:03:11 UTC (rev 170) @@ -1,3 +1,26 @@ +/* + * JBoss, Home of Professional Open Source + * + * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated + * by the @authors tag. All rights reserved. + * + * See the copyright.txt in the distribution for a full listing of individual + * contributors. 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.component; import org.jboss.envers.entities.EntityInstantiator; Modified: trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleDummyComponentMapper.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleDummyComponentMapper.java 2008-09-24 13:45:08 UTC (rev 169) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleDummyComponentMapper.java 2008-09-24 16:03:11 UTC (rev 170) @@ -1,3 +1,26 @@ +/* + * JBoss, Home of Professional Open Source + * + * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated + * by the @authors tag. All rights reserved. + * + * See the copyright.txt in the distribution for a full listing of individual + * contributors. 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.component; import org.jboss.envers.entities.EntityInstantiator; Modified: trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleMapKeyIdComponentMapper.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleMapKeyIdComponentMapper.java 2008-09-24 13:45:08 UTC (rev 169) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleMapKeyIdComponentMapper.java 2008-09-24 16:03:11 UTC (rev 170) @@ -1,3 +1,26 @@ +/* + * JBoss, Home of Professional Open Source + * + * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated + * by the @authors tag. All rights reserved. + * + * See the copyright.txt in the distribution for a full listing of individual + * contributors. 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.component; import org.jboss.envers.entities.EntityInstantiator; Modified: trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleMapKeyPropertyComponentMapper.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleMapKeyPropertyComponentMapper.java 2008-09-24 13:45:08 UTC (rev 169) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleMapKeyPropertyComponentMapper.java 2008-09-24 16:03:11 UTC (rev 170) @@ -1,3 +1,26 @@ +/* + * JBoss, Home of Professional Open Source + * + * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated + * by the @authors tag. All rights reserved. + * + * See the copyright.txt in the distribution for a full listing of individual + * contributors. 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.component; import org.jboss.envers.entities.EntityInstantiator; Modified: trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleRelatedComponentMapper.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleRelatedComponentMapper.java 2008-09-24 13:45:08 UTC (rev 169) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleRelatedComponentMapper.java 2008-09-24 16:03:11 UTC (rev 170) @@ -1,3 +1,26 @@ +/* + * JBoss, Home of Professional Open Source + * + * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated + * by the @authors tag. All rights reserved. + * + * See the copyright.txt in the distribution for a full listing of individual + * contributors. 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.component; import org.jboss.envers.entities.EntityInstantiator; Modified: trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleSimpleComponentMapper.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleSimpleComponentMapper.java 2008-09-24 13:45:08 UTC (rev 169) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/component/MiddleSimpleComponentMapper.java 2008-09-24 16:03:11 UTC (rev 170) @@ -1,3 +1,26 @@ +/* + * JBoss, Home of Professional Open Source + * + * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated + * by the @authors tag. All rights reserved. + * + * See the copyright.txt in the distribution for a full listing of individual + * contributors. 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.component; import org.jboss.envers.entities.EntityInstantiator; 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-24 13:45:08 UTC (rev 169) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/AbstractCollectionInitializor.java 2008-09-24 16:03:11 UTC (rev 170) @@ -1,3 +1,26 @@ +/* + * JBoss, Home of Professional Open Source + * + * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated + * by the @authors tag. All rights reserved. + * + * See the copyright.txt in the distribution for a full listing of individual + * contributors. 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.initializor; import org.jboss.envers.entities.mapper.relation.query.RelationQueryGenerator; 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-24 13:45:08 UTC (rev 169) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/ArrayCollectionInitializor.java 2008-09-24 16:03:11 UTC (rev 170) @@ -1,3 +1,26 @@ +/* + * JBoss, Home of Professional Open Source + * + * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated + * by the @authors tag. All rights reserved. + * + * See the copyright.txt in the distribution for a full listing of individual + * contributors. 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.initializor; import org.jboss.envers.entities.mapper.relation.query.RelationQueryGenerator; 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-24 13:45:08 UTC (rev 169) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/BasicCollectionInitializor.java 2008-09-24 16:03:11 UTC (rev 170) @@ -1,3 +1,26 @@ +/* + * JBoss, Home of Professional Open Source + * + * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated + * by the @authors tag. All rights reserved. + * + * See the copyright.txt in the distribution for a full listing of individual + * contributors. 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.initializor; import org.jboss.envers.entities.mapper.relation.query.RelationQueryGenerator; 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-24 13:45:08 UTC (rev 169) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/ListCollectionInitializor.java 2008-09-24 16:03:11 UTC (rev 170) @@ -1,3 +1,26 @@ +/* + * JBoss, Home of Professional Open Source + * + * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated + * by the @authors tag. All rights reserved. + * + * See the copyright.txt in the distribution for a full listing of individual + * contributors. 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.initializor; import org.jboss.envers.entities.mapper.relation.query.RelationQueryGenerator; 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-24 13:45:08 UTC (rev 169) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/lazy/initializor/MapCollectionInitializor.java 2008-09-24 16:03:11 UTC (rev 170) @@ -1,3 +1,26 @@ +/* + * JBoss, Home of Professional Open Source + * + * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated + * by the @authors tag. All rights reserved. + * + * See the copyright.txt in the distribution for a full listing of individual + * contributors. 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.initializor; import org.jboss.envers.entities.mapper.relation.query.RelationQueryGenerator; 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-24 13:45:08 UTC (rev 169) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/query/OneEntityQueryGenerator.java 2008-09-24 16:03:11 UTC (rev 170) @@ -1,3 +1,26 @@ +/* + * JBoss, Home of Professional Open Source + * + * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated + * by the @authors tag. All rights reserved. + * + * See the copyright.txt in the distribution for a full listing of individual + * contributors. 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.query; import org.jboss.envers.entities.mapper.relation.MiddleIdData; Modified: trunk/src/main/org/jboss/envers/entities/mapper/relation/query/OneVersionsEntityQueryGenerator.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/query/OneVersionsEntityQueryGenerator.java 2008-09-24 13:45:08 UTC (rev 169) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/query/OneVersionsEntityQueryGenerator.java 2008-09-24 16:03:11 UTC (rev 170) @@ -1,3 +1,26 @@ +/* + * JBoss, Home of Professional Open Source + * + * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated + * by the @authors tag. All rights reserved. + * + * See the copyright.txt in the distribution for a full listing of individual + * contributors. 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.query; import org.jboss.envers.entities.mapper.id.QueryParameterData; Modified: trunk/src/main/org/jboss/envers/entities/mapper/relation/query/QueryGeneratorTools.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/query/QueryGeneratorTools.java 2008-09-24 13:45:08 UTC (rev 169) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/query/QueryGeneratorTools.java 2008-09-24 16:03:11 UTC (rev 170) @@ -1,3 +1,26 @@ +/* + * JBoss, Home of Professional Open Source + * + * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated + * by the @authors tag. All rights reserved. + * + * See the copyright.txt in the distribution for a full listing of individual + * contributors. 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.query; import org.jboss.envers.tools.query.QueryBuilder; Modified: trunk/src/main/org/jboss/envers/entities/mapper/relation/query/RelationQueryGenerator.java =================================================================== --- trunk/src/main/org/jboss/envers/entities/mapper/relation/query/RelationQueryGenerator.java 2008-09-24 13:45:08 UTC (rev 169) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/query/RelationQueryGenerator.java 2008-09-24 16:03:11 UTC (rev 170) @@ -1,3 +1,26 @@ +/* + * JBoss, Home of Professional Open Source + * + * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated + * by the @authors tag. All rights reserved. + * + * See the copyright.txt in the distribution for a full listing of individual + * contributors. 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.query; import org.hibernate.Query; 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-24 13:45:08 UTC (rev 169) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/query/ThreeEntityQueryGenerator.java 2008-09-24 16:03:11 UTC (rev 170) @@ -1,3 +1,26 @@ +/* + * JBoss, Home of Professional Open Source + * + * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated + * by the @authors tag. All rights reserved. + * + * See the copyright.txt in the distribution for a full listing of individual + * contributors. 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.query; import org.jboss.envers.entities.mapper.id.QueryParameterData; 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-24 13:45:08 UTC (rev 169) +++ trunk/src/main/org/jboss/envers/entities/mapper/relation/query/TwoEntityQueryGenerator.java 2008-09-24 16:03:11 UTC (rev 170) @@ -1,3 +1,26 @@ +/* + * JBoss, Home of Professional Open Source + * + * Copyright 2008, Red Hat Middleware LLC, and others contributors as indicated + * by the @authors tag. All rights reserved. + * + * See the copyright.txt in the distribution for a full listing of individual + * contributors. 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.query; import org.jboss.envers.entities.mapper.id.QueryParameterData; Modified: trunk/src/main/org/jboss/envers/exception/NotVersionedException.java =================================================================== --- trunk/src/main/org/jboss/envers/exception/NotVersionedException.java 2008-09-24 13:45:08 UTC (rev 169) +++ trunk/src/main/org/jboss/envers/exception/NotVersionedException.java 2008-09-24 16:03:11 UTC (rev 170) @@ -22,19 +22,17 @@ package org.jboss.envers.exception; /** - * TODO: add a field describing what isn't versioned. * @author Adam Warski (adam at warski dot org) */ public class NotVersionedException extends VersionsException { - public NotVersionedException(String message) { + private final String entityName; + + public NotVersionedException(String entityName, String message) { super(message); + this.entityName = entityName; } - public NotVersionedException(String message, Throwable cause) { - super(message, cause); + public String getEntityName() { + return entityName; } - - public NotVersionedException(Throwable cause) { - super(cause); - } } Modified: trunk/src/main/org/jboss/envers/exception/RevisionDoesNotExistException.java =================================================================== --- trunk/src/main/org/jboss/envers/exception/RevisionDoesNotExistException.java 2008-09-24 13:45:08 UTC (rev 169) +++ trunk/src/main/org/jboss/envers/exception/RevisionDoesNotExistException.java 2008-09-24 16:03:11 UTC (rev 170) @@ -24,27 +24,27 @@ import java.util.Date; /** - * TODO: add the revision number. * @author Adam Warski (adam at warski dot org) */ public class RevisionDoesNotExistException extends VersionsException { - public RevisionDoesNotExistException(String message) { - super(message); - } + private Number revision; + private Date date; - public RevisionDoesNotExistException(String message, Throwable cause) { - super(message, cause); - } - - public RevisionDoesNotExistException(Throwable cause) { - super(cause); - } - public RevisionDoesNotExistException(Number revision) { super("Revision " + revision + " does not exist."); + this.revision = revision; } public RevisionDoesNotExistException(Date date) { super("There is no revision before or at " + date + "."); + this.date = date; } + + public Number getRevision() { + return revision; + } + + public Date getDate() { + return date; + } } Modified: trunk/src/main/org/jboss/envers/reader/VersionsReaderImpl.java =================================================================== --- trunk/src/main/org/jboss/envers/reader/VersionsReaderImpl.java 2008-09-24 13:45:08 UTC (rev 169) +++ trunk/src/main/org/jboss/envers/reader/VersionsReaderImpl.java 2008-09-24 16:03:11 UTC (rev 170) @@ -80,7 +80,7 @@ String entityName = cls.getName(); if (!verCfg.getEntCfg().isVersioned(entityName)) { - throw new NotVersionedException(entityName + " is not versioned!"); + throw new NotVersionedException(entityName, entityName + " is not versioned!"); } try { @@ -104,7 +104,7 @@ String entityName = cls.getName(); if (!verCfg.getEntCfg().isVersioned(entityName)) { - throw new NotVersionedException(entityName + " is not versioned!"); + throw new NotVersionedException(entityName, entityName + " is not versioned!"); } return createQuery().forRevisionsOfEntity(cls, false, true) Modified: trunk/src/test/org/jboss/envers/test/entities/customtype/ParametrizedTestUserType.java =================================================================== --- trunk/src/test/org/jboss/envers/test/entities/customtype/ParametrizedTestUserType.java 2008-09-24 13:45:08 UTC (rev 169) +++ trunk/src/test/org/jboss/envers/test/entities/customtype/ParametrizedTestUserType.java 2008-09-24 16:03:11 UTC (rev 170) @@ -4,13 +4,13 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.sql.Types; import java.util.Properties; import org.hibernate.HibernateException; import org.hibernate.Hibernate; import org.hibernate.usertype.ParameterizedType; import org.hibernate.usertype.UserType; -import org.hsqldb.Types; /** * @author Adam Warski (adam at warski dot org) Modified: trunk/src/test/org/jboss/envers/test/integration/collection/StringMap.java =================================================================== --- trunk/src/test/org/jboss/envers/test/integration/collection/StringMap.java 2008-09-24 13:45:08 UTC (rev 169) +++ trunk/src/test/org/jboss/envers/test/integration/collection/StringMap.java 2008-09-24 16:03:11 UTC (rev 170) @@ -93,9 +93,6 @@ assert rev1.getStrings().equals(Collections.EMPTY_MAP); assert rev2.getStrings().equals(TestTools.makeMap("1", "a", "2", "b")); - System.out.println(rev2.getStrings()); - System.out.println(rev3.getStrings()); - System.out.println(rev4.getStrings()); assert rev3.getStrings().equals(TestTools.makeMap("2", "b")); assert rev4.getStrings().equals(TestTools.makeMap("2", "b")); } From jboss-envers-commits at lists.jboss.org Thu Sep 25 03:16:20 2008 From: jboss-envers-commits at lists.jboss.org (jboss-envers-commits at lists.jboss.org) Date: Thu, 25 Sep 2008 03:16:20 -0400 Subject: [jboss-envers-commits] JBoss Envers SVN: r171 - trunk/src/main/org/jboss/envers/synchronization. Message-ID: Author: adamw Date: 2008-09-25 03:16:20 -0400 (Thu, 25 Sep 2008) New Revision: 171 Modified: trunk/src/main/org/jboss/envers/synchronization/VersionsSync.java Log: Fix (by adepue) for a bug reported in the forums: http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4178431 Modified: trunk/src/main/org/jboss/envers/synchronization/VersionsSync.java =================================================================== --- trunk/src/main/org/jboss/envers/synchronization/VersionsSync.java 2008-09-24 16:03:11 UTC (rev 170) +++ trunk/src/main/org/jboss/envers/synchronization/VersionsSync.java 2008-09-25 07:16:20 UTC (rev 171) @@ -126,7 +126,8 @@ return; } - if (FlushMode.isManualFlushMode(session.getFlushMode())) { + // see: http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4178431 + if (FlushMode.isManualFlushMode(session.getFlushMode()) || session.isClosed()) { Session temporarySession = null; try { temporarySession = session.getFactory().openTemporarySession(); From jboss-envers-commits at lists.jboss.org Fri Sep 26 11:33:14 2008 From: jboss-envers-commits at lists.jboss.org (jboss-envers-commits at lists.jboss.org) Date: Fri, 26 Sep 2008 11:33:14 -0400 Subject: [jboss-envers-commits] JBoss Envers SVN: r172 - tags. Message-ID: Author: adamw Date: 2008-09-26 11:33:14 -0400 (Fri, 26 Sep 2008) New Revision: 172 Added: tags/1.1.0-beta2/ Log: Taggin the 1.0.0.beta2 release Copied: tags/1.1.0-beta2 (from rev 171, trunk)