-->
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.  [ 6 posts ] 
Author Message
 Post subject: SqlException when using mapped class in HQL where clause
PostPosted: Mon Feb 25, 2008 10:59 pm 
Newbie

Joined: Tue May 22, 2007 6:48 pm
Posts: 4
The following hql query worked for me in 3.1.3:

Code:
from User u where u.account = :account


where both User and Account are Hibernate mapped classes.
I.e using a mapped class in the where clause, and binding with an instance.

when trying with 3.2.6, I get the following exception:
Code:
Caused by: java.sql.SQLException: No value specified for parameter 1
   at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:910)
   at com.mysql.jdbc.PreparedStatement.fillSendPacket(PreparedStatement.java:1718)
   at com.mysql.jdbc.PreparedStatement.fillSendPacket(PreparedStatement.java:1666)
   at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1377)
   at com.caucho.sql.UserPreparedStatement.executeQuery(UserPreparedStatement.java:89)
   at com.caucho.sql.UserPreparedStatement.executeQuery(UserPreparedStatement.java:89)
   at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:186)
   at org.hibernate.loader.Loader.getResultSet(Loader.java:1787)
   at org.hibernate.loader.Loader.doQuery(Loader.java:674)
   at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
   at org.hibernate.loader.Loader.doList(Loader.java:2213)



I am not able to find any reference to this type of where clause syntax in the online doc, could it be that it was never supported, but happened to work in 3.1? I can't find anything in the 3.2 release docs that seems related though.

I am using MySql 5.0.22

Any help is appreciated,

/ Christian


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 26, 2008 12:34 pm 
Senior
Senior

Joined: Fri Jun 01, 2007 12:41 pm
Posts: 121
Did you specify the value for account the query. You code should like like:


Query q = session.createQuery("from User u where u.account = :account");
q.setParameter("account", new Integer(123));
List results = q.list;


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 26, 2008 1:28 pm 
Newbie

Joined: Tue May 22, 2007 6:48 pm
Posts: 4
Yeah sorry, I should have been clearer in the example. "account" is actually an instance of the Account class, like so:

Account account = {retrieve account here}
Query q = session.createQuery("from User u where u.account = :account");
q.setParameter("account", account );
List results = q.list;

in 3.1.3, the generated SQL includes the join between the "User" and "Account" tables. In 3.2.6 it seems like it is expecting account to be just a row in the "User" table, and also the resulting id isn't bound in the SQL.


It works fine if in 3.2.6 if I change the query to use the primary key in the where clause:

Query q = session.createQuery("from User u where u.account.id = :id");
q.setParameter("id", new Long(35) );
List results = q.list;

However, I have a lot of code that is using the pattern above, so I would like to keep using that if possible..


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 26, 2008 1:57 pm 
Senior
Senior

Joined: Fri Jun 01, 2007 12:41 pm
Posts: 121
Try with Criteria and Restrictions.

Code:
Account account = {retrieve account here}
List result = session.createCriteria(User.class)
.add(Restrictions.eq("account", account)
.list;


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 26, 2008 2:44 pm 
Newbie

Joined: Tue May 22, 2007 6:48 pm
Posts: 4
I get the same problem when using the Criteria API unfortunately..
Could it be that this syntax was never supported, but "happened" to work earlier? There seems to be some query changes of this type in 3.2, re http://www.hibernate.org/250.html#A36 ..


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 27, 2008 7:00 pm 
Newbie

Joined: Tue May 22, 2007 6:48 pm
Posts: 4
Just realized that the association between Account and User is one-to-one, we have similar queries where the association is one-to-many that work fine in 3.2.6..


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.