-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 12 posts ] 
Author Message
 Post subject: hibernate 3.0 query fails under 3.1
PostPosted: Wed Dec 14, 2005 2:26 am 
Newbie

Joined: Wed Dec 14, 2005 2:15 am
Posts: 4
Hi,

I have a query that works under 3.0, and now fails with a parse error under 3.1

The query is "from com.test.User user where user.contacts.pCard = ? or ? in elements(user.contacts.bCards)"

contacts is a collection, and pCard is a single entity.

If I remove the pCard portion, it fails (with the same reason) on the bCards bit which is a collection.

The query should find all users that have a contact in their contacts collection that have a pCard equal to the one I substitute into the query, or, a bCard that is equal to the card I pass in.

Is this query fundementally wrong (I don't think so).

I found the change in cvs: http://cvs.sourceforge.net/viewcvs.py/h ... 1.7&r2=1.8

I think a legitimate problem was trying to be solved, but was solved incorrectly.

Hibernate version: 3.1
Code between sessionFactory.openSession() and session.close():
Object[] params = new Object[2];
params[0] = card;
params[1] = card;
return getHibernateTemplate().find("from com.test.User user where user.contacts.pCard = ? or ? in elements(user.contacts.bCards)", params);

Full stack trace of any exception that occurs:
Caused by: illegal syntax near collection: pCard
at org.hibernate.hql.ast.tree.DotNode.checkLhsIsNotCollection(DotNode.java:474)
at org.hibernate.hql.ast.tree.DotNode.resolve(DotNode.java:193)
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:660)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.expr(HqlSqlBaseWalker.java:1213)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.exprOrSubquery(HqlSqlBaseWalker.java:4022)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.comparisonExpr(HqlSqlBaseWalker.java:3508)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:1756)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:1706)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.whereClause(HqlSqlBaseWalker.java:774)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:575)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:279)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:227)
at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:218)

Name and version of the database you are using:
MySQL 5.0

The generated SQL (show_sql=true):
none - doesn't parse


Top
 Profile  
 
 Post subject: update
PostPosted: Wed Dec 14, 2005 12:56 pm 
Newbie

Joined: Wed Dec 14, 2005 2:15 am
Posts: 4
As a test, I commented out the calls to checkLhsIsNotCollection and my query executes properly now (returning the data I would expect).

Steven Ebersole - you seemed to have made the change to DotNode, can you discuss the goal of the change?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 17, 2005 1:32 pm 
Beginner
Beginner

Joined: Sat Dec 17, 2005 1:24 pm
Posts: 42
Location: Berlin, Germany
I have exactly the same problem. I use several queries which have the same layout as the one described in the first post.

Is that not correct usage of HQL?
Does someone know of a better way to retrieve the requested data?

All the best,

René


Top
 Profile  
 
 Post subject: solution
PostPosted: Sat Dec 17, 2005 2:39 pm 
Newbie

Joined: Wed Dec 14, 2005 2:15 am
Posts: 4
I think the answer is to use the JOIN syntax.

Personally I find the explicit join a little bit less intuitive than the implied join, but I can live with whatever choices the developers have made.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 17, 2005 3:32 pm 
Beginner
Beginner

Joined: Sat Dec 17, 2005 1:24 pm
Posts: 42
Location: Berlin, Germany
I have the slightly different problem, that I want to retrieve an object based on some properties of elements in the collection

Code:
from A nameA where nameA.B.propertyOfB=? AND nameA.propertyOfA=?


That query worked fine in 3.0.5. How would I express that correctly?

René


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 17, 2005 5:14 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
HQL never, ever supported navigating through a collection-valued association! I find it very difficult to believe that this ever worked, but if it did, it worked by accident in some small number of cases and was never part of the official definition of HQL as per the documentation.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 17, 2005 6:48 pm 
Newbie

Joined: Wed Dec 14, 2005 2:15 am
Posts: 4
Hey Gavin,

Yes I agree it's not a documented, supported solution. I also figured out I need to use the join, my only thoughts were that the syntax of searching without the join is pretty intuitive.

Thanks for a great product.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 18, 2005 11:09 am 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
i see that hibernate 3.1 don't convert parameter added to query - previous versions do ti

for example,
if property a is string and you whave query :

... entity.a=?

and add
q.setParameter(0,anyObject);

last hibernate 3.1 will not convert anyObject to String, previous versions (3.0 and any version 3.1 before do conversion

query don't get error - it return null, only


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 19, 2005 12:17 am 
Beginner
Beginner

Joined: Fri Nov 04, 2005 3:51 pm
Posts: 32
I also have problems with query.setParameter(n,Object). I think I only used it in 3.1 so I don't have previous version to compare to. So it's interesting that it used to handle this better.
I can't even pass it an Integer if it's expecting a Long. It's as if it doesn't even try any conversion and if it's not the exact same as the mapped type, then it just quietly sets it to null.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 10, 2006 6:10 am 
Newbie

Joined: Wed Feb 08, 2006 8:29 am
Posts: 10
Hi,

I have encountered this problem as well, where my HQL uses collections in the path to joins. Gavin said that this is an accident that it works, but it does work, and it seems to work very well. It greatly simplifies the syntax of the query. Are there any plans to make this functionality available again as part of the official functionality?

Thanks,
Juo


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 10, 2006 11:05 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Absolutely not. You may think it makes the query "simplier", but in fact it is non-sensical.

Go back to the original query 'user.contacts.pCard'. The User entity has a property named 'contacts', so no problem there. Now 'contacts' happens to be of type Set (or List, whatever); show me where java.util.Set has a property named 'pCard'...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 13, 2006 10:33 am 
Newbie

Joined: Wed Feb 08, 2006 8:29 am
Posts: 10
Thanks for the reply Steve.

What you say makes perfect sense. It would be nice to have some kind of functionality like that, one which did make sense, but at least I know that I must rewrite my HQL.

Cheers,
Juo


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 12 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.