-->
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: Difference between named queries and source code queries?
PostPosted: Thu Oct 20, 2005 1:20 pm 
Beginner
Beginner

Joined: Thu Oct 20, 2005 1:03 pm
Posts: 38
In the following query, "key" is a variable in the class Cats.

Why does this query work when embedded in a java class?
Query q = session.createQuery("from Cats where " + key + " = :value");

But not in a named query like this?
<query name="select_all_cats"><![CDATA[from cats where key = :value]]></query>

When I echo the HQL query that gets generated from both, the queries print out identical, yet when I try to run from a named query, I get no results. Both styles of query are picking up the value of "key" correctly and I know this because I can see it in the HQL that is echoed to the console.

Help! I would really like to get this query running from a named query!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 20, 2005 2:05 pm 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
for the createQuery example, what are you passing as the value for the var 'key' ; how are you calling the named query in your application?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 20, 2005 2:22 pm 
Beginner
Beginner

Joined: Thu Oct 20, 2005 1:03 pm
Posts: 38
dennisbyrne wrote:
for the createQuery example, what are you passing as the value for the var 'key' ; how are you calling the named query in your application?


the value of "key" is the name of a column name in the database table "cats" which maps to the java class Cats.

the query is called like this:

Query q = session.getNamedQuery("select_all_cats");


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 20, 2005 2:36 pm 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
take a look at http://www.hibernate.org/hib_docs/v3/re ... ting-named .

you need to bind a value to the 'value' parameter. it also looks as though you are only trying to query for a property of Cats called 'key' .

hql is case sensitive, to your going to decide on "cats" or "Cats".


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 20, 2005 2:45 pm 
Beginner
Beginner

Joined: Thu Oct 20, 2005 1:03 pm
Posts: 38
I do set the value of "value" in the source code with:

q.setParameter("value", value);

And I know it is converting the "key" variable to the column name "id" because I can see it in the hibernate query string when I echo to the console.

This is the complete text of my hibernate in source code:

Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
Query q = session.getNamedQuery("select_all_cats"); q.setParameter("value", value);
List result = q.list();
HibernateUtil.sessionFactory.close();

I know my case usage is correct. The table name is cats and the class name is Cats.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 20, 2005 2:58 pm 
Beginner
Beginner

Joined: Thu Oct 20, 2005 1:03 pm
Posts: 38
I just ran the named query again and it is true, it isn't substituting the value for "key" like I thought. Why them does it substute it correctly with an embedded createQuery statement like above but not in the named query? Am I limited to using createQuery statements when I need to use variables for column names? If so, then this limits my ability to use named queries and this is icky.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 20, 2005 3:06 pm 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
there is no substitution in the createQuery case , your literally putting the property name in the query. i don't know if you can substitute values for property/columns, but we will both know after you try ;)

if that doesn't work, try a filter. http://www.hibernate.org/hib_docs/v3/re ... te-filters

icky ? ;)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 20, 2005 3:39 pm 
Beginner
Beginner

Joined: Thu Oct 20, 2005 1:03 pm
Posts: 38
thanks for all your help and inputs Denise!

icky -- well, I just couldn't think of anything else to say. I'm tired of battling this one :-)

I know one thing for sure, if it can't be done, then we need to have Hibernate enhanced with a new method like setParameter that will set the value of a column variable. Let's call it setColumn. As a matter of fact, as much as I hate to modify third party source code, I think I'll do it!

Thanks again!


dennisbyrne wrote:
there is no substitution in the createQuery case , your literally putting the property name in the query. i don't know if you can substitute values for property/columns, but we will both know after you try ;)

if that doesn't work, try a filter. http://www.hibernate.org/hib_docs/v3/re ... te-filters

icky ? ;)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 20, 2005 4:04 pm 
Expert
Expert

Joined: Wed Apr 06, 2005 5:03 pm
Posts: 273
Location: Salt Lake City, Utah, USA
Have you looked at the Criteria interface? (http://www.hibernate.org/hib_docs/v3/reference/en/html_single/#querycriteria)

If you are trying to change which column you are using in a query, you probably want to look at using Criteria instead of named queries. It allows you to basically build queries dynamically.

_________________
nathan


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 20, 2005 5:05 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
sarahsmith_books wrote:
Why them does it substute it correctly with an embedded createQuery statement like above but not in the named query? Am I limited to using createQuery statements when I need to use variables for column names? If so, then this limits my ability to use named queries and this is icky.


The substitution you talk about in the embedded createQuery statement is merely Java String concatenation, not some magical Hibernate behavior.

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 21, 2005 8:33 am 
Beginner
Beginner

Joined: Thu Oct 20, 2005 1:03 pm
Posts: 38
Oh yes Preston, I can see that clearly now. Amazing what a little sleep will do!
Nathan, I considered criteria queries but I was hesitant to put more time into it to learn about them but it looks like I have to. I was looking for a quick way to make it work.

pksiv wrote:
sarahsmith_books wrote:
Why them does it substute it correctly with an embedded createQuery statement like above but not in the named query? Am I limited to using createQuery statements when I need to use variables for column names? If so, then this limits my ability to use named queries and this is icky.


The substitution you talk about in the embedded createQuery statement is merely Java String concatenation, not some magical Hibernate behavior.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 21, 2005 9:43 am 
Beginner
Beginner

Joined: Thu Oct 20, 2005 1:03 pm
Posts: 38
Well, like I said, a little sleep (and a lot of coffee) and the Criteria query worked great!!

Thanks everyone for your inputs!

This is a great forum. It really works!


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.