22

I want to perform search for a particular string that is starting with a particular alphabet.

So, for example if the starting alphabet is 'A' then it should produce a result which will contain all the strings with alphabet 'A'.

How do I achieve this ?

My query is as shown below:

Query qry = session.createQuery("From RegistrationBean as rb where rb."+searchCriteria+"  like %?%");
qry.setString(0,searchField);
Roman C
  • 49,761
  • 33
  • 66
  • 176
Ni3
  • 349
  • 1
  • 4
  • 8

4 Answers4

31

Change your query to this:

Query qry = session.createQuery("From RegistrationBean as rb where rb."+searchCriteria+"  like ?");
qry.setString(0, "%"+searchField+"%");
Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
18

You can use criteria for using like

session = sessionFactory.openSession();
Criteria query = session.createCriteria(Pojo.class);
query.add(Restrictions.like("column", "a", MatchMode.START));

It will give you the list of string which start by alpha-bate 'a'.

MayurB
  • 3,609
  • 2
  • 21
  • 36
12

Change to

Query qry = session.createQuery("From RegistrationBean as rb where rb ";
if (searchField != null)
{
    qry = qry + " where rb.searchCriteria like :searchField ";
}
if ( searchField!= null)
{
    query.setString("searchField","%"+searchField+"%");
}
Lukasz Stelmach
  • 5,281
  • 4
  • 25
  • 29
rinuthomaz
  • 1,393
  • 2
  • 23
  • 38
10

This way

createQuery("from RegistrationBean as rb where rb.:searchCriteria like '%:searchField%'"); 
Roman C
  • 49,761
  • 33
  • 66
  • 176