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: Optional Parameters and SQL Query in Hibernate Mapping file
PostPosted: Tue Jul 03, 2007 5:12 pm 
Beginner
Beginner

Joined: Wed Sep 21, 2005 12:02 am
Posts: 31
Using Hibernate 3.0.

I have an SQL query in a hibernate mapping file. This has been serving me well up to now. I have now been asked to do the same query but with three optional parameters. At the moment the only approach I can think of is to write multiple queries for the different combinations of parameters, but this is neither elegant or easy to maintain. Another approach is to move the query into the Java code, but I want to avoid that, since we want to keep the query text separate from the Java code.

Does anyone know whether this is possible. If using HQL is the solution, then that is fine, but either way I am not sure how to go about it.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 04, 2007 1:21 am 
Regular
Regular

Joined: Mon Apr 02, 2007 3:54 am
Posts: 67
Location: Hyderabad
make a file with .properties extension
place ur all queries in this file. every query will be having some name, i can say that this will be like a key and value pair. name in left of = sign will be key and at right, query will be.
Now use object of java.util.Property class in ur java class and access the query with its key name using this object. In case of any further problem, let me know.

Thanks,
Gopal
[please rate, if helpful]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 04, 2007 9:34 am 
Beginner
Beginner

Joined: Wed Sep 21, 2005 12:02 am
Posts: 31
Thanks for the answer. How is this different than using the mapping (hbm) file? I am not sure it solves the problem I have.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 05, 2007 12:40 am 
Regular
Regular

Joined: Mon Apr 02, 2007 3:54 am
Posts: 67
Location: Hyderabad
try this first. I am already done.


Top
 Profile  
 
 Post subject: Re: Optional Parameters and SQL Query in Hibernate Mapping f
PostPosted: Thu Jul 05, 2007 9:55 am 
Newbie

Joined: Fri Mar 03, 2006 4:13 pm
Posts: 4
I've done optional parameters in a single SQL query before. I just used a part of the WHERE clause like this:
Code:
AND ( :parameter IS NULL OR c.column = :parameter )

The only problem was that Hibernate needed the type specified when null was passed. So I had to do this:
Code:
query.setParameter("parameter", parameter, Hibernate.LONG);

instead of the usual:
Code:
query.setParameter("parameter", parameter);

In my case I don't need to query for null, so I use it to indicate not to check for the value. If you do need to query for null as well as other values then you could probably use two parameters. The first to indicate that the second should be checked for.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 06, 2007 2:10 pm 
Newbie

Joined: Thu Nov 02, 2006 1:15 pm
Posts: 3
Lance,

I did not realize it could be done that way. Thank you! I may use that myself.


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.