'Use regex query with hibernate in Spring
I am able to fire query in query browser (my sql) and able to retrieve record like below SELECT * FROM details WHERE title REGEXP 'dem';
but when I use similar type of code in server side then I is giving error as unexpected token: REGEXP
After searching on web, it seems that hibernate does not support REGEXP. but some solution are also given which are not relevant to that. I need to handle this through query only in hibernate.
sbQuery.append("select distinct id from deatil id");
sbQuery.append("where ((id.title like :keyword) OR (id.description like :keyword) ");
Solution 1:[1]
You could add your own custom function in the Hibernate dialect for your database.
I tried adding a custom function that was a bit more complex than this and I had issues to make it work, hopefully the simpler ones should work out of the box.
Solution 2:[2]
Query query = session.createSQLQuery("select distinct id from deatil id where (id.title like :keyword) OR (id.description like :keyword)")
.setParameter("keyword", keyword);
List result = query.list();
Missing ")" and must set parameter value.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Community |
| Solution 2 | Jens |
