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<QueryParameterData> 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<QueryParameterData> 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<QueryParameterData> paramDatas = mapToQueryParametersFromId(null);
-
- StringBuilder query = new StringBuilder();
-
- Iterator<QueryParameterData> 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<QueryParameterData> paramDatas1 = mapToQueryParametersFromId(null);
List<QueryParameterData> paramDatas2 =
mapper2.mapToQueryParametersFromId(null);
- StringBuilder query = new StringBuilder();
+ Parameters parametersToUse = getParametersToUse(parameters, paramDatas1);
Iterator<QueryParameterData> paramDataIter1 = paramDatas1.iterator();
Iterator<QueryParameterData> 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<QueryParameterData> paramDatas = mapToQueryParametersFromId(null);
+ public void addIdEqualsToQuery(Parameters parameters, Object id, String prefix,
boolean equals) {
+ List<QueryParameterData> paramDatas = mapToQueryParametersFromId(id);
- StringBuilder query = new StringBuilder();
+ Parameters parametersToUse = getParametersToUse(parameters, paramDatas);
- Iterator<QueryParameterData> 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<QueryParameterData> 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<QueryParameterData> 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.<String, Object>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("(");