-->
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.  [ 3 posts ] 
Author Message
 Post subject: Using IN with empty set causes error
PostPosted: Sun Jan 04, 2009 8:06 pm 
Beginner
Beginner

Joined: Thu Feb 21, 2008 3:31 pm
Posts: 34
Hi all,

The query I am using takes a set as an argument which is why I'm using the IN method. My query works fine when the values passed in contain at least one entry, but if the list has no entries in it (even if it is initialized), it throws an exception.

I am using MySQL so perhaps I'm used to the way it handles IN() which may be different for Oracle or MS SQL.

Is this a hibernate bug or improper SQL on my part?

Code:
FROM Book book INNER JOIN book.authors author WHERE author IN(:searchAuthors) AND book.name LIKE :bookName


searchAuthors is guaranteed to be non-null, but is not guaranteed to be non-empty

Thanks,
Walter


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 05, 2009 4:34 am 
Regular
Regular

Joined: Wed Oct 15, 2008 6:59 am
Posts: 103
Location: Chennai
U must want to specify some content in IN clause without any null/not empty.

u may use if condition to check whether the Set contains some thing, if it has some authors names create the query and store it in string. otherwise remove the condition and put it in string. finally run that query.

_________________
If u feel it will help you, don't forget to rate me....


Top
 Profile  
 
 Post subject: HQL Query
PostPosted: Mon Jan 05, 2009 9:48 am 
Beginner
Beginner

Joined: Thu Feb 21, 2008 3:31 pm
Posts: 34
Hi,

Thanks for your reply.

Similar to the MySQL way of handling IN, you don't always need to specify arguments. If you don't, it simply ignores the IN() and selects everything. I already had a quick workaround, but I'm sure someone else will have this question and if the database handles this empty IN() properly, then Hibernate should as well.

If you end up handling queries internally, then what is the point of using Hibernate? Using a pre-defined list of statements and queries reduces error through less code. If my query had several IN()'s and I had to check for each case, that could get out of hand very quickly. Likewise, because this code is only written in this application, the behavior is not available in other applications unless you duplicate the code or more unlikely, create a small sub-project specifically for this behavior.

Your application should not have boilerplate code in it.

I'll file a bug for it and let the experts decide what should happen.


Thanks,
Walter


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