JPA:如何将字符串持久化到数据库字段中,输入MYSQL文本。

时间:2021-07-08 23:09:40

The requirement is that the user can write an article, therefore I choose type Text for the content field inside mysql database. How can I convert Java String into MySQL Text

需求是用户可以写文章,因此我选择了mysql数据库内的内容字段的类型文本。如何将Java字符串转换成MySQL文本?

Here you go 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, content is type Text. I was hoping that there would be something like this java.sql.Text, since java.sql.Blob is an actual type, but sadly, that does not exist

在我的MYSQL数据库中,内容是类型文本。我希望有这样的java.sql。文本,因为java.sql。Blob是实际类型,但遗憾的是,它不存在。

3 个解决方案

#1


100  

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

由于您使用的是JPA,请使用Lob注释(以及列注释)。以下是JPA规范所描述的:

9.1.19 Lob Annotation

A Lob annotation 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 Lob annotation when mapping to a database Lob type. The Lob annotation may be used in conjunction with the Basic annotation. 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.

Lob注释指定持久的属性或字段应该作为大型对象持久化到数据库支持的大型对象类型中。当映射到数据库Lob类型时,可移植应用程序应该使用Lob注释。Lob注释可以与基本注释一起使用。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"
    • 部分9.1.19“Lob注释”
  • JPA 1.0规范:9.1.19“Lob注释”

#2


57  

With @Lob I always end up with a LONGTEXTin MySQL.

使用@Lob,我总是会得到一个LONGTEXTin MySQL。

To get TEXT I declare it that way (JPA 2.0):

为了得到文本,我这样声明(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 columnDefinition it is also good to read this.

对于columnDefinition,读这个也很好。

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

编辑:请注意Adam Siemions的评论,并在应用columnDefinition = "TEXT"之前检查你正在使用的数据库引擎。

#3


9  

for mysql 'text':

mysql“文本”:

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

for mysql 'longtext':

mysql“量变”:

@Lob
private String description;

#1


100  

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

由于您使用的是JPA,请使用Lob注释(以及列注释)。以下是JPA规范所描述的:

9.1.19 Lob Annotation

A Lob annotation 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 Lob annotation when mapping to a database Lob type. The Lob annotation may be used in conjunction with the Basic annotation. 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.

Lob注释指定持久的属性或字段应该作为大型对象持久化到数据库支持的大型对象类型中。当映射到数据库Lob类型时,可移植应用程序应该使用Lob注释。Lob注释可以与基本注释一起使用。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"
    • 部分9.1.19“Lob注释”
  • JPA 1.0规范:9.1.19“Lob注释”

#2


57  

With @Lob I always end up with a LONGTEXTin MySQL.

使用@Lob,我总是会得到一个LONGTEXTin MySQL。

To get TEXT I declare it that way (JPA 2.0):

为了得到文本,我这样声明(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 columnDefinition it is also good to read this.

对于columnDefinition,读这个也很好。

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

编辑:请注意Adam Siemions的评论,并在应用columnDefinition = "TEXT"之前检查你正在使用的数据库引擎。

#3


9  

for mysql 'text':

mysql“文本”:

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

for mysql 'longtext':

mysql“量变”:

@Lob
private String description;