Skip to content

Commit

Permalink
support 'this' with @query repository methods
Browse files Browse the repository at this point in the history
in a very temporary way

Signed-off-by: Gavin King <gavin@hibernate.org>
  • Loading branch information
gavinking authored and beikov committed Mar 28, 2024
1 parent 49d440e commit 39fb5a6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,10 @@ public interface Bookshop extends CrudRepository<Book,String> {

@Query("select isbn where title like ?1 order by isbn")
String[] ssns(String title);

@Query("select count(this) where title like ?1 order by isbn")
long count1(String title);

@Query("select count(this) where this.title like ?1 order by this.isbn")
long count2(String title);
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
import static org.hibernate.grammars.hql.HqlLexer.FROM;
import static org.hibernate.grammars.hql.HqlLexer.GROUP;
import static org.hibernate.grammars.hql.HqlLexer.HAVING;
import static org.hibernate.grammars.hql.HqlLexer.IDENTIFIER;
import static org.hibernate.grammars.hql.HqlLexer.ORDER;
import static org.hibernate.grammars.hql.HqlLexer.WHERE;
import static org.hibernate.internal.util.StringHelper.isNotEmpty;
Expand Down Expand Up @@ -2155,21 +2156,29 @@ else if ( isInsertUpdateDelete(hql) ) {
}
else {
final HqlLexer hqlLexer = HqlParseTreeBuilder.INSTANCE.buildHqlLexer( hql );
String thisText = "";
final List<? extends Token> allTokens = hqlLexer.getAllTokens();
for (Token token : allTokens) {
switch ( token.getType() ) {
//TEMPORARY until HQL gets support for 'this'
case IDENTIFIER:
final String text = token.getText();
if ( text.equalsIgnoreCase("this") ) {
thisText = " as " + text;
}
break;
case FROM:
return hql;
case WHERE:
case HAVING:
case GROUP:
case ORDER:
return new StringBuilder(hql)
.insert(token.getStartIndex(), "from " + entityType + " ")
.insert(token.getStartIndex(), "from " + entityType + thisText + " ")
.toString();
}
}
return hql + " from " + entityType;
return hql + " from " + entityType + thisText;
}
}

Expand Down

0 comments on commit 39fb5a6

Please sign in to comment.