-->
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: Can I make QBE ignore empty strings?
PostPosted: Tue Mar 22, 2005 12:00 pm 
Newbie

Joined: Sat Mar 12, 2005 12:01 pm
Posts: 8
Hibernate version:2.18
Struts 1.1

It appears that the using the struts <html:text/> tag will treat null values as empty strings. So, if a user does not enter a value for a given field, instead of this field being null, it gets set to "".

So, when I try to do something like this

...

First Name: <html:text property="person.firstName"/>
First Name: <html:text property="person.lastName"/>
...

Example personExample = Example.create(Candidate.class)
.enableLike(MatchMode.START)
.ignoreCase()
.excludeZeroes()
.create(sForm.getPerson());

The query that is generated contains ALL properties, not just ones that have had "real" values set.

Is there a way to make QBE ignore empty strings?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 22, 2005 12:03 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Sure, write a PropertySelector


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 22, 2005 5:55 pm 
Newbie

Joined: Sat Mar 12, 2005 12:01 pm
Posts: 8
<embarrassed>Works like a charm.</embarrassed>

Thanks,
Dave


Top
 Profile  
 
 Post subject: Example needs excludeBlankStrings method
PostPosted: Fri Sep 09, 2005 11:56 am 
Newbie

Joined: Tue Apr 19, 2005 5:05 pm
Posts: 3
dcracauer wrote:
<embarrassed>Works like a charm.</embarrassed>

Thanks,
Dave

I don't think you should be too embarassed. I had the same question and the answer isn't very obvious to someone new to Hibernate. It seems like there should be a easy switch like excludeBlankStrings that would do this for all of your String properties. And unless you have a bit of understanding of how the Example class works, you may not realize that there can be only one property selector (from what I can tell). So if you write

Code:
Example.create(myExample)
                         .ignoreCase()
                         .excludeZeroes()
                         .setPropertySelector(mySelector);


You're probably not going to get what you expected unless you added the logic from excludeZeroes into your selector.

Please correct me if I'm wrong.

Anyway, here's what I came up with for my situation:

Code:
static final class
NotNullOrBlankStringPropertySelector
implements Example.PropertySelector
{
    public boolean include(Object object, String propertyName, Type type)
    {
            return object!=null && (
                    !(object instanceof String)
                     || StringUtils.isNotBlank((String)object)
             );
    }
}


It would be nice if there was some way to chain these together so I could say

Code:
Example.create(myExample)
                         .ignoreCase()
                         .ignoreZeroes()
                         .ignoreBlankStrings();



But that's just me :-)


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.