diff --git a/hibernate-core/src/main/java/org/hibernate/internal/FilterHelper.java b/hibernate-core/src/main/java/org/hibernate/internal/FilterHelper.java index 8ae733f233..f027f8a975 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/FilterHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/FilterHelper.java @@ -14,6 +14,8 @@ import org.hibernate.internal.util.StringHelper; import org.hibernate.internal.util.collections.CollectionHelper; import org.hibernate.sql.Template; +import static org.hibernate.internal.util.StringHelper.safeInterning; + /** * Implementation of FilterHelper. * @@ -44,23 +46,27 @@ public class FilterHelper { filterCount = 0; for ( final FilterConfiguration filter : filters ) { filterAutoAliasFlags[filterCount] = false; - filterNames[filterCount] = filter.getName(); - filterConditions[filterCount] = filter.getCondition(); + filterNames[filterCount] = safeInterning( filter.getName() ); + filterConditions[filterCount] = safeInterning( filter.getCondition() ); filterAliasTableMaps[filterCount] = filter.getAliasTableMap( factory ); if ( ( filterAliasTableMaps[filterCount].isEmpty() || isTableFromPersistentClass( filterAliasTableMaps[filterCount] ) ) && filter .useAutoAliasInjection() ) { - filterConditions[filterCount] = Template.renderWhereStringTemplate( - filter.getCondition(), - FilterImpl.MARKER, - factory.getDialect(), - factory.getSqlFunctionRegistry() + filterConditions[filterCount] = safeInterning( + Template.renderWhereStringTemplate( + filter.getCondition(), + FilterImpl.MARKER, + factory.getDialect(), + factory.getSqlFunctionRegistry() + ) ); filterAutoAliasFlags[filterCount] = true; } - filterConditions[filterCount] = StringHelper.replace( - filterConditions[filterCount], - ":", - ":" + filterNames[filterCount] + "." + filterConditions[filterCount] = safeInterning( + StringHelper.replace( + filterConditions[filterCount], + ":", + ":" + filterNames[filterCount] + "." + ) ); filterCount++; } diff --git a/hibernate-core/src/main/java/org/hibernate/internal/util/StringHelper.java b/hibernate-core/src/main/java/org/hibernate/internal/util/StringHelper.java index acd425617d..4533d5b696 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/util/StringHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/util/StringHelper.java @@ -886,4 +886,25 @@ public final class StringHelper { return null; } } + + /** + * Return the interned form of a String, or null if the parameter is null. + *

+ * Use with caution: excessive interning is known to cause issues. + * Best to use only with strings which are known to be long lived constants, + * and for which the chances of being actual duplicates is proven. + * (Even better: avoid needing interning by design changes such as reusing + * the known reference) + * @param string The string to intern. + * @return The interned string. + */ + public static String safeInterning(final String string) { + if ( string == null ) { + return null; + } + else { + return string.intern(); + } + } + }