Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wild card characters in hibernate

Tags:

hibernate

how to add a wildcard characters in hibernate?

I have try to do like this but already have a errors

"from Employee where epf like"+Epf+"%";

Epf is a int parameter pass to the query

like image 279
Tharaka Avatar asked Dec 21 '22 05:12

Tharaka


1 Answers

You missing quotes '...'. The end-query should look like:

SELECT Employee WHERE epf LIKE 'text%'

so changing your code to

"from Employee where epf like '"+Epf+"%'";

should do the trick. Note: open and close single quotes '...' here '"+Epf+"%'

But you approach is not good. Adding text like this to a query is dangerous. Consider this for more information:

  • http://en.wikipedia.org/wiki/SQL_injection

It is much safer to use bind parameters:

session.createQuery("from Employee where epf like :epf")
       .setParameter("epf", epf + "%")
       .list();
like image 154
Boris Brodski Avatar answered May 27 '23 17:05

Boris Brodski