-->
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.  [ 7 posts ] 
Author Message
 Post subject: Invalid generated SQL for Restriction.in()
PostPosted: Thu May 18, 2006 6:38 am 
Newbie

Joined: Thu May 18, 2006 6:25 am
Posts: 4
It seems that the SQL generated by Hibernate in case of using
criteria.add(Restriction.in("id", collection));
to filter data, can not be executed by databases if the collection is NULL or EMPTY.

This happens because the generated SQL looks like:
select * from USERS where id in ()

and this is for sure not supported by SQL syntax.

Does anybody have any idea how could I change the code to have the same functionality (select must not return any record)?

For this test I used:
Hibernate 3.0.5
MySQL 4.0.12


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 18, 2006 6:41 am 
Regular
Regular

Joined: Wed Aug 25, 2004 6:23 am
Posts: 91
That's kind of a weird requirement, but how about:

Restrictions.eq(Integer.valueOf(1), Integer.valueOf(0)),


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 18, 2006 7:44 am 
Newbie

Joined: Thu May 18, 2006 6:25 am
Posts: 4
Yes, may be this sample looks very stupid because I wanted to emphasize the generation of the SQL.

But in reality, I used that piece of code to write a general filtering mechanism that applies for any kind of entity/class.
And in that case I don't know the TYPE of the attribute that I filter. The only thing I know is the name of the attribute (member of Hibernate class) as string and the vaue to compare with (in case of IN operation is a collection).

For my special scenario with IN operation I tried to create a collection with some dummy values but don't know what objects to put in (what type and what values).
That means I have to generate an object of a type which for sure does not fulfill my condition. Eg: in the sample provided by you if I have an user with id=1 that user will be returned, but I don't want to get back any ser.
select * from user where id in () -- I expect to return no records


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 23, 2006 9:49 am 
Newbie

Joined: Wed May 17, 2006 12:07 pm
Posts: 4
I met the same problem in my program.
I wanted to use Restrictions.in to filter my result set.

crit.add(Restrictions.in( "study", studies ));

But when the studies parameter is empty, a SQL exception throws out.
Since I don't know when the "studies" is null or not null. I have to add additional logic to check wether "studies" is null, if it is null then return null as well. Do you think it is a ok way to do that?


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 23, 2006 2:25 pm 
Senior
Senior

Joined: Wed Aug 17, 2005 12:56 pm
Posts: 136
Location: Erie, PA (USA)
calin,

You said have a general filtering mechanism so how about:
Code:
if ((collection == null) || (collection.size() == 0)) {
    // Null or empty collection, so no results should be returned.
    crit.add( Restrictions.sqlRestriction( "0 = 1" ) );
} else {
    crit.add( Restrictions.in( "propName", collection ) );
}


That way, you don't need to know the object type of the property.

Good luck,
Curtis ...

_________________
---- Don't forget to rate! ----


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 24, 2006 7:29 am 
Newbie

Joined: Thu May 18, 2006 6:25 am
Posts: 4
Thank you for your suggestion. It's really a good idea.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 24, 2006 8:11 am 
Regular
Regular

Joined: Thu Sep 22, 2005 1:53 pm
Posts: 88
Location: Rio de Janeiro
But to be honest I think it would be better if hibernate would resolve this for us.....

_________________
DonĀ“t forget to rate!


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