store strings of arbitrary length in Postgresql

元气小坏坏 提交于 2019-11-29 06:20:33
Kris

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!