Error during parse query on MS SQL

Description

HQL: select deal, items.dateBegin, client.Title from " + CoreDeal.class.getName() + " deal left join deal.stagesWorkflowInstance.history.items items, " + CoreClient.class.getName() + " client where stageResponsible=:stageResponsible and items.index=maxindex(items) and deal.parent=client and deal.stagesWorkflowInstance.Stage.showOnPersonalPage=1

It works normally on Oracle, but on MS SQL produces:

Error: String index out of range: -5
[java.lang.StringIndexOutOfBoundsException]
java.lang.String.substring(String.java:1768)
java.lang.String.substring(String.java:1735)
org.hibernate.hql.CollectionSubqueryFactory.createCollectionSubquery(CollectionSubqueryFactory.java:32)
org.hibernate.hql.ast.tree.FromElementType.toColumns(FromElementType.java:301)
org.hibernate.hql.ast.tree.FromElementType.toColumns(FromElementType.java:291)
org.hibernate.hql.ast.tree.FromElement.toColumns(FromElement.java:377)
org.hibernate.hql.ast.tree.MethodNode.resolveCollectionProperty(MethodNode.java:115)
org.hibernate.hql.ast.tree.MethodNode.collectionProperty(MethodNode.java:95)
org.hibernate.hql.ast.tree.MethodNode.resolve(MethodNode.java:44)
org.hibernate.hql.ast.HqlSqlWalker.processFunction(HqlSqlWalker.java:844)
org.hibernate.hql.antlr.HqlSqlBaseWalker.functionCall(HqlSqlBaseWalker.java:2324)
org.hibernate.hql.antlr.HqlSqlBaseWalker.expr(HqlSqlBaseWalker.java:1285)
org.hibernate.hql.antlr.HqlSqlBaseWalker.exprOrSubquery(HqlSqlBaseWalker.java:4032)
org.hibernate.hql.antlr.HqlSqlBaseWalker.comparisonExpr(HqlSqlBaseWalker.java:3521)
org.hibernate.hql.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:1758)
org.hibernate.hql.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:1686)
org.hibernate.hql.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:1683)
org.hibernate.hql.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:1683)
org.hibernate.hql.antlr.HqlSqlBaseWalker.whereClause(HqlSqlBaseWalker.java:776)
org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:577)
org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:281)
org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:229)
org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:227)
org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:159)
org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:110)
org.hibernate.engine.query.HQLQueryPlan.(HQLQueryPlan.java:77)
org.hibernate.engine.query.HQLQueryPlan.(HQLQueryPlan.java:56)
org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:71)
org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)
org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)
org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1612)
ru.naumen.crm2.bobjects.deal.CoreDealHibernateHandler.listAllDealsWithSortDataByResponsible(CoreDealHibernateHandler.java:109)
ru.naumen.crm2.ui.tlc.CoreEmployeeTableListController.listMyDealsSorted(CoreEmployeeTableListController.java:70)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

Environment

Microsoft SQL Server 2000, Windows XP, JDK 1.5 Update 4

Activity

Show:
Matthias Müller
January 3, 2017, 11:47 AM
Edited

Hm, I hoped there were some comments/news here after a few months

Matthias Herlitzius
February 7, 2017, 7:03 AM

It would be nice to see this bug resolved!

HailtonB
May 2, 2017, 9:36 PM
Edited

The problem appears to occur only with "collection properties" with join different from "inner join" ("left join", for example).
According to my tests and debugs I was able to detect this situation:

My suggestion is:

  1. I think the fix can be done creating a method "org.hibernate.engine.internal.JoinSequence.copyForCollectionProperty()" like that:

  2. and change org.hibernate.hql.internal.ast.tree.FromElementType (line: 621) to:

As soon as I can, I'll send a pull request.

HailtonB
May 3, 2017, 12:52 AM

I made a workaround to solve this problem for a software project in which I work.
I made it to Hibernate versions 3.6, 4.3 and 5.1. To apply, you need to replace the "org.hibernate.hql.spi.QueryTranslatorFactory" default by "ASTQueryTranslatorFactoryWaHhh1830" using the Hibernate property "hibernate.query.factory_class".
I tested only for version 3.6!
Zip's:

Vlad Mihalcea
January 15, 2018, 1:33 PM

Add test from this PR and fix as mentioned in the comments of this Jira issue.

Fixed

Assignee

Steve Ebersole

Reporter

DenD

Fix versions

backPortable

Backport?

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

Priority

Major
Configure