With the release of Hibernate 6, once again I am scratching my head trying to figure out how to get Hibernate to convert a @Lob to bytea for a PostGres database. I also need the code to be testable with H2 (or any in memory database).
I am using Postgres 10+ (v13), so that takes care of that requirement.
This is what I used before, which was tricky, because I also needed it to work with H2. (The PostGres dialect was only loaded in production, not tests.)
Sadly, getting bytea to work is "outside the scope" of the Hibernate 6 user guide. See 2.2.47. https://docs.jboss.org/hibernate/orm/6.0/userguide/html_single/Hibernate_User_Guide.html
The migration guide also seems silent on the topic. https://github.com/hibernate/hibernate-orm/blob/6.0/migration-guide.adoc
The entire API for SQLDialect family has been rewritten, so I am back to square 1.
public class CustomPostgresSQL10Dialect extends PostgreSQL10Dialect{
public CustomPostgresSQL10Dialect() {
super();
registerColumnType(Types.BLOB, "bytea");
}
@Override
public SqlTypeDescriptor remapSqlTypeDescriptor(SqlTypeDescriptor sqlTypeDescriptor) {
if (sqlTypeDescriptor.getSqlType() == java.sql.Types.BLOB) {
return BinaryTypeDescriptor.INSTANCE;
}
return super.remapSqlTypeDescriptor(sqlTypeDescriptor);
}
}