HHH-6738 - Fix and test

This commit is contained in:
Lukasz Antoniak 2012-01-31 15:08:16 +01:00
parent d1ac167ec6
commit 69db917760
13 changed files with 319 additions and 72 deletions

View File

@ -22,8 +22,9 @@
* Boston, MA 02110-1301 USA * Boston, MA 02110-1301 USA
*/ */
package org.hibernate.envers.entities.mapper.relation.query; package org.hibernate.envers.entities.mapper.relation.query;
import java.util.Collections;
import static org.hibernate.envers.entities.mapper.relation.query.QueryConstants.*;
import java.util.Collections;
import org.hibernate.Query; import org.hibernate.Query;
import org.hibernate.envers.RevisionType; import org.hibernate.envers.RevisionType;
import org.hibernate.envers.configuration.AuditEntitiesConfiguration; import org.hibernate.envers.configuration.AuditEntitiesConfiguration;
@ -73,8 +74,8 @@ public final class OneAuditEntityQueryGenerator implements RelationQueryGenerato
String versionsReferencedEntityName = verEntCfg.getAuditEntityName(referencedEntityName); String versionsReferencedEntityName = verEntCfg.getAuditEntityName(referencedEntityName);
// SELECT e FROM versionsEntity e // SELECT e FROM versionsEntity e
QueryBuilder qb = new QueryBuilder(versionsReferencedEntityName, "e"); QueryBuilder qb = new QueryBuilder(versionsReferencedEntityName, REFERENCED_ENTITY_ALIAS);
qb.addProjection(null, "e", false, false); qb.addProjection(null, REFERENCED_ENTITY_ALIAS, false, false);
// WHERE // WHERE
Parameters rootParameters = qb.getRootParameters(); Parameters rootParameters = qb.getRootParameters();
// e.id_ref_ed = :id_ref_ed // e.id_ref_ed = :id_ref_ed
@ -84,10 +85,10 @@ public final class OneAuditEntityQueryGenerator implements RelationQueryGenerato
// --> based on auditStrategy (see above) // --> based on auditStrategy (see above)
auditStrategy.addEntityAtRevisionRestriction(globalCfg, qb, revisionPropertyPath, auditStrategy.addEntityAtRevisionRestriction(globalCfg, qb, revisionPropertyPath,
verEntCfg.getRevisionEndFieldName(), true, referencedIdData, verEntCfg.getRevisionEndFieldName(), true, referencedIdData,
revisionPropertyPath, originalIdPropertyName, "e", "e2"); revisionPropertyPath, originalIdPropertyName, REFERENCED_ENTITY_ALIAS, REFERENCED_ENTITY_ALIAS_DEF_AUD_STR);
// e.revision_type != DEL // e.revision_type != DEL
rootParameters.addWhereWithNamedParam(verEntCfg.getRevisionTypePropName(), false, "!=", "delrevisiontype"); rootParameters.addWhereWithNamedParam(verEntCfg.getRevisionTypePropName(), false, "!=", DEL_REVISION_TYPE_PARAMETER);
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
qb.build(sb, Collections.<String, Object>emptyMap()); qb.build(sb, Collections.<String, Object>emptyMap());
@ -96,8 +97,8 @@ public final class OneAuditEntityQueryGenerator implements RelationQueryGenerato
public Query getQuery(AuditReaderImplementor versionsReader, Object primaryKey, Number revision) { public Query getQuery(AuditReaderImplementor versionsReader, Object primaryKey, Number revision) {
Query query = versionsReader.getSession().createQuery(queryString); Query query = versionsReader.getSession().createQuery(queryString);
query.setParameter("revision", revision); query.setParameter(REVISION_PARAMETER, revision);
query.setParameter("delrevisiontype", RevisionType.DEL); query.setParameter(DEL_REVISION_TYPE_PARAMETER, RevisionType.DEL);
for (QueryParameterData paramData: referencingIdData.getPrefixedMapper().mapToQueryParametersFromId(primaryKey)) { for (QueryParameterData paramData: referencingIdData.getPrefixedMapper().mapToQueryParametersFromId(primaryKey)) {
paramData.setParameterValue(query); paramData.setParameterValue(query);
} }

View File

@ -22,8 +22,9 @@
* Boston, MA 02110-1301 USA * Boston, MA 02110-1301 USA
*/ */
package org.hibernate.envers.entities.mapper.relation.query; package org.hibernate.envers.entities.mapper.relation.query;
import java.util.Collections;
import static org.hibernate.envers.entities.mapper.relation.query.QueryConstants.*;
import java.util.Collections;
import org.hibernate.Query; import org.hibernate.Query;
import org.hibernate.envers.RevisionType; import org.hibernate.envers.RevisionType;
import org.hibernate.envers.configuration.AuditEntitiesConfiguration; import org.hibernate.envers.configuration.AuditEntitiesConfiguration;
@ -73,14 +74,14 @@ public final class OneEntityQueryGenerator implements RelationQueryGenerator {
String originalIdPropertyName = verEntCfg.getOriginalIdPropName(); String originalIdPropertyName = verEntCfg.getOriginalIdPropName();
// SELECT ee FROM middleEntity ee // SELECT ee FROM middleEntity ee
QueryBuilder qb = new QueryBuilder(versionsMiddleEntityName, "ee"); QueryBuilder qb = new QueryBuilder(versionsMiddleEntityName, MIDDLE_ENTITY_ALIAS);
qb.addProjection(null, "ee", false, false); qb.addProjection(null, MIDDLE_ENTITY_ALIAS, false, false);
// WHERE // WHERE
Parameters rootParameters = qb.getRootParameters(); Parameters rootParameters = qb.getRootParameters();
// ee.originalId.id_ref_ing = :id_ref_ing // ee.originalId.id_ref_ing = :id_ref_ing
referencingIdData.getPrefixedMapper().addNamedIdEqualsToQuery(rootParameters, originalIdPropertyName, true); referencingIdData.getPrefixedMapper().addNamedIdEqualsToQuery(rootParameters, originalIdPropertyName, true);
String eeOriginalIdPropertyPath = "ee." + originalIdPropertyName; String eeOriginalIdPropertyPath = MIDDLE_ENTITY_ALIAS + "." + originalIdPropertyName;
// (with ee association at revision :revision) // (with ee association at revision :revision)
// --> based on auditStrategy (see above) // --> based on auditStrategy (see above)
@ -89,7 +90,7 @@ public final class OneEntityQueryGenerator implements RelationQueryGenerator {
eeOriginalIdPropertyPath, revisionPropertyPath, originalIdPropertyName, componentDatas); eeOriginalIdPropertyPath, revisionPropertyPath, originalIdPropertyName, componentDatas);
// ee.revision_type != DEL // ee.revision_type != DEL
rootParameters.addWhereWithNamedParam(verEntCfg.getRevisionTypePropName(), "!=", "delrevisiontype"); rootParameters.addWhereWithNamedParam(verEntCfg.getRevisionTypePropName(), "!=", DEL_REVISION_TYPE_PARAMETER);
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
qb.build(sb, Collections.<String, Object>emptyMap()); qb.build(sb, Collections.<String, Object>emptyMap());
@ -98,8 +99,8 @@ public final class OneEntityQueryGenerator implements RelationQueryGenerator {
public Query getQuery(AuditReaderImplementor versionsReader, Object primaryKey, Number revision) { public Query getQuery(AuditReaderImplementor versionsReader, Object primaryKey, Number revision) {
Query query = versionsReader.getSession().createQuery(queryString); Query query = versionsReader.getSession().createQuery(queryString);
query.setParameter("revision", revision); query.setParameter(REVISION_PARAMETER, revision);
query.setParameter("delrevisiontype", RevisionType.DEL); query.setParameter(DEL_REVISION_TYPE_PARAMETER, RevisionType.DEL);
for (QueryParameterData paramData: referencingIdData.getPrefixedMapper().mapToQueryParametersFromId(primaryKey)) { for (QueryParameterData paramData: referencingIdData.getPrefixedMapper().mapToQueryParametersFromId(primaryKey)) {
paramData.setParameterValue(query); paramData.setParameterValue(query);
} }

View File

@ -0,0 +1,20 @@
package org.hibernate.envers.entities.mapper.relation.query;
/**
* Constants used in JPQL queries.
*
* @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
*/
public class QueryConstants {
public static final String REFERENCED_ENTITY_ALIAS = "e_h";
public static final String REFERENCED_ENTITY_ALIAS_DEF_AUD_STR = "e2_h";
public static final String INDEX_ENTITY_ALIAS = "f_h";
public static final String INDEX_ENTITY_ALIAS_DEF_AUD_STR = "f2_h";
public static final String MIDDLE_ENTITY_ALIAS = "ee_h";
public static final String MIDDLE_ENTITY_ALIAS_DEF_AUD_STR = "ee2_h";
public static final String REVISION_PARAMETER = "revision";
public static final String DEL_REVISION_TYPE_PARAMETER = "delrevisiontype";
}

View File

@ -22,8 +22,9 @@
* Boston, MA 02110-1301 USA * Boston, MA 02110-1301 USA
*/ */
package org.hibernate.envers.entities.mapper.relation.query; package org.hibernate.envers.entities.mapper.relation.query;
import java.util.Collections;
import static org.hibernate.envers.entities.mapper.relation.query.QueryConstants.*;
import java.util.Collections;
import org.hibernate.Query; import org.hibernate.Query;
import org.hibernate.envers.RevisionType; import org.hibernate.envers.RevisionType;
import org.hibernate.envers.configuration.AuditEntitiesConfiguration; import org.hibernate.envers.configuration.AuditEntitiesConfiguration;
@ -114,35 +115,35 @@ public final class ThreeEntityQueryGenerator implements RelationQueryGenerator {
*/ */
String revisionPropertyPath = verEntCfg.getRevisionNumberPath(); String revisionPropertyPath = verEntCfg.getRevisionNumberPath();
String originalIdPropertyName = verEntCfg.getOriginalIdPropName(); String originalIdPropertyName = verEntCfg.getOriginalIdPropName();
String eeOriginalIdPropertyPath = "ee." + originalIdPropertyName; String eeOriginalIdPropertyPath = MIDDLE_ENTITY_ALIAS + "." + originalIdPropertyName;
// SELECT new list(ee) FROM middleEntity ee // SELECT new list(ee) FROM middleEntity ee
QueryBuilder qb = new QueryBuilder(versionsMiddleEntityName, "ee"); QueryBuilder qb = new QueryBuilder(versionsMiddleEntityName, MIDDLE_ENTITY_ALIAS);
qb.addFrom(referencedIdData.getAuditEntityName(), "e"); qb.addFrom(referencedIdData.getAuditEntityName(), REFERENCED_ENTITY_ALIAS);
qb.addFrom(indexIdData.getAuditEntityName(), "f"); qb.addFrom(indexIdData.getAuditEntityName(), INDEX_ENTITY_ALIAS);
qb.addProjection("new list", "ee, e, f", false, false); qb.addProjection("new list", MIDDLE_ENTITY_ALIAS + ", " + REFERENCED_ENTITY_ALIAS + ", " + INDEX_ENTITY_ALIAS, false, false);
// WHERE // WHERE
Parameters rootParameters = qb.getRootParameters(); Parameters rootParameters = qb.getRootParameters();
// ee.id_ref_ed = e.id_ref_ed // ee.id_ref_ed = e.id_ref_ed
referencedIdData.getPrefixedMapper().addIdsEqualToQuery(rootParameters, eeOriginalIdPropertyPath, referencedIdData.getPrefixedMapper().addIdsEqualToQuery(rootParameters, eeOriginalIdPropertyPath,
referencedIdData.getOriginalMapper(), "e." + originalIdPropertyName); referencedIdData.getOriginalMapper(), REFERENCED_ENTITY_ALIAS + "." + originalIdPropertyName);
// ee.id_ref_ind = f.id_ref_ind // ee.id_ref_ind = f.id_ref_ind
indexIdData.getPrefixedMapper().addIdsEqualToQuery(rootParameters, eeOriginalIdPropertyPath, indexIdData.getPrefixedMapper().addIdsEqualToQuery(rootParameters, eeOriginalIdPropertyPath,
indexIdData.getOriginalMapper(), "f." + originalIdPropertyName); indexIdData.getOriginalMapper(), INDEX_ENTITY_ALIAS + "." + originalIdPropertyName);
// ee.originalId.id_ref_ing = :id_ref_ing // ee.originalId.id_ref_ing = :id_ref_ing
referencingIdData.getPrefixedMapper().addNamedIdEqualsToQuery(rootParameters, originalIdPropertyName, true); referencingIdData.getPrefixedMapper().addNamedIdEqualsToQuery(rootParameters, originalIdPropertyName, true);
// (selecting e entities at revision :revision) // (selecting e entities at revision :revision)
// --> based on auditStrategy (see above) // --> based on auditStrategy (see above)
auditStrategy.addEntityAtRevisionRestriction(globalCfg, qb, "e." + revisionPropertyPath, auditStrategy.addEntityAtRevisionRestriction(globalCfg, qb, REFERENCED_ENTITY_ALIAS + "." + revisionPropertyPath,
"e." + verEntCfg.getRevisionEndFieldName(), false, REFERENCED_ENTITY_ALIAS + "." + verEntCfg.getRevisionEndFieldName(), false,
referencedIdData, revisionPropertyPath, originalIdPropertyName, "e", "e2"); referencedIdData, revisionPropertyPath, originalIdPropertyName, REFERENCED_ENTITY_ALIAS, REFERENCED_ENTITY_ALIAS_DEF_AUD_STR);
// (selecting f entities at revision :revision) // (selecting f entities at revision :revision)
// --> based on auditStrategy (see above) // --> based on auditStrategy (see above)
auditStrategy.addEntityAtRevisionRestriction(globalCfg, qb, "e." + revisionPropertyPath, auditStrategy.addEntityAtRevisionRestriction(globalCfg, qb, REFERENCED_ENTITY_ALIAS + "." + revisionPropertyPath,
"e." + verEntCfg.getRevisionEndFieldName(), false, REFERENCED_ENTITY_ALIAS + "." + verEntCfg.getRevisionEndFieldName(), false,
referencedIdData, revisionPropertyPath, originalIdPropertyName, "f", "f2"); referencedIdData, revisionPropertyPath, originalIdPropertyName, INDEX_ENTITY_ALIAS, INDEX_ENTITY_ALIAS_DEF_AUD_STR);
// (with ee association at revision :revision) // (with ee association at revision :revision)
// --> based on auditStrategy (see above) // --> based on auditStrategy (see above)
@ -151,11 +152,11 @@ public final class ThreeEntityQueryGenerator implements RelationQueryGenerator {
eeOriginalIdPropertyPath, revisionPropertyPath, originalIdPropertyName, componentDatas); eeOriginalIdPropertyPath, revisionPropertyPath, originalIdPropertyName, componentDatas);
// ee.revision_type != DEL // ee.revision_type != DEL
rootParameters.addWhereWithNamedParam(verEntCfg.getRevisionTypePropName(), "!=", "delrevisiontype"); rootParameters.addWhereWithNamedParam(verEntCfg.getRevisionTypePropName(), "!=", DEL_REVISION_TYPE_PARAMETER);
// e.revision_type != DEL // e.revision_type != DEL
rootParameters.addWhereWithNamedParam("e." + verEntCfg.getRevisionTypePropName(), false, "!=", "delrevisiontype"); rootParameters.addWhereWithNamedParam(REFERENCED_ENTITY_ALIAS + "." + verEntCfg.getRevisionTypePropName(), false, "!=", DEL_REVISION_TYPE_PARAMETER);
// f.revision_type != DEL // f.revision_type != DEL
rootParameters.addWhereWithNamedParam("f." + verEntCfg.getRevisionTypePropName(), false, "!=", "delrevisiontype"); rootParameters.addWhereWithNamedParam(INDEX_ENTITY_ALIAS + "." + verEntCfg.getRevisionTypePropName(), false, "!=", DEL_REVISION_TYPE_PARAMETER);
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
qb.build(sb, Collections.<String, Object>emptyMap()); qb.build(sb, Collections.<String, Object>emptyMap());
@ -164,8 +165,8 @@ public final class ThreeEntityQueryGenerator implements RelationQueryGenerator {
public Query getQuery(AuditReaderImplementor versionsReader, Object primaryKey, Number revision) { public Query getQuery(AuditReaderImplementor versionsReader, Object primaryKey, Number revision) {
Query query = versionsReader.getSession().createQuery(queryString); Query query = versionsReader.getSession().createQuery(queryString);
query.setParameter("revision", revision); query.setParameter(REVISION_PARAMETER, revision);
query.setParameter("delrevisiontype", RevisionType.DEL); query.setParameter(DEL_REVISION_TYPE_PARAMETER, RevisionType.DEL);
for (QueryParameterData paramData: referencingIdData.getPrefixedMapper().mapToQueryParametersFromId(primaryKey)) { for (QueryParameterData paramData: referencingIdData.getPrefixedMapper().mapToQueryParametersFromId(primaryKey)) {
paramData.setParameterValue(query); paramData.setParameterValue(query);
} }

View File

@ -22,8 +22,9 @@
* Boston, MA 02110-1301 USA * Boston, MA 02110-1301 USA
*/ */
package org.hibernate.envers.entities.mapper.relation.query; package org.hibernate.envers.entities.mapper.relation.query;
import java.util.Collections;
import static org.hibernate.envers.entities.mapper.relation.query.QueryConstants.*;
import java.util.Collections;
import org.hibernate.Query; import org.hibernate.Query;
import org.hibernate.envers.RevisionType; import org.hibernate.envers.RevisionType;
import org.hibernate.envers.configuration.AuditEntitiesConfiguration; import org.hibernate.envers.configuration.AuditEntitiesConfiguration;
@ -75,17 +76,17 @@ public final class TwoEntityOneAuditedQueryGenerator implements RelationQueryGen
String revisionPropertyPath = verEntCfg.getRevisionNumberPath(); String revisionPropertyPath = verEntCfg.getRevisionNumberPath();
String originalIdPropertyName = verEntCfg.getOriginalIdPropName(); String originalIdPropertyName = verEntCfg.getOriginalIdPropName();
String eeOriginalIdPropertyPath = "ee." + originalIdPropertyName; String eeOriginalIdPropertyPath = MIDDLE_ENTITY_ALIAS + "." + originalIdPropertyName;
// SELECT new list(ee) FROM middleEntity ee // SELECT new list(ee) FROM middleEntity ee
QueryBuilder qb = new QueryBuilder(versionsMiddleEntityName, "ee"); QueryBuilder qb = new QueryBuilder(versionsMiddleEntityName, MIDDLE_ENTITY_ALIAS);
qb.addFrom(referencedIdData.getEntityName(), "e"); qb.addFrom(referencedIdData.getEntityName(), REFERENCED_ENTITY_ALIAS);
qb.addProjection("new list", "ee, e", false, false); qb.addProjection("new list", MIDDLE_ENTITY_ALIAS + ", " + REFERENCED_ENTITY_ALIAS, false, false);
// WHERE // WHERE
Parameters rootParameters = qb.getRootParameters(); Parameters rootParameters = qb.getRootParameters();
// ee.id_ref_ed = e.id_ref_ed // ee.id_ref_ed = e.id_ref_ed
referencedIdData.getPrefixedMapper().addIdsEqualToQuery(rootParameters, eeOriginalIdPropertyPath, referencedIdData.getPrefixedMapper().addIdsEqualToQuery(rootParameters, eeOriginalIdPropertyPath,
referencedIdData.getOriginalMapper(), "e"); referencedIdData.getOriginalMapper(), REFERENCED_ENTITY_ALIAS);
// ee.originalId.id_ref_ing = :id_ref_ing // ee.originalId.id_ref_ing = :id_ref_ing
referencingIdData.getPrefixedMapper().addNamedIdEqualsToQuery(rootParameters, originalIdPropertyName, true); referencingIdData.getPrefixedMapper().addNamedIdEqualsToQuery(rootParameters, originalIdPropertyName, true);
@ -96,7 +97,7 @@ public final class TwoEntityOneAuditedQueryGenerator implements RelationQueryGen
eeOriginalIdPropertyPath, revisionPropertyPath, originalIdPropertyName, componentDatas); eeOriginalIdPropertyPath, revisionPropertyPath, originalIdPropertyName, componentDatas);
// ee.revision_type != DEL // ee.revision_type != DEL
rootParameters.addWhereWithNamedParam(verEntCfg.getRevisionTypePropName(), "!=", "delrevisiontype"); rootParameters.addWhereWithNamedParam(verEntCfg.getRevisionTypePropName(), "!=", DEL_REVISION_TYPE_PARAMETER);
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
qb.build(sb, Collections.<String, Object>emptyMap()); qb.build(sb, Collections.<String, Object>emptyMap());
@ -105,8 +106,8 @@ public final class TwoEntityOneAuditedQueryGenerator implements RelationQueryGen
public Query getQuery(AuditReaderImplementor versionsReader, Object primaryKey, Number revision) { public Query getQuery(AuditReaderImplementor versionsReader, Object primaryKey, Number revision) {
Query query = versionsReader.getSession().createQuery(queryString); Query query = versionsReader.getSession().createQuery(queryString);
query.setParameter("revision", revision); query.setParameter(REVISION_PARAMETER, revision);
query.setParameter("delrevisiontype", RevisionType.DEL); query.setParameter(DEL_REVISION_TYPE_PARAMETER, RevisionType.DEL);
for (QueryParameterData paramData: referencingIdData.getPrefixedMapper().mapToQueryParametersFromId(primaryKey)) { for (QueryParameterData paramData: referencingIdData.getPrefixedMapper().mapToQueryParametersFromId(primaryKey)) {
paramData.setParameterValue(query); paramData.setParameterValue(query);
} }

View File

@ -22,8 +22,9 @@
* Boston, MA 02110-1301 USA * Boston, MA 02110-1301 USA
*/ */
package org.hibernate.envers.entities.mapper.relation.query; package org.hibernate.envers.entities.mapper.relation.query;
import java.util.Collections;
import static org.hibernate.envers.entities.mapper.relation.query.QueryConstants.*;
import java.util.Collections;
import org.hibernate.Query; import org.hibernate.Query;
import org.hibernate.envers.RevisionType; import org.hibernate.envers.RevisionType;
import org.hibernate.envers.configuration.AuditEntitiesConfiguration; import org.hibernate.envers.configuration.AuditEntitiesConfiguration;
@ -87,25 +88,25 @@ public final class TwoEntityQueryGenerator implements RelationQueryGenerator {
String revisionPropertyPath = verEntCfg.getRevisionNumberPath(); String revisionPropertyPath = verEntCfg.getRevisionNumberPath();
String originalIdPropertyName = verEntCfg.getOriginalIdPropName(); String originalIdPropertyName = verEntCfg.getOriginalIdPropName();
String eeOriginalIdPropertyPath = "ee." + originalIdPropertyName; String eeOriginalIdPropertyPath = MIDDLE_ENTITY_ALIAS + "." + originalIdPropertyName;
// SELECT new list(ee) FROM middleEntity ee // SELECT new list(ee) FROM middleEntity ee
QueryBuilder qb = new QueryBuilder(versionsMiddleEntityName, "ee"); QueryBuilder qb = new QueryBuilder(versionsMiddleEntityName, MIDDLE_ENTITY_ALIAS);
qb.addFrom(referencedIdData.getAuditEntityName(), "e"); qb.addFrom(referencedIdData.getAuditEntityName(), REFERENCED_ENTITY_ALIAS);
qb.addProjection("new list", "ee, e", false, false); qb.addProjection("new list", MIDDLE_ENTITY_ALIAS + ", " + REFERENCED_ENTITY_ALIAS, false, false);
// WHERE // WHERE
Parameters rootParameters = qb.getRootParameters(); Parameters rootParameters = qb.getRootParameters();
// ee.id_ref_ed = e.id_ref_ed // ee.id_ref_ed = e.id_ref_ed
referencedIdData.getPrefixedMapper().addIdsEqualToQuery(rootParameters, eeOriginalIdPropertyPath, referencedIdData.getPrefixedMapper().addIdsEqualToQuery(rootParameters, eeOriginalIdPropertyPath,
referencedIdData.getOriginalMapper(), "e." + originalIdPropertyName); referencedIdData.getOriginalMapper(), REFERENCED_ENTITY_ALIAS + "." + originalIdPropertyName);
// ee.originalId.id_ref_ing = :id_ref_ing // ee.originalId.id_ref_ing = :id_ref_ing
referencingIdData.getPrefixedMapper().addNamedIdEqualsToQuery(rootParameters, originalIdPropertyName, true); referencingIdData.getPrefixedMapper().addNamedIdEqualsToQuery(rootParameters, originalIdPropertyName, true);
// (selecting e entities at revision :revision) // (selecting e entities at revision :revision)
// --> based on auditStrategy (see above) // --> based on auditStrategy (see above)
auditStrategy.addEntityAtRevisionRestriction(globalCfg, qb, "e." + revisionPropertyPath, auditStrategy.addEntityAtRevisionRestriction(globalCfg, qb, REFERENCED_ENTITY_ALIAS + "." + revisionPropertyPath,
"e." + verEntCfg.getRevisionEndFieldName(), false, REFERENCED_ENTITY_ALIAS + "." + verEntCfg.getRevisionEndFieldName(), false,
referencedIdData, revisionPropertyPath, originalIdPropertyName, "e", "e2"); referencedIdData, revisionPropertyPath, originalIdPropertyName, REFERENCED_ENTITY_ALIAS, REFERENCED_ENTITY_ALIAS_DEF_AUD_STR);
// (with ee association at revision :revision) // (with ee association at revision :revision)
// --> based on auditStrategy (see above) // --> based on auditStrategy (see above)
@ -114,9 +115,9 @@ public final class TwoEntityQueryGenerator implements RelationQueryGenerator {
eeOriginalIdPropertyPath, revisionPropertyPath, originalIdPropertyName, componentDatas); eeOriginalIdPropertyPath, revisionPropertyPath, originalIdPropertyName, componentDatas);
// ee.revision_type != DEL // ee.revision_type != DEL
rootParameters.addWhereWithNamedParam(verEntCfg.getRevisionTypePropName(), "!=", "delrevisiontype"); rootParameters.addWhereWithNamedParam(verEntCfg.getRevisionTypePropName(), "!=", DEL_REVISION_TYPE_PARAMETER);
// e.revision_type != DEL // e.revision_type != DEL
rootParameters.addWhereWithNamedParam("e." + verEntCfg.getRevisionTypePropName(), false, "!=", "delrevisiontype"); rootParameters.addWhereWithNamedParam(REFERENCED_ENTITY_ALIAS + "." + verEntCfg.getRevisionTypePropName(), false, "!=", DEL_REVISION_TYPE_PARAMETER);
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
qb.build(sb, Collections.<String, Object>emptyMap()); qb.build(sb, Collections.<String, Object>emptyMap());
@ -125,8 +126,8 @@ public final class TwoEntityQueryGenerator implements RelationQueryGenerator {
public Query getQuery(AuditReaderImplementor versionsReader, Object primaryKey, Number revision) { public Query getQuery(AuditReaderImplementor versionsReader, Object primaryKey, Number revision) {
Query query = versionsReader.getSession().createQuery(queryString); Query query = versionsReader.getSession().createQuery(queryString);
query.setParameter("revision", revision); query.setParameter(REVISION_PARAMETER, revision);
query.setParameter("delrevisiontype", RevisionType.DEL); query.setParameter(DEL_REVISION_TYPE_PARAMETER, RevisionType.DEL);
for (QueryParameterData paramData: referencingIdData.getPrefixedMapper().mapToQueryParametersFromId(primaryKey)) { for (QueryParameterData paramData: referencingIdData.getPrefixedMapper().mapToQueryParametersFromId(primaryKey)) {
paramData.setParameterValue(query); paramData.setParameterValue(query);
} }

View File

@ -22,11 +22,12 @@
* Boston, MA 02110-1301 USA * Boston, MA 02110-1301 USA
*/ */
package org.hibernate.envers.query.impl; package org.hibernate.envers.query.impl;
import static org.hibernate.envers.entities.mapper.relation.query.QueryConstants.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import javax.persistence.NoResultException; import javax.persistence.NoResultException;
import javax.persistence.NonUniqueResultException; import javax.persistence.NonUniqueResultException;
import org.hibernate.CacheMode; import org.hibernate.CacheMode;
import org.hibernate.FlushMode; import org.hibernate.FlushMode;
import org.hibernate.LockMode; import org.hibernate.LockMode;
@ -81,7 +82,7 @@ public abstract class AbstractAuditQuery implements AuditQuery {
versionsEntityName = verCfg.getAuditEntCfg().getAuditEntityName( versionsEntityName = verCfg.getAuditEntCfg().getAuditEntityName(
entityName); entityName);
qb = new QueryBuilder(versionsEntityName, "e"); qb = new QueryBuilder(versionsEntityName, REFERENCED_ENTITY_ALIAS);
} }
protected Query buildQuery() { protected Query buildQuery() {
@ -220,7 +221,7 @@ public abstract class AbstractAuditQuery implements AuditQuery {
if (cacheMode != null) query.setCacheMode(cacheMode); if (cacheMode != null) query.setCacheMode(cacheMode);
if (timeout != null) query.setTimeout(timeout); if (timeout != null) query.setTimeout(timeout);
if (lockOptions != null && lockOptions.getLockMode() != LockMode.NONE) { if (lockOptions != null && lockOptions.getLockMode() != LockMode.NONE) {
query.setLockMode("e", lockOptions.getLockMode()); query.setLockMode(REFERENCED_ENTITY_ALIAS, lockOptions.getLockMode());
} }
} }
} }

View File

@ -22,10 +22,11 @@
* Boston, MA 02110-1301 USA * Boston, MA 02110-1301 USA
*/ */
package org.hibernate.envers.query.impl; package org.hibernate.envers.query.impl;
import static org.hibernate.envers.entities.mapper.relation.query.QueryConstants.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import org.hibernate.Query; import org.hibernate.Query;
import org.hibernate.envers.RevisionType; import org.hibernate.envers.RevisionType;
import org.hibernate.envers.configuration.AuditConfiguration; import org.hibernate.envers.configuration.AuditConfiguration;
@ -84,7 +85,7 @@ public class EntitiesAtRevisionQuery extends AbstractAuditQuery {
// --> based on auditStrategy (see above) // --> based on auditStrategy (see above)
verCfg.getAuditStrategy().addEntityAtRevisionRestriction(verCfg.getGlobalCfg(), qb, revisionPropertyPath, verCfg.getAuditStrategy().addEntityAtRevisionRestriction(verCfg.getGlobalCfg(), qb, revisionPropertyPath,
verEntCfg.getRevisionEndFieldName(), true, referencedIdData, verEntCfg.getRevisionEndFieldName(), true, referencedIdData,
revisionPropertyPath, originalIdPropertyName, "e", "e2"); revisionPropertyPath, originalIdPropertyName, REFERENCED_ENTITY_ALIAS, REFERENCED_ENTITY_ALIAS_DEF_AUD_STR);
// e.revision_type != DEL // e.revision_type != DEL
qb.getRootParameters().addWhereWithParam(verEntCfg.getRevisionTypePropName(), "<>", RevisionType.DEL); qb.getRootParameters().addWhereWithParam(verEntCfg.getRevisionTypePropName(), "<>", RevisionType.DEL);
@ -97,8 +98,8 @@ public class EntitiesAtRevisionQuery extends AbstractAuditQuery {
Query query = buildQuery(); Query query = buildQuery();
// add named parameter (only used for ValidAuditTimeStrategy) // add named parameter (only used for ValidAuditTimeStrategy)
List<String> params = Arrays.asList(query.getNamedParameters()); List<String> params = Arrays.asList(query.getNamedParameters());
if (params.contains("revision")) { if (params.contains(REVISION_PARAMETER)) {
query.setParameter("revision", revision); query.setParameter(REVISION_PARAMETER, revision);
} }
List queryResult = query.list(); List queryResult = query.list();

View File

@ -1,6 +1,7 @@
package org.hibernate.envers.strategy; package org.hibernate.envers.strategy;
import java.io.Serializable;
import static org.hibernate.envers.entities.mapper.relation.query.QueryConstants.*;
import java.io.Serializable;
import org.hibernate.Session; import org.hibernate.Session;
import org.hibernate.envers.configuration.AuditConfiguration; import org.hibernate.envers.configuration.AuditConfiguration;
import org.hibernate.envers.configuration.GlobalConfiguration; import org.hibernate.envers.configuration.GlobalConfiguration;
@ -49,7 +50,7 @@ public class DefaultAuditStrategy implements AuditStrategy {
// WHERE // WHERE
Parameters maxERevQbParameters = maxERevQb.getRootParameters(); Parameters maxERevQbParameters = maxERevQb.getRootParameters();
// e2.revision <= :revision // e2.revision <= :revision
maxERevQbParameters.addWhereWithNamedParam(revisionPropertyPath, "<=", "revision"); maxERevQbParameters.addWhereWithNamedParam(revisionPropertyPath, "<=", REVISION_PARAMETER);
// e2.id_ref_ed = e.id_ref_ed // e2.id_ref_ed = e.id_ref_ed
idData.getOriginalMapper().addIdsEqualToQuery(maxERevQbParameters, idData.getOriginalMapper().addIdsEqualToQuery(maxERevQbParameters,
alias1 + "." + originalIdPropertyName, alias2 +"." + originalIdPropertyName); alias1 + "." + originalIdPropertyName, alias2 +"." + originalIdPropertyName);
@ -66,14 +67,14 @@ public class DefaultAuditStrategy implements AuditStrategy {
Parameters rootParameters = rootQueryBuilder.getRootParameters(); Parameters rootParameters = rootQueryBuilder.getRootParameters();
// SELECT max(ee2.revision) FROM middleEntity ee2 // SELECT max(ee2.revision) FROM middleEntity ee2
QueryBuilder maxEeRevQb = rootQueryBuilder.newSubQueryBuilder(versionsMiddleEntityName, "ee2"); QueryBuilder maxEeRevQb = rootQueryBuilder.newSubQueryBuilder(versionsMiddleEntityName, MIDDLE_ENTITY_ALIAS_DEF_AUD_STR);
maxEeRevQb.addProjection("max", revisionPropertyPath, false); maxEeRevQb.addProjection("max", revisionPropertyPath, false);
// WHERE // WHERE
Parameters maxEeRevQbParameters = maxEeRevQb.getRootParameters(); Parameters maxEeRevQbParameters = maxEeRevQb.getRootParameters();
// ee2.revision <= :revision // ee2.revision <= :revision
maxEeRevQbParameters.addWhereWithNamedParam(revisionPropertyPath, "<=", "revision"); maxEeRevQbParameters.addWhereWithNamedParam(revisionPropertyPath, "<=", REVISION_PARAMETER);
// ee2.originalId.* = ee.originalId.* // ee2.originalId.* = ee.originalId.*
String ee2OriginalIdPropertyPath = "ee2." + originalIdPropertyName; String ee2OriginalIdPropertyPath = MIDDLE_ENTITY_ALIAS_DEF_AUD_STR + "." + originalIdPropertyName;
referencingIdData.getPrefixedMapper().addIdsEqualToQuery(maxEeRevQbParameters, eeOriginalIdPropertyPath, ee2OriginalIdPropertyPath); referencingIdData.getPrefixedMapper().addIdsEqualToQuery(maxEeRevQbParameters, eeOriginalIdPropertyPath, ee2OriginalIdPropertyPath);
for (MiddleComponentData componentData : componentDatas) { for (MiddleComponentData componentData : componentDatas) {
componentData.getComponentMapper().addMiddleEqualToQuery(maxEeRevQbParameters, eeOriginalIdPropertyPath, ee2OriginalIdPropertyPath); componentData.getComponentMapper().addMiddleEqualToQuery(maxEeRevQbParameters, eeOriginalIdPropertyPath, ee2OriginalIdPropertyPath);

View File

@ -1,9 +1,10 @@
package org.hibernate.envers.strategy; package org.hibernate.envers.strategy;
import static org.hibernate.envers.entities.mapper.relation.query.QueryConstants.*;
import java.io.Serializable; import java.io.Serializable;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.hibernate.LockOptions; import org.hibernate.LockOptions;
import org.hibernate.Session; import org.hibernate.Session;
import org.hibernate.envers.RevisionType; import org.hibernate.envers.RevisionType;
@ -64,7 +65,7 @@ public class ValidityAuditStrategy implements AuditStrategy {
select e from audited_ent e where e.end_rev is null and e.id = :id select e from audited_ent e where e.end_rev is null and e.id = :id
*/ */
QueryBuilder qb = new QueryBuilder(auditedEntityName, "e"); QueryBuilder qb = new QueryBuilder(auditedEntityName, MIDDLE_ENTITY_ALIAS);
// e.id = :id // e.id = :id
IdMapper idMapper = auditCfg.getEntCfg().get(entityName).getIdMapper(); IdMapper idMapper = auditCfg.getEntCfg().get(entityName).getIdMapper();
@ -87,7 +88,7 @@ public class ValidityAuditStrategy implements AuditStrategy {
public void performCollectionChange(Session session, AuditConfiguration auditCfg, public void performCollectionChange(Session session, AuditConfiguration auditCfg,
PersistentCollectionChangeData persistentCollectionChangeData, Object revision) { PersistentCollectionChangeData persistentCollectionChangeData, Object revision) {
final QueryBuilder qb = new QueryBuilder(persistentCollectionChangeData.getEntityName(), "e"); final QueryBuilder qb = new QueryBuilder(persistentCollectionChangeData.getEntityName(), MIDDLE_ENTITY_ALIAS);
// Adding a parameter for each id component, except the rev number // Adding a parameter for each id component, except the rev number
final String originalIdPropName = auditCfg.getAuditEntCfg().getOriginalIdPropName(); final String originalIdPropName = auditCfg.getAuditEntCfg().getOriginalIdPropName();
@ -146,8 +147,8 @@ public class ValidityAuditStrategy implements AuditStrategy {
// e.revision <= _revision and (e.endRevision > _revision or e.endRevision is null) // e.revision <= _revision and (e.endRevision > _revision or e.endRevision is null)
Parameters subParm = rootParameters.addSubParameters("or"); Parameters subParm = rootParameters.addSubParameters("or");
rootParameters.addWhereWithNamedParam(revisionProperty, addAlias, "<=", "revision"); rootParameters.addWhereWithNamedParam(revisionProperty, addAlias, "<=", REVISION_PARAMETER);
subParm.addWhereWithNamedParam(revisionEndProperty + ".id", addAlias, ">", "revision"); subParm.addWhereWithNamedParam(revisionEndProperty + ".id", addAlias, ">", REVISION_PARAMETER);
subParm.addWhere(revisionEndProperty, addAlias, "is", "null", false); subParm.addWhere(revisionEndProperty, addAlias, "is", "null", false);
} }

View File

@ -0,0 +1,74 @@
package ee.estonia.entities;
import org.hibernate.envers.Audited;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import java.io.Serializable;
/**
* @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
*/
@Entity
@Audited
public class Child implements Serializable {
@Id
@GeneratedValue
private Long id;
private String data;
public Child() {
}
public Child(String data) {
this.data = data;
}
public Child(String data, Long id) {
this.id = id;
this.data = data;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Child)) return false;
Child that = (Child) 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;
}
@Override
public int hashCode() {
int result = id != null ? id.hashCode() : 0;
result = 31 * result + (data != null ? data.hashCode() : 0);
return result;
}
@Override
public String toString() {
return "Child(id = " + id + ", data = " + data + ")";
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
}

View File

@ -0,0 +1,88 @@
package ee.estonia.entities;
import org.hibernate.envers.Audited;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
/**
* @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
*/
@Entity
@Audited
public class Parent implements Serializable {
@Id
@GeneratedValue
private Long id;
private String data;
@OneToMany
private Set<Child> collection = new HashSet<Child>();
public Parent() {
}
public Parent(String data) {
this.data = data;
}
public Parent(String data, Long id) {
this.id = id;
this.data = data;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Parent)) return false;
Parent that = (Parent) 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;
}
@Override
public int hashCode() {
int result = id != null ? id.hashCode() : 0;
result = 31 * result + (data != null ? data.hashCode() : 0);
return result;
}
@Override
public String toString() {
return "Parent(id = " + id + ", data = " + data + ")";
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
public Set<Child> getCollection() {
return collection;
}
public void setCollection(Set<Child> collection) {
this.collection = collection;
}
}

View File

@ -0,0 +1,56 @@
package org.hibernate.envers.test.integration.naming;
import ee.estonia.entities.Child;
import ee.estonia.entities.Parent;
import org.hibernate.ejb.Ejb3Configuration;
import org.hibernate.envers.test.AbstractEntityTest;
import org.hibernate.envers.test.Priority;
import org.hibernate.envers.test.tools.TestTools;
import org.hibernate.testing.TestForIssue;
import org.junit.Assert;
import org.junit.Test;
import javax.persistence.EntityManager;
/**
* @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
*/
@TestForIssue(jiraKey = "HHH-6738")
public class EstonianTableAlias extends AbstractEntityTest {
private Long parentId = null;
private Long childId = null;
public void configure(Ejb3Configuration cfg) {
cfg.addAnnotatedClass(Parent.class);
cfg.addAnnotatedClass(Child.class);
}
@Test
@Priority(10)
public void initData() {
EntityManager em = getEntityManager();
// Revision 1
em.getTransaction().begin();
Parent parent = new Parent("parent");
Child child = new Child("child");
parent.getCollection().add(child);
em.persist(child);
em.persist(parent);
em.getTransaction().commit();
parentId = parent.getId();
childId = child.getId();
}
@Test
public void testAuditChildTableAlias() {
Parent parent = new Parent("parent", parentId);
Child child = new Child("child", childId);
Parent ver1 = getAuditReader().find(Parent.class, parentId, 1);
Assert.assertEquals(parent, ver1);
Assert.assertEquals(TestTools.makeSet(child), ver1.getCollection());
}
}