简体   繁体   中英

How to alias “lo” to “oid” in Hibernate ORM?

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)]

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 eg "\\"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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM