-->
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.  [ 4 posts ] 
Author Message
 Post subject: Dynamic add condition to native sql
PostPosted: Tue Nov 27, 2007 12:25 am 
Newbie

Joined: Tue Nov 27, 2007 12:16 am
Posts: 4
I define a named native SQL. But the where condition is not static. It must dynamic add according input parameter. How can I do?
Named native SQL is:

<sql-query name="test">
select name, age
from student
where age > 30
</sql-query>

If user input name parameter, the sql will become:

select name, age
from student
where age > 30
and name='Mike';

In other word, add name condition to the sql.

If name paramter is null, don't add name condition.
How can I do?

In program, I write code for example:

SQLQuery q = session.getNamedQuery("test");

But I can't add new condition to this sql query. How Can I do?


Last edited by hns1971 on Tue Nov 27, 2007 12:58 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 27, 2007 12:50 am 
Regular
Regular

Joined: Mon Mar 26, 2007 12:38 am
Posts: 119
Hi,
Do Criteria Queries help you ?
They can be used to build queries at run-time.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 27, 2007 1:02 am 
Newbie

Joined: Tue Nov 27, 2007 12:16 am
Posts: 4
pramodkp wrote:
Hi,
Do Criteria Queries help you ?
They can be used to build queries at run-time.


Thank you very much. But I want to use native SQL. Can I get the native SQL and then add codition at runtime?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 28, 2007 9:49 am 
Newbie

Joined: Wed Nov 28, 2007 9:40 am
Posts: 1
You can try this...

SQLQuery query = (SQLQuery)session.getNamedQuery("test").getQueryString()+" and name='Mike' "

... but be carefull if you are ordering or grouping...
Using Criteria would be the best way to do it.


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