store strings of arbitrary length in Postgresql

前端 未结 3 1040
悲&欢浪女
悲&欢浪女 2020-12-17 17:12

I have a Spring application which uses JPA (Hibernate) initially created with Spring Roo. I need to store Strings with arbitrary length, so for that reason

相关标签:
3条回答
  • 2020-12-17 18:00

    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
    
    0 讨论(0)
  • 2020-12-17 18:10

    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.

    0 讨论(0)
  • 2020-12-17 18:14

    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.

    0 讨论(0)
提交回复
热议问题