-->
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: NullObject (SpecialCase) pattern for Criterion
PostPosted: Thu Jun 01, 2006 6:40 am 
Newbie

Joined: Tue Nov 22, 2005 10:08 am
Posts: 17
Hi,

I need 'special case' org.hibernate.criterion.Criterion implementation that will NOT evaluate to any sql expression and will NOT create additional AND entry (or other) in output SQL.

The reason of this is that I want to get rid of all the conditional logic with assembling Criteria. e.g:

Code:
if (lastName != null) {
  userProfileCriteria.add(Restrictions.eq("lastName", lastName);
}


In future we can create a Criterion hierarchy that may check if given value is null, and if so, don't evaluate to any sql fragment.

For now, 'special case' Criterion is enough since you can arrange your code and get rid of null checking conditional logic, creating 'special case' criterion when is needed.

Or perhaps there is already something similar in Hibernate?

Thanks,
Szczepan


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 05, 2006 5:56 am 
Regular
Regular

Joined: Tue May 16, 2006 3:32 am
Posts: 117
You could have a look at Example queries.

http://www.hibernate.org/hib_docs/v3/re ... a-examples

By default it uses only the not-null properties.

e.g.

UserProfle userProfile = new UserProfile();
userProfile.setLastName(lastName);
.
.
userProfile.set...


userProfileCriteria.add(Example.create(userProfile));

However, if ALL the properties in userProfile object are null then you would get an extra condition "and (1=1)" in the output SQL, otherwise only the not-null properties are included.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 05, 2006 8:09 am 
Newbie

Joined: Tue Nov 22, 2005 10:08 am
Posts: 17
Nice workaround :)))

Still, I want to avoid the (1=1) entry. I've allready created similar solution (stubbing Criterion with 1=1 implementation) but I don't like it.

I am thinking about a patch but I can't get to mailing list yet...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 05, 2006 11:27 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
what is wrong with "(1=1)" ?

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 05, 2006 11:47 am 
Newbie

Joined: Tue Nov 22, 2005 10:08 am
Posts: 17
max wrote:
what is wrong with "(1=1)" ?


1. Are you sure that every jdbc vendor provides implementation smart enough to remove supid clause fragment prior to execution of a query (performance concern)? Some database engines may even throw an exception (or have a mode that acts this way) when query fragment is unnecessary.

2. Looks awful in logs, just unprofessional.

3. If I create IgnorableCriterion that evaluates to 1=1, the class that creates where clause adds 'and' joins automatically. So the output may be like: 1=1 and 1=1 and 1=1 and 1=1 ....

The question is what's the big deal to change it? It's just adding Special Case Criterion that evaluates to nothing and single type check in class that creates where clause.

Not mentioning more robust solutions -> refactoring Restrictions from static class to genuine object with setIgnoreNulls() method or refactoring to hierarchy. That will dramatically reduce amount of conditional logic in every piece of code that uses criteria API. Delibrate null comparisons are very rare in search criteria implementations.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 05, 2006 12:13 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
1. don't use non-smart vendors ;)

2. well, maybe.

3. ...

..no don't do type checking for this. should be some polymorphic/behavioral thing.

Patches can be added to our jira, but please add a testcase to show its purpose.

_________________
Max
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.  [ 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.