HHH-8036 - Fix and test

This commit is contained in:
Lukasz Antoniak 2013-03-04 22:17:47 +01:00
parent 36c8a2e0fd
commit 18d2f136f8
26 changed files with 304 additions and 74 deletions

View File

@ -22,6 +22,8 @@
* Boston, MA 02110-1301 USA
*/
package org.hibernate.envers.query;
import org.hibernate.criterion.Restrictions;
import org.hibernate.envers.RevisionType;
import org.hibernate.envers.query.criteria.AuditConjunction;
import org.hibernate.envers.query.criteria.AuditCriterion;
@ -39,7 +41,7 @@ import org.hibernate.envers.query.property.RevisionTypePropertyName;
/**
* TODO: ilike
* @author Adam Warski (adam at warski dot org)
* @see org.hibernate.criterion.Restrictions
* @see Restrictions
*/
@SuppressWarnings({"JavaDoc"})
public class AuditEntity {

View File

@ -27,6 +27,7 @@ import java.util.List;
import org.hibernate.envers.configuration.AuditConfiguration;
import org.hibernate.envers.query.property.PropertyNameGetter;
import org.hibernate.envers.reader.AuditReaderImplementor;
import org.hibernate.envers.tools.query.Parameters;
import org.hibernate.envers.tools.query.QueryBuilder;
@ -54,8 +55,9 @@ public class AggregatedAuditExpression implements AuditCriterion, ExtendableCrit
return this;
}
public void addToQuery(AuditConfiguration auditCfg, String entityName, QueryBuilder qb, Parameters parameters) {
String propertyName = propertyNameGetter.get(auditCfg);
public void addToQuery(AuditConfiguration auditCfg, AuditReaderImplementor versionsReader, String entityName,
QueryBuilder qb, Parameters parameters) {
String propertyName = CriteriaTools.determinePropertyName( auditCfg, versionsReader, entityName, propertyNameGetter );
CriteriaTools.checkPropertyNotARelation(auditCfg, entityName, propertyName);
@ -67,8 +69,8 @@ public class AggregatedAuditExpression implements AuditCriterion, ExtendableCrit
// Adding all specified conditions both to the main query, as well as to the
// aggregated one.
for (AuditCriterion versionsCriteria : criterions) {
versionsCriteria.addToQuery(auditCfg, entityName, qb, subParams);
versionsCriteria.addToQuery(auditCfg, entityName, subQb, subQb.getRootParameters());
versionsCriteria.addToQuery(auditCfg, versionsReader, entityName, qb, subParams);
versionsCriteria.addToQuery(auditCfg, versionsReader, entityName, subQb, subQb.getRootParameters());
}
// Setting the desired projection of the aggregated query

View File

@ -26,6 +26,7 @@ import java.util.ArrayList;
import java.util.List;
import org.hibernate.envers.configuration.AuditConfiguration;
import org.hibernate.envers.reader.AuditReaderImplementor;
import org.hibernate.envers.tools.query.Parameters;
import org.hibernate.envers.tools.query.QueryBuilder;
@ -44,14 +45,15 @@ public class AuditConjunction implements AuditCriterion, ExtendableCriterion {
return this;
}
public void addToQuery(AuditConfiguration verCfg, String entityName, QueryBuilder qb, Parameters parameters) {
public void addToQuery(AuditConfiguration verCfg, AuditReaderImplementor versionsReader, String entityName,
QueryBuilder qb, Parameters parameters) {
Parameters andParameters = parameters.addSubParameters(Parameters.AND);
if (criterions.size() == 0) {
andParameters.addWhere("1", false, "=", "1", false);
} else {
for (AuditCriterion criterion : criterions) {
criterion.addToQuery(verCfg, entityName, qb, andParameters);
criterion.addToQuery(verCfg, versionsReader, entityName, qb, andParameters);
}
}
}

View File

@ -22,7 +22,9 @@
* Boston, MA 02110-1301 USA
*/
package org.hibernate.envers.query.criteria;
import org.hibernate.envers.configuration.AuditConfiguration;
import org.hibernate.envers.reader.AuditReaderImplementor;
import org.hibernate.envers.tools.query.Parameters;
import org.hibernate.envers.tools.query.QueryBuilder;
@ -30,5 +32,6 @@ import org.hibernate.envers.tools.query.QueryBuilder;
* @author Adam Warski (adam at warski dot org)
*/
public interface AuditCriterion {
void addToQuery(AuditConfiguration auditCfg, String entityName, QueryBuilder qb, Parameters parameters);
void addToQuery(AuditConfiguration auditCfg, AuditReaderImplementor versionsReader, String entityName,
QueryBuilder qb, Parameters parameters);
}

View File

@ -26,6 +26,7 @@ import java.util.ArrayList;
import java.util.List;
import org.hibernate.envers.configuration.AuditConfiguration;
import org.hibernate.envers.reader.AuditReaderImplementor;
import org.hibernate.envers.tools.query.Parameters;
import org.hibernate.envers.tools.query.QueryBuilder;
@ -44,14 +45,15 @@ public class AuditDisjunction implements AuditCriterion, ExtendableCriterion {
return this;
}
public void addToQuery(AuditConfiguration verCfg, String entityName, QueryBuilder qb, Parameters parameters) {
public void addToQuery(AuditConfiguration verCfg, AuditReaderImplementor versionsReader, String entityName,
QueryBuilder qb, Parameters parameters) {
Parameters orParameters = parameters.addSubParameters(Parameters.OR);
if (criterions.size() == 0) {
orParameters.addWhere("0", false, "=", "1", false);
} else {
for (AuditCriterion criterion : criterions) {
criterion.addToQuery(verCfg, entityName, qb, orParameters);
criterion.addToQuery(verCfg, versionsReader, entityName, qb, orParameters);
}
}
}

View File

@ -21,40 +21,63 @@
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.envers.query.criteria;
import org.hibernate.envers.query.projection.AuditProjection;
import org.hibernate.envers.query.projection.PropertyAuditProjection;
import org.hibernate.envers.query.property.EntityPropertyName;
import org.hibernate.envers.query.property.OriginalIdPropertyName;
import org.hibernate.envers.query.property.PropertyNameGetter;
/**
* Create restrictions and projections for the id of an audited entity.
*
* @author Adam Warski (adam at warski dot org)
* @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
*/
@SuppressWarnings({"JavaDoc"})
public class AuditId {
/**
@SuppressWarnings({ "JavaDoc" })
public class AuditId<T> extends AuditProperty<T> {
public static final String IDENTIFIER_PLACEHOLDER = "$$id$$";
private static final PropertyNameGetter identifierPropertyGetter = new EntityPropertyName( IDENTIFIER_PLACEHOLDER );
public AuditId() {
super( identifierPropertyGetter );
}
/**
* Apply an "equal" constraint
*/
public AuditCriterion eq(Object id) {
return new IdentifierEqAuditExpression(id, true);
return new IdentifierEqAuditExpression( id, true );
}
/**
/**
* Apply a "not equal" constraint
*/
public AuditCriterion ne(Object id) {
return new IdentifierEqAuditExpression(id, false);
return new IdentifierEqAuditExpression( id, false );
}
// Projections
// Projections
/**
* Projection counting the values
* TODO: idPropertyName isn't needed, should be read from the configuration
* @param idPropertyName Name of the identifier property
*/
public AuditProjection count(String idPropertyName) {
return new PropertyAuditProjection(new OriginalIdPropertyName(idPropertyName), "count", false);
}
/**
* Projection counting the values
*
* @param idPropertyName Name of the identifier property
*
* @deprecated Use {@link #count()}.
*/
public AuditProjection count(String idPropertyName) {
return new PropertyAuditProjection( new OriginalIdPropertyName( idPropertyName ), "count", false );
}
@Override
public AuditCriterion hasChanged() {
throw new UnsupportedOperationException();
}
@Override
public AuditCriterion hasNotChanged() {
throw new UnsupportedOperationException();
}
}

View File

@ -24,6 +24,7 @@
package org.hibernate.envers.query.criteria;
import org.hibernate.envers.configuration.AuditConfiguration;
import org.hibernate.envers.query.property.PropertyNameGetter;
import org.hibernate.envers.reader.AuditReaderImplementor;
import org.hibernate.envers.tools.query.Parameters;
import org.hibernate.envers.tools.query.QueryBuilder;
@ -41,8 +42,9 @@ public class BetweenAuditExpression implements AuditCriterion {
this.hi = hi;
}
public void addToQuery(AuditConfiguration auditCfg, String entityName, QueryBuilder qb, Parameters parameters) {
String propertyName = propertyNameGetter.get(auditCfg);
public void addToQuery(AuditConfiguration auditCfg, AuditReaderImplementor versionsReader, String entityName,
QueryBuilder qb, Parameters parameters) {
String propertyName = CriteriaTools.determinePropertyName( auditCfg, versionsReader, entityName, propertyNameGetter );
CriteriaTools.checkPropertyNotARelation(auditCfg, entityName, propertyName);
Parameters subParams = parameters.addSubParameters(Parameters.AND);

View File

@ -22,10 +22,13 @@
* Boston, MA 02110-1301 USA
*/
package org.hibernate.envers.query.criteria;
import org.hibernate.envers.configuration.AuditConfiguration;
import org.hibernate.envers.entities.RelationDescription;
import org.hibernate.envers.entities.RelationType;
import org.hibernate.envers.exception.AuditException;
import org.hibernate.envers.query.property.PropertyNameGetter;
import org.hibernate.envers.reader.AuditReaderImplementor;
/**
* @author Adam Warski (adam at warski dot org)
@ -56,4 +59,28 @@ public class CriteriaTools {
throw new AuditException("This type of relation (" + entityName + "." + propertyName +
") isn't supported and can't be used in queries.");
}
/**
* @see #determinePropertyName(AuditConfiguration, AuditReaderImplementor, String, String)
*/
public static String determinePropertyName(AuditConfiguration auditCfg, AuditReaderImplementor versionsReader,
String entityName, PropertyNameGetter propertyNameGetter) {
return determinePropertyName( auditCfg, versionsReader, entityName, propertyNameGetter.get( auditCfg ) );
}
/**
* @param auditCfg Audit configuration.
* @param versionsReader Versions reader.
* @param entityName Original entity name (not audited).
* @param propertyName Property name or placeholder.
* @return Path to property. Handles identifier placeholder used by {@link AuditId}.
*/
public static String determinePropertyName(AuditConfiguration auditCfg, AuditReaderImplementor versionsReader,
String entityName, String propertyName) {
if ( AuditId.IDENTIFIER_PLACEHOLDER.equals( propertyName ) ) {
final String identifierPropertyName = versionsReader.getSessionImplementor().getFactory().getEntityPersister( entityName ).getIdentifierPropertyName();
propertyName = auditCfg.getAuditEntCfg().getOriginalIdPropName() + "." + identifierPropertyName;
}
return propertyName;
}
}

View File

@ -23,6 +23,7 @@
*/
package org.hibernate.envers.query.criteria;
import org.hibernate.envers.configuration.AuditConfiguration;
import org.hibernate.envers.reader.AuditReaderImplementor;
import org.hibernate.envers.tools.query.Parameters;
import org.hibernate.envers.tools.query.QueryBuilder;
@ -39,7 +40,8 @@ public class IdentifierEqAuditExpression implements AuditCriterion {
this.equals = equals;
}
public void addToQuery(AuditConfiguration verCfg, String entityName, QueryBuilder qb, Parameters parameters) {
public void addToQuery(AuditConfiguration verCfg, AuditReaderImplementor versionsReader, String entityName,
QueryBuilder qb, Parameters parameters) {
verCfg.getEntCfg().get(entityName).getIdMapper()
.addIdEqualsToQuery(parameters, id, verCfg.getAuditEntCfg().getOriginalIdPropName(), equals);
}

View File

@ -22,8 +22,10 @@
* Boston, MA 02110-1301 USA
*/
package org.hibernate.envers.query.criteria;
import org.hibernate.envers.configuration.AuditConfiguration;
import org.hibernate.envers.query.property.PropertyNameGetter;
import org.hibernate.envers.reader.AuditReaderImplementor;
import org.hibernate.envers.tools.query.Parameters;
import org.hibernate.envers.tools.query.QueryBuilder;
@ -39,8 +41,9 @@ public class InAuditExpression implements AuditCriterion {
this.values = values;
}
public void addToQuery(AuditConfiguration auditCfg, String entityName, QueryBuilder qb, Parameters parameters) {
String propertyName = propertyNameGetter.get(auditCfg);
public void addToQuery(AuditConfiguration auditCfg, AuditReaderImplementor versionsReader, String entityName,
QueryBuilder qb, Parameters parameters) {
String propertyName = CriteriaTools.determinePropertyName( auditCfg, versionsReader, entityName, propertyNameGetter );
CriteriaTools.checkPropertyNotARelation(auditCfg, entityName, propertyName);
parameters.addWhereWithParams(propertyName, "in (", values, ")");
}

View File

@ -23,6 +23,7 @@
*/
package org.hibernate.envers.query.criteria;
import org.hibernate.envers.configuration.AuditConfiguration;
import org.hibernate.envers.reader.AuditReaderImplementor;
import org.hibernate.envers.tools.query.Parameters;
import org.hibernate.envers.tools.query.QueryBuilder;
@ -40,10 +41,11 @@ public class LogicalAuditExpression implements AuditCriterion {
this.op = op;
}
public void addToQuery(AuditConfiguration verCfg, String entityName, QueryBuilder qb, Parameters parameters) {
public void addToQuery(AuditConfiguration verCfg, AuditReaderImplementor versionsReader, 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"));
lhs.addToQuery(verCfg, versionsReader, entityName, qb, opParameters.addSubParameters("and"));
rhs.addToQuery(verCfg, versionsReader, entityName, qb, opParameters.addSubParameters("and"));
}
}

View File

@ -22,7 +22,9 @@
* Boston, MA 02110-1301 USA
*/
package org.hibernate.envers.query.criteria;
import org.hibernate.envers.configuration.AuditConfiguration;
import org.hibernate.envers.reader.AuditReaderImplementor;
import org.hibernate.envers.tools.query.Parameters;
import org.hibernate.envers.tools.query.QueryBuilder;
@ -36,7 +38,8 @@ public class NotAuditExpression implements AuditCriterion {
this.criterion = criterion;
}
public void addToQuery(AuditConfiguration verCfg, String entityName, QueryBuilder qb, Parameters parameters) {
criterion.addToQuery(verCfg, entityName, qb, parameters.addNegatedParameters());
public void addToQuery(AuditConfiguration verCfg, AuditReaderImplementor versionsReader, String entityName,
QueryBuilder qb, Parameters parameters) {
criterion.addToQuery(verCfg, versionsReader, entityName, qb, parameters.addNegatedParameters());
}
}

View File

@ -22,9 +22,11 @@
* Boston, MA 02110-1301 USA
*/
package org.hibernate.envers.query.criteria;
import org.hibernate.envers.configuration.AuditConfiguration;
import org.hibernate.envers.entities.RelationDescription;
import org.hibernate.envers.query.property.PropertyNameGetter;
import org.hibernate.envers.reader.AuditReaderImplementor;
import org.hibernate.envers.tools.query.Parameters;
import org.hibernate.envers.tools.query.QueryBuilder;
@ -38,8 +40,9 @@ public class NotNullAuditExpression implements AuditCriterion {
this.propertyNameGetter = propertyNameGetter;
}
public void addToQuery(AuditConfiguration auditCfg, String entityName, QueryBuilder qb, Parameters parameters) {
String propertyName = propertyNameGetter.get(auditCfg);
public void addToQuery(AuditConfiguration auditCfg, AuditReaderImplementor versionsReader, String entityName,
QueryBuilder qb, Parameters parameters) {
String propertyName = CriteriaTools.determinePropertyName( auditCfg, versionsReader, entityName, propertyNameGetter );
RelationDescription relatedEntity = CriteriaTools.getRelatedEntity(auditCfg, entityName, propertyName);
if (relatedEntity == null) {

View File

@ -22,9 +22,11 @@
* Boston, MA 02110-1301 USA
*/
package org.hibernate.envers.query.criteria;
import org.hibernate.envers.configuration.AuditConfiguration;
import org.hibernate.envers.entities.RelationDescription;
import org.hibernate.envers.query.property.PropertyNameGetter;
import org.hibernate.envers.reader.AuditReaderImplementor;
import org.hibernate.envers.tools.query.Parameters;
import org.hibernate.envers.tools.query.QueryBuilder;
@ -38,8 +40,9 @@ public class NullAuditExpression implements AuditCriterion {
this.propertyNameGetter = propertyNameGetter;
}
public void addToQuery(AuditConfiguration auditCfg, String entityName, QueryBuilder qb, Parameters parameters) {
String propertyName = propertyNameGetter.get(auditCfg);
public void addToQuery(AuditConfiguration auditCfg, AuditReaderImplementor versionsReader, String entityName,
QueryBuilder qb, Parameters parameters) {
String propertyName = CriteriaTools.determinePropertyName( auditCfg, versionsReader, entityName, propertyNameGetter );
RelationDescription relatedEntity = CriteriaTools.getRelatedEntity(auditCfg, entityName, propertyName);
if (relatedEntity == null) {

View File

@ -22,8 +22,10 @@
* Boston, MA 02110-1301 USA
*/
package org.hibernate.envers.query.criteria;
import org.hibernate.envers.configuration.AuditConfiguration;
import org.hibernate.envers.query.property.PropertyNameGetter;
import org.hibernate.envers.reader.AuditReaderImplementor;
import org.hibernate.envers.tools.query.Parameters;
import org.hibernate.envers.tools.query.QueryBuilder;
@ -41,8 +43,9 @@ public class PropertyAuditExpression implements AuditCriterion {
this.op = op;
}
public void addToQuery(AuditConfiguration auditCfg, String entityName, QueryBuilder qb, Parameters parameters) {
String propertyName = propertyNameGetter.get(auditCfg);
public void addToQuery(AuditConfiguration auditCfg, AuditReaderImplementor versionsReader, String entityName,
QueryBuilder qb, Parameters parameters) {
String propertyName = CriteriaTools.determinePropertyName( auditCfg, versionsReader, entityName, propertyNameGetter );
CriteriaTools.checkPropertyNotARelation(auditCfg, entityName, propertyName);
CriteriaTools.checkPropertyNotARelation(auditCfg, entityName, otherPropertyName);
parameters.addWhere(propertyName, op, otherPropertyName);

View File

@ -22,10 +22,12 @@
* Boston, MA 02110-1301 USA
*/
package org.hibernate.envers.query.criteria;
import org.hibernate.envers.configuration.AuditConfiguration;
import org.hibernate.envers.entities.RelationDescription;
import org.hibernate.envers.exception.AuditException;
import org.hibernate.envers.query.property.PropertyNameGetter;
import org.hibernate.envers.reader.AuditReaderImplementor;
import org.hibernate.envers.tools.query.Parameters;
import org.hibernate.envers.tools.query.QueryBuilder;
@ -43,8 +45,9 @@ public class RelatedAuditExpression implements AuditCriterion {
this.equals = equals;
}
public void addToQuery(AuditConfiguration auditCfg, String entityName, QueryBuilder qb, Parameters parameters) {
String propertyName = propertyNameGetter.get(auditCfg);
public void addToQuery(AuditConfiguration auditCfg, AuditReaderImplementor versionsReader, String entityName,
QueryBuilder qb, Parameters parameters) {
String propertyName = CriteriaTools.determinePropertyName( auditCfg, versionsReader, entityName, propertyNameGetter );
RelationDescription relatedEntity = CriteriaTools.getRelatedEntity(auditCfg, entityName, propertyName);

View File

@ -21,9 +21,10 @@
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.envers.query.criteria;
import org.hibernate.envers.configuration.AuditConfiguration;
import org.hibernate.envers.reader.AuditReaderImplementor;
import org.hibernate.envers.tools.query.Parameters;
import org.hibernate.envers.tools.query.QueryBuilder;
@ -39,7 +40,8 @@ public class RevisionTypeAuditExpression implements AuditCriterion {
this.op = op;
}
public void addToQuery(AuditConfiguration verCfg, String entityName, QueryBuilder qb, Parameters parameters) {
public void addToQuery(AuditConfiguration verCfg, AuditReaderImplementor versionsReader, String entityName,
QueryBuilder qb, Parameters parameters) {
parameters.addWhereWithParam(verCfg.getAuditEntCfg().getRevisionTypePropName(), op, value);
}
}

View File

@ -22,10 +22,12 @@
* Boston, MA 02110-1301 USA
*/
package org.hibernate.envers.query.criteria;
import org.hibernate.envers.configuration.AuditConfiguration;
import org.hibernate.envers.entities.RelationDescription;
import org.hibernate.envers.exception.AuditException;
import org.hibernate.envers.query.property.PropertyNameGetter;
import org.hibernate.envers.reader.AuditReaderImplementor;
import org.hibernate.envers.tools.query.Parameters;
import org.hibernate.envers.tools.query.QueryBuilder;
@ -43,8 +45,9 @@ public class SimpleAuditExpression implements AuditCriterion {
this.op = op;
}
public void addToQuery(AuditConfiguration auditCfg, String entityName, QueryBuilder qb, Parameters parameters) {
String propertyName = propertyNameGetter.get(auditCfg);
public void addToQuery(AuditConfiguration auditCfg, AuditReaderImplementor versionsReader, String entityName,
QueryBuilder qb, Parameters parameters) {
String propertyName = CriteriaTools.determinePropertyName( auditCfg, versionsReader, entityName, propertyNameGetter );
RelationDescription relatedEntity = CriteriaTools.getRelatedEntity(auditCfg, entityName, propertyName);

View File

@ -38,6 +38,7 @@ import org.hibernate.envers.entities.EntityInstantiator;
import org.hibernate.envers.exception.AuditException;
import org.hibernate.envers.query.AuditQuery;
import org.hibernate.envers.query.criteria.AuditCriterion;
import org.hibernate.envers.query.criteria.CriteriaTools;
import org.hibernate.envers.query.order.AuditOrder;
import org.hibernate.envers.query.projection.AuditProjection;
import org.hibernate.envers.reader.AuditReaderImplementor;
@ -64,7 +65,7 @@ public abstract class AbstractAuditQuery implements AuditQuery {
protected boolean hasOrder;
protected final AuditConfiguration verCfg;
private final AuditReaderImplementor versionsReader;
protected final AuditReaderImplementor versionsReader;
protected AbstractAuditQuery(AuditConfiguration verCfg, AuditReaderImplementor versionsReader,
Class<?> cls) {
@ -129,15 +130,16 @@ public abstract class AbstractAuditQuery implements AuditQuery {
public AuditQuery addProjection(AuditProjection projection) {
Triple<String, String, Boolean> projectionData = projection.getData(verCfg);
hasProjection = true;
qb.addProjection(projectionData.getFirst(), projectionData.getSecond(), projectionData.getThird());
String propertyName = CriteriaTools.determinePropertyName( verCfg, versionsReader, entityName, projectionData.getSecond() );
qb.addProjection(projectionData.getFirst(), propertyName, projectionData.getThird());
return this;
}
public AuditQuery addOrder(AuditOrder order) {
hasOrder = true;
Pair<String, Boolean> orderData = order.getData(verCfg);
qb.addOrder(orderData.getFirst(), orderData.getSecond());
String propertyName = CriteriaTools.determinePropertyName( verCfg, versionsReader, entityName, orderData.getFirst() );
qb.addOrder(propertyName, orderData.getSecond());
return this;
}

View File

@ -96,7 +96,7 @@ public class EntitiesAtRevisionQuery extends AbstractAuditQuery {
// all specified conditions
for (AuditCriterion criterion : criterions) {
criterion.addToQuery(verCfg, entityName, qb, qb.getRootParameters());
criterion.addToQuery(verCfg, versionsReader, entityName, qb, qb.getRootParameters());
}
Query query = buildQuery();

View File

@ -45,7 +45,7 @@ public class EntitiesModifiedAtRevisionQuery extends AbstractAuditQuery {
// all specified conditions
for (AuditCriterion criterion : criterions) {
criterion.addToQuery(verCfg, entityName, qb, qb.getRootParameters());
criterion.addToQuery(verCfg, versionsReader, entityName, qb, qb.getRootParameters());
}
Query query = buildQuery();

View File

@ -96,7 +96,7 @@ public class RevisionsOfEntityQuery extends AbstractAuditQuery {
// all specified conditions, transformed
for (AuditCriterion criterion : criterions) {
criterion.addToQuery(verCfg, entityName, qb, qb.getRootParameters());
criterion.addToQuery(verCfg, versionsReader, entityName, qb, qb.getRootParameters());
}
if (!hasProjection && !hasOrder) {

View File

@ -21,13 +21,16 @@
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.envers.query.property;
import org.hibernate.envers.configuration.AuditConfiguration;
import org.hibernate.envers.query.criteria.AuditId;
/**
* Used for specifying restrictions on the identifier.
* TODO: idPropertyName should be read basing on auditCfg + entityName
*
* @deprecated To be removed together with {@link AuditId#count(String)} in version 5.0.
*
* @author Adam Warski (adam at warski dot org)
*/
public class OriginalIdPropertyName implements PropertyNameGetter {

View File

@ -0,0 +1,68 @@
package org.hibernate.envers.test.entities.ids;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.Id;
import org.hibernate.envers.Audited;
/**
* @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
*/
@Entity
@Audited
public class UnusualIdNamingEntity implements Serializable {
@Id
private String uniqueField;
private String variousData;
public UnusualIdNamingEntity() {
}
public UnusualIdNamingEntity(String uniqueField, String variousData) {
this.uniqueField = uniqueField;
this.variousData = variousData;
}
@Override
public boolean equals(Object o) {
if ( this == o ) return true;
if ( !( o instanceof UnusualIdNamingEntity) ) return false;
UnusualIdNamingEntity that = (UnusualIdNamingEntity) o;
if ( uniqueField != null ? !uniqueField.equals( that.uniqueField ) : that.uniqueField != null ) return false;
if ( variousData != null ? !variousData.equals( that.variousData ) : that.variousData != null ) return false;
return true;
}
@Override
public int hashCode() {
int result = uniqueField != null ? uniqueField.hashCode() : 0;
result = 31 * result + ( variousData != null ? variousData.hashCode() : 0 );
return result;
}
@Override
public String toString() {
return "UnusualIdNamingEntity(uniqueField = " + uniqueField + ", variousData = " + variousData + ")";
}
public String getUniqueField() {
return uniqueField;
}
public void setUniqueField(String uniqueField) {
this.uniqueField = uniqueField;
}
public String getVariousData() {
return variousData;
}
public void setVariousData(String variousData) {
this.variousData = variousData;
}
}

View File

@ -23,8 +23,11 @@
*/
package org.hibernate.envers.test.integration.query;
import java.util.Arrays;
import java.util.List;
import javax.persistence.EntityManager;
import org.junit.Assert;
import org.junit.Test;
import org.hibernate.ejb.Ejb3Configuration;
@ -32,53 +35,52 @@ import org.hibernate.envers.query.AuditEntity;
import org.hibernate.envers.test.BaseEnversJPAFunctionalTestCase;
import org.hibernate.envers.test.Priority;
import org.hibernate.envers.test.entities.IntTestEntity;
import org.hibernate.envers.test.entities.ids.UnusualIdNamingEntity;
import org.hibernate.envers.test.tools.TestTools;
import org.hibernate.testing.TestForIssue;
/**
* @author Adam Warski (adam at warski dot org)
*/
@SuppressWarnings({"unchecked"})
public class AggregateQuery extends BaseEnversJPAFunctionalTestCase {
public void configure(Ejb3Configuration cfg) {
cfg.addAnnotatedClass(IntTestEntity.class);
}
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class[] { IntTestEntity.class, UnusualIdNamingEntity.class };
}
@Test
@Test
@Priority(10)
public void initData() {
// Revision 1
EntityManager em = getEntityManager();
em.getTransaction().begin();
// Revision 1
em.getTransaction().begin();
IntTestEntity ite1 = new IntTestEntity(2);
IntTestEntity ite2 = new IntTestEntity(10);
em.persist(ite1);
em.persist(ite2);
Integer id1 = ite1.getId();
Integer id2 = ite2.getId();
em.getTransaction().commit();
// Revision 2
em.getTransaction().begin();
IntTestEntity ite3 = new IntTestEntity(8);
UnusualIdNamingEntity uine1 = new UnusualIdNamingEntity( "id1", "data1" );
em.persist( uine1 );
em.persist(ite3);
ite1 = em.find(IntTestEntity.class, id1);
ite1.setNumber(0);
em.getTransaction().commit();
// Revision 3
em.getTransaction().begin();
ite2 = em.find(IntTestEntity.class, id2);
ite2.setNumber(52);
em.getTransaction().commit();
em.close();
}
@Test
@ -110,4 +112,69 @@ public class AggregateQuery extends BaseEnversJPAFunctionalTestCase {
assert (Integer) ver3[0] == 52;
assert (Double) ver3[1] == 20.0;
}
@Test
@TestForIssue( jiraKey = "HHH-8036" )
public void testEntityIdProjection() {
Integer maxId = (Integer) getAuditReader().createQuery().forRevisionsOfEntity( IntTestEntity.class, true, true )
.addProjection( AuditEntity.id().max() )
.add( AuditEntity.revisionNumber().gt( 2 ) )
.getSingleResult();
Assert.assertEquals( Integer.valueOf( 2 ), maxId );
}
@Test
@TestForIssue( jiraKey = "HHH-8036" )
public void testEntityIdRestriction() {
List<IntTestEntity> list = getAuditReader().createQuery().forRevisionsOfEntity( IntTestEntity.class, true, true )
.add( AuditEntity.id().between( 2, 3 ) )
.getResultList();
Assert.assertTrue(
TestTools.checkList( list,
new IntTestEntity( 10, 2 ), new IntTestEntity( 8, 3 ), new IntTestEntity( 52, 2 )
)
);
}
@Test
@TestForIssue( jiraKey = "HHH-8036" )
public void testEntityIdOrdering() {
List<IntTestEntity> list = getAuditReader().createQuery().forRevisionsOfEntity( IntTestEntity.class, true, true )
.add( AuditEntity.revisionNumber().lt( 2 ) )
.addOrder( AuditEntity.id().desc() )
.getResultList();
Assert.assertEquals( Arrays.asList( new IntTestEntity( 10, 2 ), new IntTestEntity( 2, 1 ) ), list );
}
@Test
@TestForIssue( jiraKey = "HHH-8036" )
public void testUnusualIdFieldName() {
UnusualIdNamingEntity entity = (UnusualIdNamingEntity) getAuditReader().createQuery()
.forRevisionsOfEntity( UnusualIdNamingEntity.class, true, true )
.add( AuditEntity.id().like( "id1" ) )
.getSingleResult();
Assert.assertEquals( new UnusualIdNamingEntity( "id1", "data1" ), entity );
}
@Test
@TestForIssue( jiraKey = "HHH-8036" )
public void testEntityIdModifiedFlagNotSupported() {
try {
getAuditReader().createQuery().forRevisionsOfEntity( IntTestEntity.class, true, true )
.add( AuditEntity.id().hasChanged() )
.getResultList();
}
catch ( UnsupportedOperationException e1 ) {
try {
getAuditReader().createQuery().forRevisionsOfEntity( IntTestEntity.class, true, true )
.add( AuditEntity.id().hasNotChanged() )
.getResultList();
}
catch ( UnsupportedOperationException e2 ) {
return;
}
Assert.fail();
}
Assert.fail();
}
}

View File

@ -82,7 +82,7 @@ public class DeletedEntities extends BaseEnversJPAFunctionalTestCase {
assert (Long) getAuditReader().createQuery().forEntitiesAtRevision(StrIntTestEntity.class, 1)
.addProjection(AuditEntity.id().count("id")).getResultList().get(0) == 2;
assert (Long) getAuditReader().createQuery().forEntitiesAtRevision(StrIntTestEntity.class, 2)
.addProjection(AuditEntity.id().count("id")).getResultList().get(0) == 1;
.addProjection(AuditEntity.id().count()).getResultList().get(0) == 1;
}
@Test