Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong templates object for embedded function used when calling SUBSTR_2ARGS function #209

ageery opened this issue Aug 3, 2012 · 3 comments


Copy link

@ageery ageery commented Aug 3, 2012

I'm using JPAQuery to build my queries (Hibernate is my JPA provider). I've found that if I use the INDEX_OF function inside of a SUBSTR_2ARGS function, the Templates class used by the INDEX_OF function is not the same Templates class that is by the JPAQuery object. Instead, the INDEX_OF function is using the Templates.DEFAULT variable.

I noticed the problem when doing something like this:

final Expression startIndex = Expressions.constant(0);
final Expression endIndex = NumberOperation.create(Integer.class, Ops.INDEX_OF, path, delimiter);
final Expression substr = StringOperation.create(Ops.SUBSTR_2ARGS, path, startIndex, endIndex);

This should generate HQL (as opposed to SQL) that's something like: substring(path, 0, locate(path, delimiter)). Instead, it generates this: substring(path, 0, indexOf(path, delimiter)).

It is doing this because in SerializerBase.visitOperation, the logic is evaluating this clause:

else if (element.isAsString()) {

This eventually calls ExpressionBase.toString():

public final String toString() {
if (toString == null) {
toString = accept(ToStringVisitor.DEFAULT, Templates.DEFAULT);
return toString;

This uses the DEFAULT template rather than the template associated with the query.

I think the problem is actually in the definition of the INDEX_OF in JPQLTemplates:

add(Ops.INDEX_OF_2ARGS, "locate({1},{0},{2s}+1) - 1");

It seems that the second argument shouldn't be marked as a String (the "s" suffix) since it is, in fact, a number (the starting position for the search []). However, I see that other function definitions in this class also use the "s" suffix where a number is expected so perhaps I'm misunderstanding the suffix...

The other fix would be to pass the templates currently being used by the SerializerBase object to Expression.toString() in SerializerBase.appendAsString(Expressiom<?>).

Copy link

@timowest timowest commented Aug 3, 2012

The "s" suffix is used to serialize some constants directly into the query instead of using prepared statements, this is mostly used for index parameters.

I fixed this now to serialize only Constants directly for the "s" flag and serializing everything else normally.

Copy link

@timowest timowest commented Aug 4, 2012

Released in 2.7.2

@timowest timowest closed this Aug 4, 2012
Copy link

@ageery ageery commented Aug 4, 2012

Thank you!

On Sat, Aug 4, 2012 at 1:37 PM, Timo Westkämper <


Released in 2.7.2

Reply to this email directly or view it on GitHub:
#209 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants