Buggy code:
criteria . add ( criterion ) ; return null ; } @ Override public StorageResults visit ( Max max ) { } @ Override public StorageResults visit ( Min min ) { } @ Override public StorageResults visit ( IsNull isNull ) { } @ Override public StorageResults visit ( IsEmpty isEmpty ) { } @ Override public StorageResults visit ( NotIsEmpty notIsEmpty ) { } @ Override public StorageResults visit ( NotIsNull notIsNull ) { } @ Override public StorageResults visit ( Range range ) { } private class CriterionAdapter extends VisitorAdapter < Criterion > { private final StandardQueryHandler . CriterionFieldCondition visitor = new StandardQueryHandler . CriterionFieldCondition ( ) ; private final RDBMSDataSource datasource ; private CriterionAdapter ( RDBMSDataSource datasource ) { } @ Override public Criterion visit ( Condition condition ) { } @ Override public Criterion visit ( UnaryLogicOperator condition ) { } @ Override public Criterion visit ( Isa isa ) { } @ Override public Criterion visit ( IsNull isNull ) { } @ Override public Criterion visit ( IsEmpty isEmpty ) { } @ Override public Criterion visit ( NotIsEmpty notIsEmpty ) { } @ Override public Criterion visit ( GroupSize groupSize ) { } @ Override public Criterion visit ( NotIsNull notIsNull ) { } @ Override public Criterion visit ( BinaryLogicOperator condition ) { } @ Override public Criterion visit ( Range range ) { } @ Override public Criterion visit ( Compare condition ) { FieldCondition leftFieldCondition = condition . getLeft ( ) . accept ( criterionFieldCondition ) ; FieldCondition rightFieldCondition = condition . getRight ( ) . accept ( criterionFieldCondition ) ; Predicate predicate = condition . getPredicate ( ) ; if ( ! ( leftFieldCondition . isProperty ) ) { if ( leftFieldCondition . isComputedProperty ) { String mainTableAlias = "this_" ; String mainTableName = resolver . get ( mainType ) ; Object value = condition . getRight ( ) . accept ( VALUE_ADAPTER ) ; String comparator ; if ( predicate == ( Predicate . EQUALS ) ) { comparator = "=" ; } else if ( predicate == ( Predicate . GREATER_THAN ) ) { comparator = ">" ; } else if ( predicate == ( Predicate . GREATER_THAN_OR_EQUALS ) ) { comparator = ">=" ; } else if ( predicate == ( Predicate . LOWER_THAN ) ) { comparator = "<" ; } else if ( predicate == ( Predicate . LOWER_THAN_OR_EQUALS ) ) { comparator = "<=" ; } else { throw new IllegalArgumentException ( ( ( "Predicate<seq2seq4repair_space>'" + predicate ) + "'<seq2seq4repair_space>is<seq2seq4repair_space>not<seq2seq4repair_space>supported<seq2seq4repair_space>on<seq2seq4repair_space>group_size<seq2seq4repair_space>value." ) ) ; } String sqlConditionBuilder = "(" ; sqlConditionBuilder += "select<seq2seq4repair_space>count(1)<seq2seq4repair_space>from" ; sqlConditionBuilder += ( '<seq2seq4repair_space>' + mainTableName ) + '<seq2seq4repair_space>' ; sqlConditionBuilder += ( ( ( ( "where<seq2seq4repair_space>" + ( Storage . METADATA_TASK_ID ) ) + "<seq2seq4repair_space>=<seq2seq4repair_space>" ) + mainTableAlias ) + "." ) + ( Storage . METADATA_TASK_ID ) ; sqlConditionBuilder += ')' ; sqlConditionBuilder += ( ( '<seq2seq4repair_space>' + comparator ) + '<seq2seq4repair_space>' ) + value ; return Restrictions . Restrictions . sqlRestriction ( sqlConditionBuilder ) ; } if ( StandardQueryHandler . LOGGER . isDebugEnabled ( ) ) { StandardQueryHandler . LOGGER . debug ( ( ( "Query<seq2seq4repair_space>on<seq2seq4repair_space>'" + leftFieldCondition ) + "'<seq2seq4repair_space>is<seq2seq4repair_space>not<seq2seq4repair_space>a<seq2seq4repair_space>user<seq2seq4repair_space>set<seq2seq4repair_space>property.<seq2seq4repair_space>Ignore<seq2seq4repair_space>this<seq2seq4repair_space>condition." ) ) ; } return TRUE_CRITERION ; } if ( ( condition . getLeft ( ) ) instanceof Field ) { Field leftField = ( ( Field ) ( condition . getLeft ( ) ) ) ; FieldMetadata fieldMetadata = leftField . getFieldMetadata ( ) ; Set < String > aliases = Collections . singleton ( fieldMetadata . getContainingType ( ) . getName ( ) ) ; <START_BUG> if ( ( ! ( mainType . equals ( fieldMetadata . getContainingType ( ) ) ) ) || ( fieldMetadata instanceof ReferenceFieldMetadata ) ) { <END_BUG> leftField . accept ( StandardQueryHandler . this ) ; aliases = getAliases ( mainType , leftField ) ; if ( ! ( fieldMetadata . isMany ( ) ) ) { leftFieldCondition . criterionFieldNames . clear ( ) ; for ( String alias : aliases ) { if ( fieldMetadata instanceof ReferenceFieldMetadata ) { if ( ! ( ( ( ( ReferenceFieldMetadata ) ( fieldMetadata ) ) . getReferencedField ( ) ) instanceof CompoundFieldMetadata ) ) { leftFieldCondition . criterionFieldNames . add ( ( ( alias + '.' ) + ( ( ( ReferenceFieldMetadata ) ( fieldMetadata ) ) . getReferencedField ( ) . getName ( ) ) ) ) ; } } else { leftFieldCondition . criterionFieldNames . add ( ( ( alias + '.' ) + ( fieldMetadata . getName ( ) ) ) ) ; } } } } if ( ( leftFieldCondition . isMany ) || ( rightFieldCondition . isMany ) ) { if ( ( criteria ) instanceof CriteriaImpl ) { Iterator iterator = ( ( CriteriaImpl ) ( criteria ) ) . iterateSubcriteria ( ) ; Criteria typeCheckCriteria = criteria ; while ( iterator . hasNext ( ) ) { Criteria subCriteria = ( ( Criteria ) ( iterator . next ( ) ) ) ; if ( aliases . contains ( subCriteria . getAlias ( ) ) ) { typeCheckCriteria = subCriteria ; break ; } } if ( ( leftFieldCondition . position ) >= 0 ) { return new ManyFieldCriterion ( datasource , typeCheckCriteria , resolver , fieldMetadata , condition . getRight ( ) . accept ( VALUE_ADAPTER ) , leftFieldCondition . position ) ; } else { return new ManyFieldCriterion ( datasource , typeCheckCriteria , resolver , fieldMetadata , condition . getRight ( ) . accept ( VALUE_ADAPTER ) ) ; } } else { throw new IllegalStateException ( ( ( "Expected<seq2seq4repair_space>a<seq2seq4repair_space>criteria<seq2seq4repair_space>instance<seq2seq4repair_space>of<seq2seq4repair_space>" + ( CriteriaImpl . class . getName ( ) ) ) + "." ) ) ; } } } if ( ! ( rightFieldCondition . isProperty ) ) { Object compareValue = condition . getRight ( ) . accept ( VALUE_ADAPTER ) ;

Fix:
if ( ( ( leftFieldCondition . position ) < 0 ) && ( ( ! ( mainType . equals ( fieldMetadata . getContainingType ( ) ) ) ) || ( fieldMetadata instanceof ReferenceFieldMetadata ) ) ) {