Hello,
I use Hibernate 3.2.4.sp1. My database model extensively exploits effective date pattern to filter active/inactive records. As described in hibernate documentation the hibernate filters are to be used in such occasions. It works fine with ManyToOne relationships, but only when fetch type is set to EAGER.
If the LAZY fetch strategy is used the filtering does not work and causes the ‘More than one row with the given identifier was found’ exception. Is there any way to avoid this behavior without turning on the EAGER strategy (which is not suitable in my situation)? I could not find any answer on this and other forums.
Few simple adjustment in EntityJoinWalker and QueryParameters solved the problem, but I’m not entirely sure if those changes will not affect the rest of hibernate functionality. Below is the list of changes:
// EntityJoinWalker.java (rev.7652), line 40
Code:
StringBuffer whereCondition = whereString( getAlias(), uniqueKey, batchSize )
.append( persister.filterFragment( getAlias(), enabledFilters ) );
// QueryParameters (rev. 9636), lines 396-427
Code:
List queryParamsPositions = new ArrayList();
while ( tokens.hasMoreTokens() ) {
final String token = tokens.nextToken();
if ( token.startsWith( ParserHelper.HQL_VARIABLE_PREFIX ) ) {
String filterParameterName = token.substring( 1 );
Object value = session.getFilterParameterValue( filterParameterName );
Type type = session.getFilterParameterType( filterParameterName );
if ( value != null && Collection.class.isAssignableFrom( value.getClass() ) ) {
Iterator itr = ( ( Collection ) value ).iterator();
while ( itr.hasNext() ) {
Object elementValue = itr.next();
result.append( '?' );
parameters.add( elementValue );
parameterTypes.add( type );
if ( itr.hasNext() ) {
result.append( ", " );
}
}
}
else {
result.append( '?' );
parameters.add( value );
parameterTypes.add( type );
}
}
else {
if ("?".equals(token)){
queryParamsPositions.add(parameters.size());
}
result.append( token );
}
}
for (int i = 0 ; i < queryParamsPositions.size() ; i++) {
int currIndex = (Integer) queryParamsPositions.get(i);
parameters.add(currIndex,getPositionalParameterValues()[i]);
parameterTypes.add(currIndex, getPositionalParameterTypes()[i]);
}
if (queryParamsPositions.size() == 0) {
parameters.addAll(Arrays.asList( getPositionalParameterValues() ) );
parameterTypes.addAll(Arrays.asList( getPositionalParameterTypes() ) );
}
[/code]