How to Use Lob Datatype in PostgreSQL

1. Problem

Here's the typical code we use when creating a Lob column.

Column definition

@NotNull
@Lob
@Column(name = "message")
private String message;

But when we create our tables we would encounter the error below, because PostgreSQL doesn't support lob.

Caused by: org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: wrong column type encountered in column [message] in table [comm_messages]; found [text (Types#VARCHAR)], but expecting [oid (Types#CLOB)]
    at org.hibernate.tool.schema.internal.AbstractSchemaValidator.validateColumnType(AbstractSchemaValidator.java:167)

2. Solution

To resolve this problem we can implement several solutions.

2.1 Set the column to TEXT in the table. With this approach, we need to manually edit the entity where the column is defined.

@NotNull
@Lob
@Column(name = "message", columnDefinition = "TEXT")
private String message;

2.2 If you are using Spring, you can set the non_contextual_creation to true in the application property file.

spring:
  jpa:
    properties:
      hibernate:
        jdbc:
          lob:
            non_contextual_creation: true

0 Comments

Post a Comment

Post a Comment (0)

Previous Post Next Post
NERV Open Source

Building production Spring Boot systems?

Explore NERV — open-source Java libraries for audit trails, persistence, exception handling, and reliable event-driven architecture.

Explore NERV on GitHub →