Skip to content

NullableCharacterType prevents Hibernate from escaping backslash properly #715

@samnyan

Description

@samnyan

Describe the bug
When using hypersistence-utils-hibernate-63:3.7.4 with hibernate-core:6.4.4.Final and MySQL
like query always failed because of the backslash hadling in escape

JDBC exception executing SQL [select p1_0.id,p1_0.label from permission p1_0 where p1_0.label like ? escape '\' limit ?,?] [Parameter index out of range (2 > number of parameters, which is 1).] [n/a]

The reason is in Hibernate 6 will add escape '\' after the like clause, and the backslash should be double backslash in sql string.
When not using hypersistence-utils, the output sql is correct:

Hibernate: select p1_0.id,p1_0.label from permission p1_0 where p1_0.label like ? escape '\\' limit ?,?

But adding hypersistence-utils will cause the above error.

Because in org.hibernate.sql.ast.spi.AbstractSqlAstTranslator.renderLiteral(), by default it will use org.hibernate.type.descriptor.jdbc.internal.JdbcLiteralFormatterCharacterData to append the sql which using org.hibernate.dialect.MySQLDialect.appendLiteral() method. This can handle backslash in sql correctly.

But when adding hypersistence-utils,
The JdbcMapping type became io.hypersistence.utils.hibernate.type.basic.NullableCharacterType,
and it will use org.hibernate.type.internal.UserTypeSqlTypeAdapter to append the sql instead.
Then it calls io.hypersistence.utils.hibernate.type.ImmutableType.toSqlLiteral() which using String.format(Locale.ROOT, "'%s'", o) inside.
The result is only one backslash in the sql string. (Breaking the following sql statement prepare)

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions