Bad SQL Grammer: Implicit join when using r.fooClass.attributes[?]=?

Description

Please find this test case atteched to demonstrate the bug, maybe you can add to the hibernate distribution for future testing and piece of mind for me.

The error:

Testsuite: org.hibernate.test.joinedmapfilter.JoinedMapFilterTest
Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0.498 sec

Testcase: testJoinedMapFilter took 0.495 sec
Caused an ERROR
could not execute query
org.hibernate.exception.SQLGrammarException: could not execute query
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.loader.Loader.doList(Loader.java:2147)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2028)
at org.hibernate.loader.Loader.list(Loader.java:2023)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:393)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:338)
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:172)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1121)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
at org.hibernate.test.joinedmapfilter.JoinedMapFilterTest.testJoinedMapFilter(JoinedMapFilterTest.java:116)
at org.hibernate.test.TestCase.runTest(TestCase.java:250)
at org.hibernate.test.TestCase.runBare(TestCase.java:316)
Caused by: java.sql.SQLException: Unknown table 'productdet1_' in where clause
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2928)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1571)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1666)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2994)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:936)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1030)
at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:186)
at org.hibernate.loader.Loader.getResultSet(Loader.java:1668)
at org.hibernate.loader.Loader.doQuery(Loader.java:662)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
at org.hibernate.loader.Loader.doList(Loader.java:2144)
... 25 more

SELECT r FROM com.company.Toplevel AS r WHERE r.toplevel=? AND rec.productDetail.attributes[?]=?

Also my recent post: http://forum.hibernate.org/viewtopic.php?t=966374&start=0&postdays=0&postorder=asc&highlight=

This testcase has been tested with 3.1.3 and 3.2.0-ga.

2nd problem:

There is also another situation in the testcase which I found through serendipity which causes a NPE inside hibernate because I misspelled "r" as "rec". Check out the commented out section and swap over the createQuery() lines to demonstrate that NPE.

Attachments

2

Activity

Show:

Brett MeyerJuly 8, 2014 at 3:12 PM

Bulk rejecting stale issues. If this is still a legitimate issue on ORM 4, feel free to comment and attach a test case. I'll address responses case-by-case. Thanks!

Brett MeyerApril 7, 2014 at 5:45 PM

In an effort to clean up, in bulk, tickets that are most likely out of date, we're transitioning all ORM 3 tickets to an "Awaiting Test Case" state. Please see http://in.relation.to/Bloggers/HibernateORMJIRAPoliciesAndCleanUpTactics for more information.

If this is still a legitimate bug in ORM 4, please provide either a test case that reproduces it or enough detail (entities, mappings, snippets, etc.) to show that it still fails on 4. If nothing is received within 3 months or so, we'll be automatically closing them.

Thank you!

Darryl MilesNovember 2, 2008 at 3:45 PM

HQL:

13:34:03,477 DEBUG QueryTranslatorImpl:241 - HQL: SELECT r FROM org.hibernate.test.joinedmapfilter.Toplevel AS r WHERE r.toplevelId=? AND r.productDetail.attributes[?]=?

SQL (written out nicer) :

13:34:03,478 DEBUG QueryTranslatorImpl:242 - SQL:
select
toplevel0_.toplevel_id as toplevel1_0_,
toplevel0_.long_value_one as long2_0_,
toplevel0_.product_detail_id as product3_0_,
toplevel0_.description as descript4_0_
from
toplevel toplevel0_,
/////////////// MISSING TABLE NOT EMITTED BY HIB "product_detail productdet1_, " ///////////////
product_detail_attrs attributes2_
where
toplevel0_.product_detail_id=productdet1_.product_detail_id
and productdet1_.product_detail_id=attributes2_.product_detail_id
and attributes2_.name = ?
and toplevel0_.toplevel_id=?
and attributes2_.value=?

I inserted the missing: "product_detail productdet1_" in the FROM clause. That missing table is the reason for this bug report (just in case it hasn't been obvious so far).

Darryl MilesNovember 2, 2008 at 3:39 PM

Attached is a logging output of testcase here are the notable entries.

13:34:02,609 DEBUG DotNode:591 - getDataType() : productDetail -> org.hibernate.type.ManyToOneType(org.hibernate.test.joinedmapfilter.ProductDetail)
13:34:02,610 DEBUG DotNode:390 - dereferenceEntityJoin() : generating join for productDetail in org.hibernate.test.joinedmapfilter.Toplevel {no alias} parent = [ ( . ( . toplevel0_.toplevel_id productDetail ) attributes ) ]
13:34:02,616 DEBUG FromElement:131 - FromClause{level=1} : org.hibernate.test.joinedmapfilter.ProductDetail (no alias) -> productdet1_
13:34:02,617 DEBUG FromClause:257 - addJoinByPathMap() : r.productDetail -> product_detail productdet1_
13:34:02,618 DEBUG FromReferenceNode:74 - Resolved : r.productDetail -> toplevel0_.product_detail_id

Notice here the 2nd FROM_FRAGMENT of ImpliedFromElement with '' as the getText(). This should be 'product_detail productdet1_' you can see it knows the tableName and tableAlias but the getText() is blank.

13:34:02,680 DEBUG AST:258 - — SQL AST —
-[SELECT] QueryNode: 'SELECT' querySpaces (product_detail_attrs,toplevel,product_detail)
+-[SELECT_CLAUSE] SelectClause: '{select clause}'

+-[ALIAS_REF] IdentNode: 'toplevel0_.toplevel_id as toplevel1_0_' {alias=r, className=org.hibernate.test.joinedmapfilter.Toplevel, tableAlias=toplevel0_}

-[SQL_TOKEN] SqlFragment: 'toplevel0_.long_value_one as long2_0_, toplevel0_.product_detail_id as product3_0_, toplevel0_.description as descript4_0_'
+-[FROM] FromClause: 'FROM' FromClause{level=1, fromElementCounter=2, fromElements=3, fromElementByClassAlias=[r], fromElementByTableAlias=[toplevel0_, productdet1_, attributes2_], fromElementsByPath=[r.productDetail], collectionJoinFromElementsByPath=[r.productDetail.attributes], impliedElements=[]}

+-[FROM_FRAGMENT] FromElement: 'toplevel toplevel0_' FromElement{explicit,not a collection join,not a fetch join,fetch non-lazy properties,classAlias=r,role=null,tableName=toplevel,tableAlias=toplevel0_,origin=null,colums={,className=org.hibernate.test.joinedmapfilter.Toplevel}}

+-[FROM_FRAGMENT] ImpliedFromElement: '' ImpliedFromElement{implied,collection join,not a fetch join,fetch non-lazy properties,classAlias=null,role=null,tableName=product_detail,tableAlias=productdet1_,origin=toplevel toplevel0_,colums={toplevel0_.product_detail_id ,className=org.hibernate.test.joinedmapfilter.ProductDetail}}

-[FROM_FRAGMENT] ImpliedFromElement: 'product_detail_attrs attributes2_' ImpliedFromElement{implied,collection join,not a fetch join,fetch non-lazy properties,classAlias=null,role=org.hibernate.test.joinedmapfilter.ProductDetail.attributes,tableName={none},tableAlias=attributes2_,origin=toplevel toplevel0_,colums={,className=null}}
-[WHERE] SqlNode: 'WHERE'
+-[THETA_JOINS] SqlNode: '{theta joins}'

+-[SQL_TOKEN] SqlFragment: 'toplevel0_.product_detail_id=productdet1_.product_detail_id'

-[SQL_TOKEN] SqlFragment: 'productdet1_.product_detail_id=attributes2_.product_detail_id and attributes2_.name = ?'
-[AND] SqlNode: 'AND'
+-[EQ] BinaryLogicOperatorNode: '='

+-[DOT] DotNode: 'toplevel0_.toplevel_id' {propertyName=toplevelId,dereferenceType=4,propertyPath=toplevelId,path=r.toplevelId,tableAlias=toplevel0_,className=org.hibernate.test.joinedmapfilter.Toplevel,classAlias=r}

 

+-[ALIAS_REF] IdentNode: 'toplevel0_.toplevel_id' {alias=r, className=org.hibernate.test.joinedmapfilter.Toplevel, tableAlias=toplevel0_}

 

-[IDENT] IdentNode: 'toplevelId' {originalText=toplevelId}

-[PARAM] ParameterNode: '?' {ordinal=0, expectedType=org.hibernate.type.LongType@6e3e5e}
-[EQ] BinaryLogicOperatorNode: '='
+-[INDEX_OP] IndexNode: 'attributes2_.value' {ImpliedFromElement{implied,collection join,not a fetch join,fetch non-lazy properties,classAlias=null,role=org.hibernate.test.joinedmapfilter.ProductDetail.attributes,tableName={none},tableAlias=attributes2_,origin=toplevel toplevel0_,colums={,className=null}}}

+-[DOT] DotNode: '.' {propertyName=attributes,dereferenceType=3,propertyPath=attributes,path=r.productDetail.attributes,tableAlias=attributes2_,className=null,classAlias=null}

 

+-[DOT] DotNode: 'toplevel0_.product_detail_id' {propertyName=productDetail,dereferenceType=1,propertyPath=productDetail,path=r.productDetail,tableAlias=productdet1_,className=org.hibernate.test.joinedmapfilter.ProductDetail,classAlias=null}

 

 

+-[ALIAS_REF] IdentNode: 'toplevel0_.toplevel_id' {alias=r, className=org.hibernate.test.joinedmapfilter.Toplevel, tableAlias=toplevel0_}

 

 

-[IDENT] IdentNode: 'productDetail' {originalText=productDetail}

 

-[IDENT] IdentNode: 'attributes' {originalText=attributes}

-[PARAM] ParameterNode: '?' {ordinal=1, expectedType=null}
-[PARAM] ParameterNode: '?' {ordinal=2, expectedType=null}

There is also ./core/src/main/java/org/hibernate/hql/ast/SqlGenerator.java around line 249. I guess my missing table in the FROM clause is one of these "ghosts".

///////////////////////////////////////////////////////////////////////
// HACK ALERT !!!!!!!!!!!!!!!!!!!!!!!!!!!!
// Attempt to work around "ghost" ImpliedFromElements that occasionally
// show up between the actual things being joined. This consistently
// occurs from index nodes (at least against many-to-many). Not sure
// if there are other conditions
//
// Essentially, look-ahead to the next FromElement that actually
// writes something to the SQL
while ( right != null && !hasText( right ) ) {
right = ( FromElement ) right.getNextSibling();
}
if ( right == null ) {
return;
}
///////////////////////////////////////////////////////////////////////

Darryl MilesNovember 2, 2008 at 3:35 PM

Logging output of testcase.

Rejected

Details

Assignee

Reporter

Components

Affects versions

Priority

Created November 21, 2006 at 6:27 PM
Updated July 8, 2014 at 3:12 PM
Resolved July 8, 2014 at 3:12 PM