Basically, to use the MySQL regexp function in Hibernate, we need to create an "SQLFunctionTemplate".
Now how to do it:
First: create a class called "AppMySQLDialect" and continue from MySQLDialect, then override the empty constructor and finally register the regexp function:
public class AppMySQLDialect extends MySQLDialect { public AppMySQLDialect() { super(); registerFunction("regexp", new SQLFunctionTemplate(Hibernate.INTEGER, "?1 REGEXP ?2")); } }
Ok, now you can use it as follows:
FROM Entity E WHERE regexp(E.string2evaluate, '\d') = 1
Create your HibernateQuery and execute.
source share