Incorrect SQL syntax is getting generated when trying to project collection properties.
for e.g. the following HQL:HQL1
Select Sel__0.name as ActivityName, Sel__0.program as Schedule, Sel__0.category as Level from com.informatica.repository.testmodels.colony.Activity as Sel__0
(NOTE: in the above HQL1 'name' is a primitive property, 'program' is a collection of type 'Schedule' and 'category' is aproperty of type 'Level' which is an enum.)
Generates the following SQL: SQL1
[select activityim0_.C_name as col_0_0_, activityim0_.C_nameisEmpty as col_0_1_, . as col_1_0_, activityim0_.C_category as col_2_0_, program1_.PK_persistentIdentifier as PK1_60_, program1_.VERS_persistentVersion as VERS2_60_, program1_.FK_event_Activity as FK3_60_, program1_.C_allDay as C4_60_, program1_.C_day as C5_60_, program1_.FK_timings_Period as FK6_60_, program1_.FK_location_Room as FK7_60_, program1_.C_firstClassParentPid as C8_60_, program1_.C_firstClassParentType as C9_60_, program1_.C_identifier as C10_60_, program1_.iContainerPid as iContai11_60_, program1_.iContainerType as iContai12_60_ from T_Activity1506318555 activityim0_ inner join T_Schedule548272467 program1_ on activityim0_.PK_persistentIdentifier=program1_.FK_event_Activity]
But if the HQL is written with an explicit inner join with the collection property type then the generated SQL is correct.
For eg. if the above HQL query is changed to the following: HQL2
Select Sel__0.name as ActivityName, Sel__1 as Schedule, Sel__0.category as Level from com.informatica.repository.testmodels.colony.Activity as Sel__0 INNER JOIN Sel__0.program as Sel__1
then the following SQL is generated: SQL2
[select activityim0_.C_name as col_0_0_, activityim0_.C_nameisEmpty as col_0_1_, program1_.PK_persistentIdentifier as col_1_0_, activityim0_.C_category as col_2_0_, program1_.PK_persistentIdentifier as PK1_60_, program1_.VERS_persistentVersion as VERS2_60_, program1_.FK_event_Activity as FK3_60_, program1_.C_allDay as C4_60_, program1_.C_day as C5_60_, program1_.FK_timings_Period as FK6_60_, program1_.FK_location_Room as FK7_60_, program1_.C_firstClassParentPid as C8_60_, program1_.C_firstClassParentType as C9_60_, program1_.C_identifier as C10_60_, program1_.iContainerPid as iContai11_60_, program1_.iContainerType as iContai12_60_ from T_Activity1506318555 activityim0_ inner join T_Schedule548272467 program1_ on activityim0_.PK_persistentIdentifier=program1_.FK_event_Activity]
The HQL and SQL AST for the two Scenarios are: FOR HQL1:
14:52:55,430 DEBUG AST:266 - --- HQL AST ---
\-[QUERY] 'query'
\-[SELECT_FROM] 'SELECT_FROM'
+-[FROM] 'from'
| \-[RANGE] 'RANGE'
| +-[DOT] '.'
| | +-[DOT] '.'
| | | +-[DOT] '.'
| | | | +-[DOT] '.'
| | | | | +-[DOT] '.'
| | | | | | +-[DOT] '.'
| | | | | | | +-[IDENT] 'com'
| | | | | | | \-[IDENT] 'informatica'
| | | | | | \-[IDENT] 'repository'
| | | | | \-[IDENT] 'testmodels'
| | | | \-[IDENT] 'colony'
| | | \-[IDENT] 'impl'
| | \-[IDENT] 'ActivityImpl'
| \-[ALIAS] 'Sel__0'
\-[SELECT] 'Select'
+-[AS] 'as'
| +-[DOT] '.'
| | +-[IDENT] 'Sel__0'
| | \-[IDENT] 'name'
| \-[IDENT] 'ActivityName'
+-[AS] 'as'
| +-[DOT] '.'
| | +-[IDENT] 'Sel__0'
| | \-[IDENT] 'program'
| \-[IDENT] 'Schedule'
\-[AS] 'as'
+-[DOT] '.'
| +-[IDENT] 'Sel__0'
| \-[IDENT] 'category'
\-[IDENT] 'Level'
14:52:55,711 DEBUG AST:232 - --- SQL AST ---
\-[SELECT] QueryNode: 'SELECT' querySpaces (T_Activity1506318555,T_Schedule548272467)
+-[SELECT_CLAUSE] SelectClause: '{select clause}'
| +-[DOT] DotNode: 'activityim0_.C_name' {propertyName=name,dereferenceType=2,propertyPath=name,path=Sel__0.name,tableAlias=activityim0_,className=com.informatica.repository.testmodels.colony.impl.ActivityImpl,classAlias=Sel__0}
| | +-[ALIAS_REF] IdentNode: 'activityim0_.PK_persistentIdentifier' {alias=Sel__0, className=com.informatica.repository.testmodels.colony.impl.ActivityImpl, tableAlias=activityim0_}
| | \-[IDENT] IdentNode: 'name' {originalText=name}
| +-[SELECT_COLUMNS] SqlNode: ' as col_0_0_'
| +-[SQL_TOKEN] SqlFragment: 'activityim0_.C_nameisEmpty'
| +-[SELECT_COLUMNS] SqlNode: ' as col_0_1_'
| +-[DOT] DotNode: '.' {propertyName=program,dereferenceType=3,propertyPath=program,path=Sel__0.program,tableAlias=program1_,className=com.informatica.repository.testmodels.colony.impl.ScheduleImpl,classAlias=null}
| | +-[ALIAS_REF] IdentNode: 'activityim0_.PK_persistentIdentifier' {alias=Sel__0, className=com.informatica.repository.testmodels.colony.impl.ActivityImpl, tableAlias=activityim0_}
| | \-[IDENT] IdentNode: 'program' {originalText=program}
| +-[SELECT_COLUMNS] SqlNode: ' as col_1_0_'
| +-[DOT] DotNode: 'activityim0_.C_category' {propertyName=category,dereferenceType=4,propertyPath=category,path=Sel__0.category,tableAlias=activityim0_,className=com.informatica.repository.testmodels.colony.impl.ActivityImpl,classAlias=Sel__0}
| | +-[ALIAS_REF] IdentNode: 'activityim0_.PK_persistentIdentifier' {alias=Sel__0, className=com.informatica.repository.testmodels.colony.impl.ActivityImpl, tableAlias=activityim0_}
| | \-[IDENT] IdentNode: 'category' {originalText=category}
| +-[SELECT_COLUMNS] SqlNode: ' as col_2_0_'
| +-[SQL_TOKEN] SqlFragment: 'program1_.PK_persistentIdentifier as PK1_60_'
| \-[SQL_TOKEN] SqlFragment: 'program1_.VERS_persistentVersion as VERS2_60_, program1_.FK_event_Activity as FK3_60_, program1_.C_allDay as C4_60_, program1_.C_day as C5_60_, program1_.FK_timings_Period as FK6_60_, program1_.FK_location_Room as FK7_60_, program1_.C_firstClassParentPid as C8_60_, program1_.C_firstClassParentType as C9_60_, program1_.C_identifier as C10_60_, program1_.iContainerPid as iContai11_60_, program1_.iContainerType as iContai12_60_'
\-[FROM] FromClause: 'from' FromClause{level=1, fromElementCounter=2, fromElements=2, fromElementByClassAlias=[Sel__0], fromElementByTableAlias=[activityim0_, program1_], fromElementsByPath=[Sel__0.program], collectionJoinFromElementsByPath=[], impliedElements=[]}
\-[FROM_FRAGMENT] FromElement: 'T_Activity1506318555 activityim0_' FromElement{explicit,not a collection join,not a fetch join,fetch non-lazy properties,classAlias=Sel__0,role=null,tableName=T_Activity1506318555,tableAlias=activityim0_,origin=null,colums={,className=com.informatica.repository.testmodels.colony.impl.ActivityImpl}}
\-[JOIN_FRAGMENT] FromElement: 'inner join T_Schedule548272467 program1_ on activityim0_.PK_persistentIdentifier=program1_.FK_event_Activity' FromElement{explicit,not a collection join,not a fetch join,fetch non-lazy properties,classAlias=null,role=com.informatica.repository.testmodels.colony.impl.ActivityImpl.program,tableName=T_Schedule548272467,tableAlias=program1_,origin=T_Activity1506318555 activityim0_,colums={activityim0_.PK_persistentIdentifier ,className=com.informatica.repository.testmodels.colony.impl.ScheduleImpl}}
For HQL2:
14:58:23,809 DEBUG AST:266 - --- HQL AST ---
\-[QUERY] 'query'
\-[SELECT_FROM] 'SELECT_FROM'
+-[FROM] 'from'
| +-[RANGE] 'RANGE'
| | +-[DOT] '.'
| | | +-[DOT] '.'
| | | | +-[DOT] '.'
| | | | | +-[DOT] '.'
| | | | | | +-[DOT] '.'
| | | | | | | +-[DOT] '.'
| | | | | | | | +-[IDENT] 'com'
| | | | | | | | \-[IDENT] 'informatica'
| | | | | | | \-[IDENT] 'repository'
| | | | | | \-[IDENT] 'testmodels'
| | | | | \-[IDENT] 'colony'
| | | | \-[IDENT] 'impl'
| | | \-[IDENT] 'ActivityImpl'
| | \-[ALIAS] 'Sel__0'
| \-[JOIN] 'JOIN'
| +-[INNER] 'INNER'
| +-[DOT] '.'
| | +-[IDENT] 'Sel__0'
| | \-[IDENT] 'program'
| \-[ALIAS] 'Sel__1'
\-[SELECT] 'Select'
+-[AS] 'as'
| +-[DOT] '.'
| | +-[IDENT] 'Sel__0'
| | \-[IDENT] 'name'
| \-[IDENT] 'ActivityName'
+-[AS] 'as'
| +-[IDENT] 'Sel__1'
| \-[IDENT] 'Schedule'
\-[AS] 'as'
+-[DOT] '.'
| +-[IDENT] 'Sel__0'
| \-[IDENT] 'category'
\-[IDENT] 'Level'
14:58:24,090 DEBUG AST:232 - --- SQL AST ---
\-[SELECT] QueryNode: 'SELECT' querySpaces (T_Activity1506318555,T_Schedule548272467)
+-[SELECT_CLAUSE] SelectClause: '{select clause}'
| +-[DOT] DotNode: 'activityim0_.C_name' {propertyName=name,dereferenceType=2,propertyPath=name,path=Sel__0.name,tableAlias=activityim0_,className=com.informatica.repository.testmodels.colony.impl.ActivityImpl,classAlias=Sel__0}
| | +-[ALIAS_REF] IdentNode: 'activityim0_.PK_persistentIdentifier' {alias=Sel__0, className=com.informatica.repository.testmodels.colony.impl.ActivityImpl, tableAlias=activityim0_}
| | \-[IDENT] IdentNode: 'name' {originalText=name}
| +-[SELECT_COLUMNS] SqlNode: ' as col_0_0_'
| +-[SQL_TOKEN] SqlFragment: 'activityim0_.C_nameisEmpty'
| +-[SELECT_COLUMNS] SqlNode: ' as col_0_1_'
| +-[ALIAS_REF] IdentNode: 'program1_.PK_persistentIdentifier as col_1_0_' {alias=Sel__1, className=com.informatica.repository.testmodels.colony.impl.ScheduleImpl, tableAlias=program1_}
| +-[DOT] DotNode: 'activityim0_.C_category' {propertyName=category,dereferenceType=4,propertyPath=category,path=Sel__0.category,tableAlias=activityim0_,className=com.informatica.repository.testmodels.colony.impl.ActivityImpl,classAlias=Sel__0}
| | +-[ALIAS_REF] IdentNode: 'activityim0_.PK_persistentIdentifier' {alias=Sel__0, className=com.informatica.repository.testmodels.colony.impl.ActivityImpl, tableAlias=activityim0_}
| | \-[IDENT] IdentNode: 'category' {originalText=category}
| +-[SELECT_COLUMNS] SqlNode: ' as col_2_0_'
| +-[SQL_TOKEN] SqlFragment: 'program1_.PK_persistentIdentifier as PK1_60_'
| \-[SQL_TOKEN] SqlFragment: 'program1_.VERS_persistentVersion as VERS2_60_, program1_.FK_event_Activity as FK3_60_, program1_.C_allDay as C4_60_, program1_.C_day as C5_60_, program1_.FK_timings_Period as FK6_60_, program1_.FK_location_Room as FK7_60_, program1_.C_firstClassParentPid as C8_60_, program1_.C_firstClassParentType as C9_60_, program1_.C_identifier as C10_60_, program1_.iContainerPid as iContai11_60_, program1_.iContainerType as iContai12_60_'
\-[FROM] FromClause: 'from' FromClause{level=1, fromElementCounter=2, fromElements=2, fromElementByClassAlias=[Sel__1, Sel__0], fromElementByTableAlias=[activityim0_, program1_], fromElementsByPath=[Sel__0.program], collectionJoinFromElementsByPath=[], impliedElements=[]}
\-[FROM_FRAGMENT] FromElement: 'T_Activity1506318555 activityim0_' FromElement{explicit,not a collection join,not a fetch join,fetch non-lazy properties,classAlias=Sel__0,role=null,tableName=T_Activity1506318555,tableAlias=activityim0_,origin=null,colums={,className=com.informatica.repository.testmodels.colony.impl.ActivityImpl}}
\-[JOIN_FRAGMENT] FromElement: 'inner join T_Schedule548272467 program1_ on activityim0_.PK_persistentIdentifier=program1_.FK_event_Activity' FromElement{explicit,not a collection join,not a fetch join,fetch non-lazy properties,classAlias=Sel__1,role=com.informatica.repository.testmodels.colony.impl.ActivityImpl.program,tableName=T_Schedule548272467,tableAlias=program1_,origin=T_Activity1506318555 activityim0_,colums={activityim0_.PK_persistentIdentifier ,className=com.informatica.repository.testmodels.colony.impl.ScheduleImpl}}
[b]Hibernate version:[3.2.4]
[b]Code between sessionFactory.openSession() and session.close():
[ [color=green]// generate query string
HQLQueryStringGenerator hqlStringGen = new HQLQueryStringGenerator();
String queryString = hqlStringGen.getQueryString(iQuery, targetValues);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(queryString);
}
// translate to HQL query
Query q = null;
try {
q = this.hibernateSession.createQuery(queryString);
} catch (HibernateException e) {
LOGGER.error("", "Error occured while creating query: " + queryString, e);
throw new MalformedQueryException(e, e.getMessage());
}
// set the max result size if specified
if (maxResultSize > 0) {
q.setMaxResults(maxResultSize);
}
// Bind parameter values
for (BindElement be : hqlStringGen.getBindDTO().getBindData()) {
IType propType = be.getPropertyType();
bind(q, propType, be.getqueryParameterName(), be.getPropertyValue());
}
// execute the HQL query
List result = null;
try {
result = q.list();
} catch (HibernateException e) {
throw new PersistenceException(e,e.getMessage());
}
][/color]
[b]Full stack trace of any exception that occurs:[
Caused by: com.shared.service.request.exceptions.SurrogateException: java.sql.SQLException: Unexpected token: . in statement [select activityim0_.C_name as col_0_0_, activityim0_.C_nameisEmpty as col_0_1_, . as col_1_0_, activityim0_.C_category as col_2_0_, program1_.PK_persistentIdentifier as PK1_60_, program1_.VERS_persistentVersion as VERS2_60_, program1_.FK_event_Activity as FK3_60_, program1_.C_allDay as C4_60_, program1_.C_day as C5_60_, program1_.FK_timings_Period as FK6_60_, program1_.FK_location_Room as FK7_60_, program1_.C_firstClassParentPid as C8_60_, program1_.C_firstClassParentType as C9_60_, program1_.C_identifier as C10_60_, program1_.iContainerPid as iContai11_60_, program1_.iContainerType as iContai12_60_ from T_Activity1506318555 activityim0_ inner join T_Schedule548272467 program1_ on activityim0_.PK_persistentIdentifier=program1_.FK_event_Activity] at org.hsqldb.jdbc.jdbcUtil.throwError(Unknown Source) at org.hsqldb.jdbc.jdbcPreparedStatement.<init>(Unknown Source) at org.hsqldb.jdbc.jdbcConnection.prepareStatement(Unknown Source) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:585) at com.mchange.v2.c3p0.stmt.GooGooStatementCache$1StmtAcquireTask.run(GooGooStatementCache.java:525) at com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:547)
]
[b]Name and version of the database you are using:[HSQL DB]
[b]The generated SQL (show_sql=true):[
[select activityim0_.C_name as col_0_0_, activityim0_.C_nameisEmpty as col_0_1_, . as col_1_0_, activityim0_.C_category as col_2_0_, program1_.PK_persistentIdentifier as PK1_60_, program1_.VERS_persistentVersion as VERS2_60_, program1_.FK_event_Activity as FK3_60_, program1_.C_allDay as C4_60_, program1_.C_day as C5_60_, program1_.FK_timings_Period as FK6_60_, program1_.FK_location_Room as FK7_60_, program1_.C_firstClassParentPid as C8_60_, program1_.C_firstClassParentType as C9_60_, program1_.C_identifier as C10_60_, program1_.iContainerPid as iContai11_60_, program1_.iContainerType as iContai12_60_ from T_Activity1506318555 activityim0_ inner join T_Schedule548272467 program1_ on activityim0_.PK_persistentIdentifier=program1_.FK_event_Activity]
]
|