-->
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: hibernate criteria
PostPosted: Wed Dec 19, 2007 3:57 pm 
Newbie

Joined: Thu May 24, 2007 3:01 am
Posts: 11
Hi,

I am using Hibernate criteria for search purposes. I have an option to search the objects using its id.(primary key). This id search should be like search and not exact search. i.e If i give 1, then all objects having key start with 1 should be displayed. In my pojo this id is Long. (if i concat % to this key and add it in expression , i got error)
So how can i do this?

Thank you,
Regards,
K.Siva


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 20, 2007 9:13 pm 
Newbie

Joined: Mon Oct 29, 2007 2:33 pm
Posts: 16
Location: Cambridge, MA
In SQL it would be, for numbers starting with 1:
select id from table t
where cast(t.id as char(10)) like '1%'

so I guess the Criteria expression would be:

.add (Restrictions.sqlRestriction( "cast((alias).ID as char(10)) like '?%', firstNum, Hibernate.LONG);

I'm cribbing from JPwH, pg. 669 here.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 21, 2007 1:09 am 
Newbie

Joined: Thu May 24, 2007 3:01 am
Posts: 11
hi,

thanks for your coding. But i have some issues over it.

how to give alias name. Now iam hard coded as this_.id. Is it ok?
If i use that code then i got error like invalid parameter 2.

I coded like this and its working.

add(Restrictions.sqlRestriction("(this_.ID) like '"+ aVO.getId()+"%'"));


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 21, 2007 1:28 pm 
Beginner
Beginner

Joined: Wed May 16, 2007 7:12 am
Posts: 41
Location: London
Code:
session().createCriteria(Blahblah.class)
.add(Restrictions.like("id", "%"+strId+"%"))

to ignorecase , use ilike

_________________
Dinesh Mahadevan

Visit me at http://www.yelani.com/


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 24, 2007 2:20 am 
Newbie

Joined: Thu May 24, 2007 3:01 am
Posts: 11
hi dinesh,

Since the id is long in the pojo class, if you do like this it will throw exception. (may be class cast)

Thanks for your reply.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 24, 2007 6:58 am 
Beginner
Beginner

Joined: Wed May 16, 2007 7:12 am
Posts: 41
Location: London
hmmm.. I didn't see you mention that..the other example will work, but don't try it in production quality code, or a large table.

_________________
Dinesh Mahadevan

Visit me at http://www.yelani.com/


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