How just found this and wondered whether this is expected behavior or a bug. Being expected behavior this should have been at least reminded with strong red in documentation, while is somehow counterintuitiv.
So imagine two tables
CARD:
primary key:
GUID
other fields:
name -- string
state -- string
PAYMENT
primary key:
card(foreign key to CARD through GUID)
payment_time -- timestamp
other fields:
...
I do hibernate query against PAYMENT:
session.createQuery("from PAYMENT where id.card.name = 'KAWAPONGA'").list()
does work ok.
Then try
session.createQuery("from PAYMENT where id.card.state = 'OK'").list()
works either.
BUT! try the both conditions combined:
session.createQuery("from PAYMENT where id.card.name = 'KAWAPONGA' and id.card.state = 'OK'").list()
And you'll get the
RuntimeException in...
at org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:59)
at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:223)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:156)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:103)
at org.hibernate.impl.SessionFactoryImpl.getQuery(SessionFactoryImpl.java:473)
at org.hibernate.impl.SessionImpl.getQueries(SessionImpl.java:1060)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1010)
...
Caused by: Invalid path: 'id.card.state'
at org.hibernate.hql.ast.util.LiteralProcessor.lookupConstant(LiteralProcessor.java:93)
at org.hibernate.hql.ast.tree.DotNode.resolve(DotNode.java:178)
at org.hibernate.hql.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:94)
at org.hibernate.hql.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:90)
at org.hibernate.hql.ast.HqlSqlWalker.resolve(HqlSqlWalker.java:626)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.expr(HqlSqlBaseWalker.java:1215)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.exprOrSubquery(HqlSqlBaseWalker.java:4024)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.comparisonExpr(HqlSqlBaseWalker.java:3872)
Wow, this looks already weird. But try then
session.createQuery("from PAYMENT where id.card.name = 'KAWAPONGA' and state = 'OK'").list()
... and you'll get no exception.
It seems the parser, that parses these dot-qualified names keeps the state of the last id parsed, so the further id are counted in virtually the same namespace as the last id parsed (so it is like with directories descent down but forgot to jump up afterwards)? The normal behavior to be expected is that the namespace is always root namespace unless somehow specified otherwise, isn't it?
With table names explicitly specified, e.g.
session.createQuery("from PAYMENT p where p.id.card.name = 'KAWAPONGA' and p.id.card.state = 'OK'").list()
.. there is no problem.
Hibernate version:
tried with 3.1
Some comments on this behavior?
|