-->
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 a Restriction.like criteria on a numeric property
PostPosted: Thu May 27, 2010 4:20 pm 
Newbie

Joined: Thu May 27, 2010 4:10 pm
Posts: 2
I need to create a criteria using a like restriction on a numeric column.
Users need to filter a transactions list, and sometimes all they know is the last 4 digits of its id.

I created the DetachedCriteria and tried a few approaches with no luck

Code:
// (...)
DetachedCriteria c = DetachedCriteria.forClass(getDomainClass());;

c.add(Restrictions.like("transactionId", 42));
// or
c.add(Restrictions.like("transactionId", "%42%"));
// or
c.add(Restrictions.like("str(transactionId)", "%42%"));
// (...)
List l = getHibernateTemplate().findByCriteria(c);
// (...)

Any idea how I can achieve that using Criteria (not HQL)

Thanks for your time

_________________
Juan Manuel Formoso
http://twitter.com/juanformoso


Top
 Profile  
 
 Post subject: Re: Using a Restriction.like criteria on a numeric property
PostPosted: Thu May 27, 2010 4:41 pm 
Newbie

Joined: Mon May 10, 2010 1:41 pm
Posts: 15
I've never done this with hibernate, just an idea....

can you use an expression in the restriction?

Simply take the modulus of the numeric field against the search string length and compare the values (both numeric). [Edit it should read 10^(power of the search string length), read between the lines =P, same goes for later]
This will find if the string matches at the end.
Then add an "or" condition with criteria to use integer division to truncate the end of the fields string and compare, to search the start.

If it is possible it isn't beautiful but it is more likely that people want the start or end and not the middle.


Top
 Profile  
 
 Post subject: Re: Using a Restriction.like criteria on a numeric property
PostPosted: Thu May 27, 2010 4:59 pm 
Newbie

Joined: Thu May 27, 2010 4:10 pm
Posts: 2
Thanks Quaternion, but that's too complex. Besides, I could need to match in the middle.

I temporarily fixed it using a sqlRestriction:

Code:
c.add(Restrictions.sqlRestriction("transactionId like '%42%'"));

I don't really like this solution, please let me know if there's a better way to do it

_________________
Juan Manuel Formoso
http://twitter.com/juanformoso


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.