emanemos
emanemos

Reputation: 275

JPA and PostqreSQL: long string persistence

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.

Upvotes: 19

Views: 24104

Answers (6)

MHK
MHK

Reputation: 71

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

on top of the getter and it works.

Upvotes: 0

Cassio Lemos
Cassio Lemos

Reputation: 61

JPA, Postgresql You can use:

@Column(length = 2000)
    private String template;

it generates:

template character varying(2000)

Upvotes: 1

prefabSOFT
prefabSOFT

Reputation: 1163

You can also use the validation annotations:

@Size(max=xxx)

That would also serve as a validation constraint of course.

Upvotes: 0

ezgi
ezgi

Reputation: 376

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.

Upvotes: 17

Indigenuity
Indigenuity

Reputation: 9740

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.

Upvotes: 12

Jonathan Feinberg
Jonathan Feinberg

Reputation: 45354

@Column(columnDefinition="TEXT")

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

Upvotes: 21

Related Questions