Mohammad Faisal
Mohammad Faisal

Reputation: 5959

hibernate MaterializedClobType storing a long object instead of text

I have an entity class which has a field declared as:

    @NotNull
    @Lob
    private String xmlString;

Now I have to maintain the compatibility with both Oracle and PostgreSQL. The respective column definition in DDL are xmlString clob and xmlString text. The application works well and there in only one issue with PostgreSQL that the value stored in column xmlString is of type long object instead of the original xml string.
As suggested here to add @Type(type="org.hibernate.type.MaterializedClobType") which provides compatibility with both Oracle and PostgreSQL but even after adding this annotation the value stored in PostgreSQL is long object instead of the original xml string.

The changes I made:

    @NotNull
    @Lob
    @org.hibernate.annotations.Type(type = "org.hibernate.type.MaterializedClobType")
    private String xmlMessage;

and the value stored in xmlString is a number which seems to be the reference id of long object.

Is there any possibility to make this work on both Oracle and PostgreSQL?


It works well in PostgreSQL if I change the annotation to @org.hibernate.annotations.Type(type = "org.hibernate.type.TextType") but then there is schema validation failure on Oracle.

Upvotes: 1

Views: 1402

Answers (1)

Mohammad Faisal
Mohammad Faisal

Reputation: 5959

In order to solve this problem I had created a custom dialect by extending the org.hibernate.dialect.PostgreSQL9Dialect and overriding the remapSqlTypeDescriptor as follows:

    @Override
    public SqlTypeDescriptor remapSqlTypeDescriptor(SqlTypeDescriptor sqlTypeDescriptor)
    {
        if (sqlTypeDescriptor.getSqlType() == Types.CLOB)
        {
            log.debug("remapping CLOB to LongVarchar");
            return LongVarcharTypeDescriptor.INSTANCE;
        }
        return super.remapSqlTypeDescriptor(sqlTypeDescriptor);
    }

With this I do not need to annotate my entity field by

@org.hibernate.annotations.Type(type = "org.hibernate.type.MaterializedClobType")

Upvotes: 3

Related Questions