-->
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.  [ 10 posts ] 
Author Message
 Post subject: Can a column name be replaced by named parameter?
PostPosted: Tue Oct 18, 2005 9:21 am 
Newbie

Joined: Tue Sep 06, 2005 9:50 am
Posts: 13
Hi all,
is it possible to use named parmeters also for the property names?
In this case I used ":show" to represent showPPO property of the POJO. When I execute the code no errors are returned but the list I get is empty. If I remove ":show" and put p.showPPO than I get a filled list.

Hibernate version: 2.1.8

Mapping documents:

Code between sessionFactory.openSession() and session.close():
Code:
String queryString = "from Promotion p where :date between p.startDate AND p.endDate AND p.status = :status"
   + " AND (:show like :id1 OR :show like :id2 OR :show like :id3 OR p.showPPO like 'message.promotion.show.all' OR p.showto like 'message.promotion.showto.both')";
           
Query hqlQuery = session.createQuery(queryString).setDate("date", new Date()).setString("status", "message.promotion.state.active");

hqlQuery.setString("show", "p.showPPO");
hqlQuery.setString("id1", String.valueOf(user.getWorkmate().getPpoId() + ",%"));
hqlQuery.setString("id2", "%," + String.valueOf(user.getWorkmate().getPpoId() + ",%"));
hqlQuery.setString("id3", "%," + String.valueOf(user.getWorkmate().getPpoId()));


Full stack trace of any exception that occurs: No errors

Name and version of the database you are using: Oracle 9i

The generated SQL (show_sql=true):
from Promotion p where :date between p.startDate AND p.endDate AND p.status = :status AND (:show like :id1 OR :show like :id2 OR :show like :id3 OR p.showPPO like 'message.promotion.show.all' OR p.showto like 'message.promotion.showto.both')


Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 18, 2005 11:24 am 
Beginner
Beginner

Joined: Mon Aug 16, 2004 6:15 am
Posts: 24
I think you may want to look at using Criteria queries for this.

This would allow you to perform something like the following (example from the javadoc of Criteria).

Code:
List cats = session.createCriteria(Cat.class)
     .add( Expression.like("name", "Iz%") )
     .add( Expression.gt( "weight", new Float(minWeight) ) )
     .addOrder( Order.asc("age") )
     .list();


Top
 Profile  
 
 Post subject: Re: Can a column name be replaced by named parameter?
PostPosted: Tue Oct 18, 2005 11:42 am 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
Can you actually do what you're attempting using indexed parameters ?

This has nothing to do with Hibernate. I'm pretty sure that the SQL standard doesn't allow you to do things like "WHERE :x = :y"

_________________
Preston

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


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 19, 2005 10:07 am 
Newbie

Joined: Tue Sep 06, 2005 9:50 am
Posts: 13
The probelm with criteria is how to implement ":date between p.startDate and p.EndDate"

Normally the criteria supports "p.startDate between :date1 and :date2"


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 19, 2005 10:26 am 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
turkosh wrote:
The probelm with criteria is how to implement ":date between p.startDate and p.EndDate"

Normally the criteria supports "p.startDate between :date1 and :date2"

Code:
criteria.add(Restrictions.and(
                  Restrictions.le(p.startDate, :date),
                  Restrictions.ge(p.endDate, :date))

_________________
Preston

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


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 19, 2005 11:18 am 
Newbie

Joined: Tue Oct 18, 2005 3:07 pm
Posts: 2
This is a really stupid reply. It has nothing to do with the question asked.
He's isn't asking about the date part of the query - he's asking about the :show -

As for the answer, I don't believe you can do it - I have tried a zillion ways to do this and nothing works unless you create the query in the java object itself which allows you to use double quotes around the query string and + in the value you want for :show

like this:

String queryString = "from Promotion p where :date between p.startDate AND p.endDate AND p.status = :status"
+ " AND (" + show + " like :id1 OR " + show + " like :id2 OR " + show + " like :id3 OR p.showPPO like 'message.promotion.show.all' OR p.showto like 'message.promotion.showto.both')";

The only drawback is that you can't use a named query as far as I can tell because named queries don't allow double quotes.

If anyone has been able to do it in a named query, I would like to know how.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 19, 2005 2:04 pm 
Newbie

Joined: Tue Oct 18, 2005 3:07 pm
Posts: 2
It has come to my attention that I might have offended people. I didn't mean to use the word stupid in the way americans use it. I meant it to mean unreasoned thinking, going down the wrong path. Sorry for offensing.

betsyruns wrote:
This is a really stupid reply. It has nothing to do with the question asked.
He's isn't asking about the date part of the query - he's asking about the :show -

As for the answer, I don't believe you can do it - I have tried a zillion ways to do this and nothing works unless you create the query in the java object itself which allows you to use double quotes around the query string and + in the value you want for :show

like this:

String queryString = "from Promotion p where :date between p.startDate AND p.endDate AND p.status = :status"
+ " AND (" + show + " like :id1 OR " + show + " like :id2 OR " + show + " like :id3 OR p.showPPO like 'message.promotion.show.all' OR p.showto like 'message.promotion.showto.both')";

The only drawback is that you can't use a named query as far as I can tell because named queries don't allow double quotes.

If anyone has been able to do it in a named query, I would like to know how.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 19, 2005 3:09 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
No offense taken, mainly because I read the last post before reading the prior post calling me stupid. ;-)

Now that I finally understand your question, you can definitely NOT do what you are trying to do. The named and indexed parameter replacement is just that, parameter replacement. It's not intended for replacing any arbitrary part of the query.

_________________
Preston

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


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 20, 2005 3:23 am 
Newbie

Joined: Tue Sep 06, 2005 9:50 am
Posts: 13
Thanks for all the answers. Now I understand why I can't replace columns with named parameters.
So I created some filters that alter from the second part of the query the comlumn names.
The problem is the result that I get from the query.
The second query that looks up throug a column - that contains string ids seperated by comma's (2,5,8,12,18) or contains the text "message.promotion.show.all" - and should return the matching record by given parameter user.getWorkmate().getPpoId().

So if the parameter is 8 it should look through the column for an "8,%" "%,8,%" and "%,8".
The weird thing is that this query allways returns results, even if the 8 isn't present inside the column. And idea's on how to rectify this?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 20, 2005 3:31 am 
Newbie

Joined: Tue Sep 06, 2005 9:50 am
Posts: 13
I think I have located the problem:
(p.showto like 'message.promotion.showto.both') is the dirty one. Thanks for all your input guys.


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