Spring Boot and JPA+Postges: Lob mapped to Text but integer is persisted

Viewed 1873

I created a simple Spring Boot application with JPA connecting to a Postgres database. I want to store large text files in the database and for that I am using the @Lob annotation on a String attribute, which JPA successfully maps to a Text type in Postgres. However, when I persist an entity to the database, the text contents are always saved as an unrelated integer. A varchar is persisted as expected. Please see the snippets below. What am I missing here?

(some lines omitted for simplification purposes)

SampleEntity.java:

@Entity
public class SampleEntity {
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Id
  private Long id;

  @Column
  private String varcharTest;

  @Lob
  private String textTest;
...
}

SampleRepository.java

@Repository
public interface SampleRepository extends JpaRepository<SampleEntity, Long> {
}

My application.yml:

spring:
  datasource:
    platform: postgres
    url: jdbc:postgresql://myserver:32432/postgres
    username: postgres
    password: <password>

Application.java:

@SpringBootApplication
public class Application {
    private static ApplicationContext ctx;

    public static void main(String[] args) throws Exception {
        ctx = SpringApplication.run(Application.class, args);
        SampleEntity se = new SampleEntity();
        se.setTextTest("abc1234");
        se.setVarcharTest("xyz5678");
        SampleRepository sampleRepo = ctx.getBean(SampleRepository.class);
        sampleRepo.save(se);

Database Select

2 Answers

CLOB datatype is currently unsupported in postgres database. You can use text datatype instead. To save data in string format just use @Column(columnDefinition = "text") annotation.

It's just an OID to the actual file that is stored outside the table. It's the default behavior for a @Lob field.

Related