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 fa76fe9f59..f54c8e533f 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 @@ -212,6 +212,29 @@ public final class StringHelper { return buf.toString(); } + /** + * Used to find the ordinal parameters (e.g. '?1') in a string. + */ + public static int indexOfIdentifierWord(String str, String word) { + if ( str == null || str.length() == 0 || word == null ) { + return -1; + } + + int position = 0; + while ( position < str.length() ) { + position = str.indexOf( word, position ); + if ( + ( position == 0 || !Character.isJavaIdentifierPart( str.charAt( position - 1 ) ) ) && + ( position + word.length() == str.length() || !Character.isJavaIdentifierPart( str.charAt( position + word.length() ) ) ) + ) { + return position; + } + position = position + 1; + } + + return -1; + } + public static char getLastNonWhitespaceCharacter(String str) { if ( str != null && str.length() > 0 ) { for ( int i = str.length() - 1; i >= 0; i-- ) { diff --git a/hibernate-core/src/main/java/org/hibernate/query/internal/QueryParameterBindingsImpl.java b/hibernate-core/src/main/java/org/hibernate/query/internal/QueryParameterBindingsImpl.java index 207a0acf13..55b4d89d51 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/internal/QueryParameterBindingsImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/query/internal/QueryParameterBindingsImpl.java @@ -532,7 +532,7 @@ public class QueryParameterBindingsImpl implements QueryParameterBindings { sourceToken = "?" + OrdinalParameterDescriptor.class.cast( sourceParam ).getPosition(); } - final int loc = queryString.indexOf( sourceToken ); + final int loc = StringHelper.indexOfIdentifierWord( queryString, sourceToken ); if ( loc < 0 ) { continue; diff --git a/hibernate-core/src/test/java/org/hibernate/test/util/StringHelperTest.java b/hibernate-core/src/test/java/org/hibernate/test/util/StringHelperTest.java index 7111e85f4f..bec3ebef07 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/util/StringHelperTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/util/StringHelperTest.java @@ -42,4 +42,16 @@ public class StringHelperTest extends BaseUnitTestCase { assertEquals( STRING_HELPER_NAME, StringHelper.collapseQualifierBase( STRING_HELPER_NAME, BASE_PACKAGE ) ); assertEquals( "o.h.internal.util.StringHelper", StringHelper.collapseQualifierBase( STRING_HELPER_FQN, BASE_PACKAGE ) ); } + + @Test + public void testFindIdentifierWord() { + assertEquals( StringHelper.indexOfIdentifierWord( "", "word" ), -1 ); + assertEquals( StringHelper.indexOfIdentifierWord( null, "word" ), -1 ); + assertEquals( StringHelper.indexOfIdentifierWord( "sentence", null ), -1 ); + assertEquals( StringHelper.indexOfIdentifierWord( "where name=?13 and description=?1", "?1" ), 31 ); + assertEquals( StringHelper.indexOfIdentifierWord( "where name=?13 and description=?1 and category_id=?4", "?1" ), 31 ); + assertEquals( StringHelper.indexOfIdentifierWord( "?1", "?1" ), 0 ); + assertEquals( StringHelper.indexOfIdentifierWord( "no identifier here", "?1" ), -1 ); + assertEquals( StringHelper.indexOfIdentifierWord( "some text ?", "?" ), 10 ); + } }