postgresql JPA 和 PostqreSQL:长字符串持久化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1870927/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JPA and PostqreSQL: long string persistence
提问by emanemos
Can anybody tell me how to persist long text using JPA (I use PostgreSQL)?
谁能告诉我如何使用 JPA 保留长文本(我使用 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.
但是,这会在数据库中生成一个charactervariing(255)类型的字段。
Furthermore, I've tried to use @Column annotation:
此外,我尝试使用 @Column 注释:
@Column(columnDefinition="TEXT")
private String body;
But everything in vain.
但一切都是徒劳的。
I would be gratefull for a helpfull comment on this problem.
如果您对这个问题有帮助,我将不胜感激。
回答by Jonathan Feinberg
@Column(columnDefinition="TEXT")
is indeed the way to do it. Perhaps you have to regenerate your DDL?
确实是这样做的方法。也许您必须重新生成 DDL?
回答by ezgi
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.
应该创建一个类型为“text”(PostgreSQL - 可变长度不限)的 db 列,但应该重新生成表。
回答by Indigenuity
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:
我的解决方案是类似的,但我发现我需要该LONGTEXT
列:
@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.
是的,你必须重新生成它。我正在使用播放!我不得不重新启动我的应用程序。
回答by Cassio Lemos
JPA, Postgresql You can use:
JPA、Postgresql 可以使用:
@Column(length = 2000)
private String template;
it generates:
它产生:
template character varying(2000)
回答by prefabSOFT
You can also use the validation annotations:
您还可以使用验证注释:
@Size(max=xxx)
That would also serve as a validation constraint of course.
当然,这也可以作为验证约束。