DATAJPA-1506 - Fix alias detection for native query.

When no alias is specified in a native query that does not contain a WHERE clause right after the FROM clause, this is now correctly detected.

Original pull request: #379.
This commit is contained in:
Florian Lüdiger
2019-05-09 17:16:14 +02:00
committed by Oliver Drotbohm
parent d523e8a9b4
commit e4a6bed366
2 changed files with 8 additions and 1 deletions

View File

@@ -76,6 +76,7 @@ import org.springframework.util.StringUtils;
* @author Jens Schauder
* @author Nils Borrmann
* @author Reda.Housni-Alaoui
* @author Florian Lüdiger
*/
public abstract class QueryUtils {
@@ -133,7 +134,7 @@ public abstract class QueryUtils {
builder.append(IDENTIFIER_GROUP); // Entity name, can be qualified (any
builder.append("(?:\\sas)*"); // exclude possible "as" keyword
builder.append("(?:\\s)+"); // at least one space separating
builder.append("(?!(?:where))(\\w+)"); // the actual alias
builder.append("(?!(?:where|group by|order by))(\\w+)"); // the actual alias
ALIAS_MATCH = compile(builder.toString(), CASE_INSENSITIVE);

View File

@@ -38,6 +38,7 @@ import org.springframework.data.jpa.domain.JpaSort;
* @author Komi Innocent
* @author Christoph Strobl
* @author Jens Schauder
* @author Florian Lüdiger
*/
public class QueryUtilsUnitTests {
@@ -114,6 +115,11 @@ public class QueryUtilsUnitTests {
assertThat(detectAlias("select u from User u"), IS_U);
assertThat(detectAlias("select u from com.acme.User u"), IS_U);
assertThat(detectAlias("select u from T05User u"), IS_U);
assertThat(detectAlias("select * from User group by name"), isEmptyOrNullString());
assertThat(detectAlias("select * from User order by name"), isEmptyOrNullString());
assertThat(detectAlias("select * from User u group by name"), IS_U);
assertThat(detectAlias("select * from User u order by name"), IS_U);
}
@Test