-->
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.  [ 2 posts ] 
Author Message
 Post subject: Hibernate-Search: How can I create a search query?
PostPosted: Tue Apr 20, 2010 5:01 am 
Newbie

Joined: Sun Apr 11, 2010 7:22 am
Posts: 4
I have a web app build with Hibernate. In one page, I have one submit button with 5 inputs

* field1
* field2
* field3
* field4
* field5

When all the inputs are null, I make a simple querry

Code:
Query query = session.createQuery("from MyTable");


but when I have at least one input not null, I must create the search query. Let's say, the field1 is not null:

Code:
Query query = session.createQuery("from MyTable where field1= :field1");
query.setParameter("field1", field1);


But I need to check every single input and I need to create the query String based on this thing. It's really a pain in the neck for 2 fields, but for 5...

What is the smartest, easiest way to create the search query in this situation?


Top
 Profile  
 
 Post subject: Re: Hibernate-Search: How can I create a search query?
PostPosted: Tue Apr 20, 2010 5:11 am 
Regular
Regular

Joined: Wed Mar 10, 2010 4:48 am
Posts: 106
You should use LIKE with wildcards.
If a field is empty, it will match to an empty string. Every row will match to an empty string.

For example:
Code:
Query query = session.createQuery("from MyTable where field1 LIKE '%input1%'");


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