store strings of arbitrary length in Postgresql

杀马特。学长 韩版系。学妹 提交于 2019-12-18 04:43:11

问题


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 I've annotated the field with @Lob:

public class MyEntity{

    @NotNull
    @Size(min = 2)
    @Lob
    private String message;

    ...
}

The application works ok in localhost but I've deployed it to an external server and it a problem with encoding has appeared. For that reason I'd like to check if the data stored in the PostgreSQL database is ok or not. The application creates/updates the tables automatically. And for that field (message) it has created a column of type:

text NOT NULL

The problem is that after storing data if I browse the table or just do a SELECT of that column I can't see the text but numbers. Those numbers seems to be identifiers to "somewhere" where that information is stored.

Can anyone tell me exactly what are these identifiers and if there is any way of being able to see the stored data in a @Lob columm from a pgAdmin or a select clause?

Is there any better way to store Strings of arbitrary length in JPA?

Thanks.


回答1:


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.




回答2:


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.




回答3:


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


来源:https://stackoverflow.com/questions/9158645/store-strings-of-arbitrary-length-in-postgresql

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