store strings of arbitrary length in Postgresql

I would recommend skipping the '@Lob' annotation and use columnDefinition like this:

@Column(columnDefinition="TEXT")

see if that helps viewing the data while browsing the database itself.


Use the @LOB definition, it is correct. The table is storing an OID to the catalogs -> postegreSQL-> tables -> pg_largeobject table.

The binary data is stored here efficiently and JPA will correctly get the data out and store it for you with this as an implementation detail.


Old question, but here is what I found when I encountered this: http://www.solewing.org/blog/2015/08/hibernate-postgresql-and-lob-string/

Relevant parts below.

    @Entity
    @Table(name = "note")
    @Access(AccessType.FIELD)
    class NoteEntity {

      @Id
      private Long id;

      @Lob
      @Column(name = "note_text")
      private String noteText;

      public NoteEntity() { }

      public NoteEntity(String noteText) { this.noteText = noteText }
    }

The Hibernate PostgreSQL9Dialect stores @Lob String attribute values by explicitly creating a large object instance, and then storing the UID of the object in the column associated with attribute.

Obviously, the text of our notes isn’t really in the column. So where is it? The answer is that Hibernate explicitly created a large object for each note, and stored the UID of the object in the column. If we use some PostgreSQL large object functions, we can retrieve the text itself.

Use this to query:

SELECT id, 
  convert_from(loread(
      lo_open(note_text::int, x'40000'::int), x'40000'::int), 'UTF-8') 
  AS note_text
FROM note