18

Can anybody tell me how to persist long text using JPA (I use PostgreSQL)?

Here's the way I defined a very long string in my class:

@Lob
private String  body;

However, this produces a field of type charactervarying(255) in the database.

Furthermore, I've tried to use @Column annotation:

@Column(columnDefinition="TEXT")
private String  body;

But everything in vain.

I would be gratefull for a helpfull comment on this problem.

emanemos
  • 265
  • 2
  • 6
  • 14

6 Answers6

20
@Column(columnDefinition="TEXT")

is indeed the way to do it. Perhaps you have to regenerate your DDL?

Jonathan Feinberg
  • 44,698
  • 7
  • 80
  • 103
  • Unfortunately, it seems that @Column annotation is completely ignored. – emanemos Dec 09 '09 at 10:40
  • Dear Jonathan, I tired the above suggested fix but I am getting error. Your help will be apreciable. Please help. http://stackoverflow.com/questions/25094410/hibernate-abstractmethoderror-setcharacterstreamiljava-io-readerjv?noredirect=1#comment39048566_25094410 – Ashish Agarwal Aug 02 '14 at 12:52
17

I had the same case today. Just

@Lob
private String  body;

should create a db column of typ "text" (PostgreSQL - variable unlimited length) but the table should be regenerated.

ezgi
  • 376
  • 2
  • 3
12

This is a very old post, but since it has lots of views, and I had a similar question even today, I guess it's worth a simple answer.

My solution was similar, but I found that I needed the LONGTEXT column instead:

@Column(columnDefinition="LONGTEXT")
private String body;

And yes, you do have to regenerate it. I'm using Play! and I had to restart my app.

Indigenuity
  • 9,332
  • 6
  • 39
  • 68
1

JPA, Postgresql You can use:

@Column(length = 2000)
    private String template;

it generates:

template character varying(2000)
0

You can also use the validation annotations:

@Size(max=xxx)

That would also serve as a validation constraint of course.

prefabSOFT
  • 1,143
  • 1
  • 12
  • 23
0

I dont know why, but I added @Column(length = 1024)

on top of the getter and it works.

MHK
  • 63
  • 1
  • 8