Default for the Escape Character in MySQL

The default escape character in MySQL is a backslash, but Hibernate 6 sets it to empty (which might be the proper value according to the SQL specification, but not for MySQL). Is it possible to make it at least configurable?

The current implementation of the MySQLSqlAstTranslator adds "escape ‘’ " if the escape character is not set explicitly (hibernate-orm/hibernate-core/src/main/java/org/hibernate/dialect/MySQLSqlAstTranslator.java at main · hibernate/hibernate-orm · GitHub)

After migrating to Hibernate 6 we need to add " escape '' " to every like query. It would be great if the default is a backslash or if the escape character can be configured.

I know a possible solution would be to override the MySQLDialect, but in my opinion that would just be a workaround (the default MySQLDialect should work with a default MySQL database).

That is the only solution to override this behavior across the entire application. Hibernate overrides the default escape character to ensure portability across all supported databases that have different defaults. You can always explicitly write like '...' escape '\' in specific queries to use backslash as an escape.

1 Like