“I want to store Large Objects (LOBs) in a PostgreSQL database using the @Lob
annotation in a JPA Entity, but it’s creating an OID column. How can I store @Lob
as a text column in the database?”
You can achieve this by using the following approaches:
- Using Native Database Definition
@Column(columnDefination="text")
- Using custom Dialect for Spring Boot version <= 2.6.x
package com.decimaltech.adminportal.dialect;
import org.hibernate.dialect.PostgreSQL10Dialect;
import org.springframework.stereotype.Component;
import java.sql.Types;
@Component
public class CustomPostgreSQLDialect extends PostgreSQL10Dialect {
@Override
public SqlTypeDescriptor remapSqlTypeDescriptor(SqlTypeDescriptor sqlTypeDescriptor) {
if (sqlTypeDescriptor.getSqlType() == Types.BLOB) {
return LongVarcharTypeDescriptor.INSTANCE;
}
if (sqlTypeDescriptor.getSqlType() == Types.CLOB) {
return LongVarcharTypeDescriptor.INSTANCE;
}
return super.remapSqlTypeDescriptor(sqlTypeDescriptor);
}
}
- Using custom Dialect for Spring Boot version > 2.6.x
package com.decimaltech.adminportal.dialect;
import org.hibernate.dialect.PostgreSQL10Dialect;
import org.springframework.stereotype.Component;
import java.sql.Types;
@Component
public class CustomPostgreSQLDialect extends PostgreSQL10Dialect {
public CustomPostgreSQLDialect() {
super();
registerColumnType(Types.CLOB, "text");
registerColumnType(Types.BLOB, "text");
}
}
Now add below properties:
spring.jpa.properties.hibernate.dialect = <your_package_name>.CustomPostgreSQLDialect```