From a506f53a5caf416b4c7d058a2317155ea02313f6 Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Tue, 10 Jan 2017 18:20:03 +0000 Subject: [PATCH] HHH-11376 - Malformed SQL query sent to SQL Server with left outer join and pessimistic lock --- .../src/main/java/org/hibernate/LockOptions.java | 3 ++- .../hibernate/dialect/AbstractTransactSQLDialect.java | 11 +++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/LockOptions.java b/hibernate-core/src/main/java/org/hibernate/LockOptions.java index 3024ec24e6..9882172004 100644 --- a/hibernate-core/src/main/java/org/hibernate/LockOptions.java +++ b/hibernate-core/src/main/java/org/hibernate/LockOptions.java @@ -10,6 +10,7 @@ import java.io.Serializable; import java.util.Collections; import java.util.HashMap; import java.util.Iterator; +import java.util.LinkedHashMap; import java.util.Map; /** @@ -113,7 +114,7 @@ public class LockOptions implements Serializable { */ public LockOptions setAliasSpecificLockMode(String alias, LockMode lockMode) { if ( aliasSpecificLockModes == null ) { - aliasSpecificLockModes = new HashMap(); + aliasSpecificLockModes = new LinkedHashMap<>(); } aliasSpecificLockModes.put( alias, lockMode ); return this; diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/AbstractTransactSQLDialect.java b/hibernate-core/src/main/java/org/hibernate/dialect/AbstractTransactSQLDialect.java index 20f10a4d1b..28be869864 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/AbstractTransactSQLDialect.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/AbstractTransactSQLDialect.java @@ -146,7 +146,7 @@ abstract class AbstractTransactSQLDialect extends Dialect { // TODO: merge additional lockoptions support in Dialect.applyLocksToSql final Iterator itr = aliasedLockOptions.getAliasLockIterator(); final StringBuilder buffer = new StringBuilder( sql ); - int correction = 0; + while ( itr.hasNext() ) { final Map.Entry entry = (Map.Entry) itr.next(); final LockMode lockMode = (LockMode) entry.getValue(); @@ -155,16 +155,16 @@ abstract class AbstractTransactSQLDialect extends Dialect { int start = -1; int end = -1; if ( sql.endsWith( " " + alias ) ) { - start = ( sql.length() - alias.length() ) + correction; + start = ( buffer.length() - alias.length() ); end = start + alias.length(); } else { - int position = sql.indexOf( " " + alias + " " ); + int position = buffer.indexOf( " " + alias + " " ); if ( position <= -1 ) { - position = sql.indexOf( " " + alias + "," ); + position = buffer.indexOf( " " + alias + "," ); } if ( position > -1 ) { - start = position + correction + 1; + start = position + 1; end = start + alias.length(); } } @@ -172,7 +172,6 @@ abstract class AbstractTransactSQLDialect extends Dialect { if ( start > -1 ) { final String lockHint = appendLockHint( aliasedLockOptions, alias ); buffer.replace( start, end, lockHint ); - correction += ( lockHint.length() - alias.length() ); } } }