How to alias "lo" to "oid" in Hibernate ORM?

OrangeDog :

In Postgres 10, I'm using the lo module for my large object fields. This creates a custom type lo that's a simple alias to oid. However, I cannot work out how to let Hibernate 5.3 know this.

CREATE EXTENSION lo;
CREATE TABLE foo (
    bigserial id PRIMARY KEY,
    bar lo
);
@lombok.Data
@javax.persistence.Entity
public class Foo {
    @javax.persistence.Id
    private long id;

    @javax.persistence.Lob
    private java.sql.Blob bar;
}

org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: wrong column type encountered in column [bar] in table [foo]; found [lo (Types#DISTINCT)], but expecting [oid (Types#BLOB)]

OrangeDog :

You can do it with a custom dialect, but this then requires manually specifying it instead of allowing autodetection.

package com.example

public class CustomDialect extends PostgreSQL95Dialect {
    public CustomDialect () {
        this.registerColumnType(Types.BLOB, "lo");
    }
}
hibernate.dialect=com.example.CustomDialect

This gets more complicated if the type is schema-qualified instead of relying on the search path. You will need to register e.g. "\"public\".\"lo\""


You can use a custom DialectResolver to only replace detected postgres dialects, but if you want to support multiple versions you're going to need lots of classes: CustomPostgreSQL95Dialect, CustomPostgreSQL94Dialect, etc.

package com.example

public class CustomDialectResolver extends BasicDialectResolver {
    public CustomDialectResolver() {
        super("PostgreSQL", CustomDialect.class);
    }
}
hibernate.dialect_resolvers=com.example.CustomDialectResolver

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=83210&siteId=1