Java JPA:如何将 String 持久化到数据库字段中,键入 MYSQL Text

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3868096/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 06:04:27  来源:igfitidea点击:

JPA: how do I persist a String into a database field, type MYSQL Text

javamysqlormjpa

提问by Thang Pham

The requirement is that the user can write an article, therefore I choose type Textfor the contentfield inside mysql database. How can I convert Java Stringinto MySQL Text

要求是用户可以写文章,所以我选择mysql数据库里面Textcontent字段类型。我怎样才能转换Java StringMySQL Text

Here you go Jim Tough

干得好 Jim Tough

@Entity
public class Article implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private Long userId;

    private String title;

    private String content;

    private Integer vote;

    //Constructors, setters, getters, equals and hashcode
}

In my MYSQL database, contentis type Text. I was hoping that there would be something like this java.sql.Text, since java.sql.Blobis an actual type, but sadly, that does not exist

在我的 MYSQL 数据库中,content是 type Text。我希望会有这样的东西java.sql.Text,因为它java.sql.Blob是一种实际类型,但遗憾的是,它不存在

采纳答案by Pascal Thivent

Since you're using JPA, use the Lobannotation (and optionally the Columnannotation). Here is what the JPA specification says about it:

由于您使用的是 JPA,因此请使用Lob注释(以及可选的Column注释)。这是 JPA 规范对此的说明:

9.1.19 Lob Annotation

A Lobannotation specifies that a persistent property or field should be persisted as a large object to a database-supported large object type. Portable applications should use the Lobannotation when mapping to a database Lob type. The Lob annotation may be used in conjunction with the Basicannotation. A Lob may be either a binary or character type. The Lob type is inferred from the type of the persistent field or property, and except for string and character-based types defaults to Blob.

9.1.19 Lob 注释

Lob注释指定持久性属性或字段应保持为一个大型对象到数据库支持的大对象类型。Lob当映射到数据库 Lob 类型时,便携式应用程序应该使用 注释。Lob 注释可以与Basic注释结合使用 。Lob 可以是二进制或字符类型。Lob 类型是从持久字段或属性的类型推断出来的,除了字符串和基于字符的类型之外,默认为 Blob。

So declare something like this:

所以声明如下:

@Lob 
@Column(name="CONTENT", length=512)
private String content;

References

参考

  • JPA 1.0 specification:
    • Section 9.1.19 "Lob Annotation"
  • JPA 1.0 规范:
    • 第 9.1.19 节“Lob 注释”

回答by Reitffunk

With @LobI always end up with a LONGTEXTin MySQL.

随着@Lob我总是最后一个LONGTEXT在MySQL。

To get TEXTI declare it that way (JPA 2.0):

为了让TEXT我这样声明(JPA 2.0):

@Column(columnDefinition = "TEXT")
private String text

Find this better, because I can directly choose which Text-Type the column will have in database.

找到这个更好,因为我可以直接选择列在数据库中将具有的文本类型。

For columnDefinitionit is also good to read this.

因为columnDefinition阅读这篇文章也很好。

EDIT: Please pay attention to Adam Siemions commentand check the database engine you are using, before applying columnDefinition = "TEXT".

编辑:请注意 Adam Siemions 的评论并在应用columnDefinition = "TEXT".

回答by Maxpan

for mysql 'text':

对于 mysql '文本':

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

for mysql 'longtext':

对于 mysql 'longtext':

@Lob
private String description;