-->
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: Named queries with nullable columns/values
PostPosted: Fri Aug 12, 2005 2:32 am 
Beginner
Beginner

Joined: Wed Aug 10, 2005 12:09 am
Posts: 30
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:
3.0.3

How to use named queries with potential nullable columns/values?
e.g.

<query ...
and a.mycol = :mycolval
</query>

At runtime there are some possibilities:
1. mycolval != null
2. mycolval == null
3. mycolval == (empty string)

for 1. the named query works fine
for 2. (and possibly 3), the named query is wrong (s/be a.mycol is null)

Is their any elegant solution using named queries?
Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 12, 2005 2:45 am 
Expert
Expert

Joined: Thu Sep 04, 2003 8:23 am
Posts: 368
try

<query ...
and (a.mycol is null or a.mycol = :mycolval)
</query>

_________________
Seb
(Please don't forget to give credits if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 13, 2005 3:53 am 
Beginner
Beginner

Joined: Wed Aug 10, 2005 12:09 am
Posts: 30
thanks for the reply - but I need matching values in this case (I only want null's if mycolval == null).

I would use Criteria as follows:

if(obj == null) criteria.add(Restrictions.isNull(mycol));
else criteria.add(Restrictions.eg(mycol, obj));

???

however I can't figure out using this criteria with an aggregate select

eg

'select max(t.num) from table t' --> + above Criteria


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 14, 2005 1:52 pm 
Newbie

Joined: Wed Jan 05, 2005 12:30 pm
Posts: 17
Location: Philadelphia, PA
I use named queries often and they're great. But they're not for everything.

This is one such case. Stick with the criteria.


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.