org.postgresql.util.PSQLException: 错误: 类型字符变化的值太长 (255)

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

org.postgresql.util.PSQLException: ERROR: value too long for type character varying(255)

javapostgresqlhibernatespring-bootspring-data-jpa

提问by Albin Gjoka

When I execute my project, I get this error: enter image description here

当我执行我的项目时,我收到此错误: 在此处输入图片说明

The goal is to save jsontext into a database using hibernate.

目标是json使用hibernate将文本保存到数据库中。

Users.java& UsersBooks.javais likewise,

Users.java&UsersBooks.java同样是,

enter image description here

在此处输入图片说明

Books.java:

书籍.java:

@Entity
@Table(name="tblbooks")
public class Books {


@Id
@Column(name = "bookshareId")
private int bookshareId;

@Column(name="author")
private String author;

@Column(name = "availableToDownload")
private int availableToDownload;

@Column(name = "briefSynopsis")
private String briefSynopsis;

@Column(name="category")
private String category;

@Column(name = "completeSynopsis")
private String completeSynopsis;

@Column(name = "contentId")
private int contentId;

@Column(name = "copyright")
private Date copyright;

@Column(name="downloadFormat")
private String downloadFormat;

@Column(name="dtbookSize")
private int dtbookSize;

@Column(name = "freelyAvailable")
private int freelyAvailable;

@Column(name = "brf")
private int brf;

@Column(name = "daisy")
private int daisy;

@Column(name = "images")
private int images;

@Column(name = "isbn13")
private String isbn13;

@Column(name="language")
private String language;

@Column(name = "publishDate")
private Date publishDate;

@Column(name = "publisher")
private String publisher;

@Column(name = "quality")
private String quality;

@Column(name = "title")
private String title;

@OneToMany(mappedBy="book")
private List<UsersBooks> usersBooks;



//Getters & Setters

回答by v.ladynev

You try to save a string value more than 255 chars length. Just increase a column length

您尝试保存长度超过 255 个字符的字符串值。只需增加一列长度

@Column(name = "xxx", length = 1024)

you need to alter a column length in the database too.

您也需要更改数据库中的列长度。

When you use

当你使用

@Column(name = "xxx")

Hibernate uses a default column length.

Hibernate 使用默认的列长度。

You can use @Lobfor a really large text data.

您可以@Lob用于非常大的文本数据。

Please, use xxx_usersin place of tblusers.

请用xxx_users代替tblusers

Use Userin place of Users.

使用User到位Users

Use CascadeType.ALLon the @OneToManypart of the association.

使用CascadeType.ALL@OneToMany协会的一部分。

Use a lazy loading on the @ManyToOnepart of the association.

@ManyToOne在关联方面使用延迟加载。

@ManyToOne(fetch = FetchType.Lazy)
pravate User user;

回答by veben

For String with more than 255 chars length you can increase column length :

对于长度超过 255 个字符的字符串,您可以增加列长度:

@Column(length = 2048)
private String column;

For large size :

对于大尺寸:

@Lob
private String column;

For unlimited size :

对于无限大小:

@Column(columnDefinition="text")
private String column;

回答by Arnaud Denoyelle

The error message tells that you are trying to store a String which is too large for its destination column (255).

该错误消息表明您正在尝试存储一个对于其目标列 (255) 而言太大的字符串。

You can either :

你可以:

  • Increase the column size or
  • Change the column type to TEXTinstead of VARCHAR(255).
  • 增加列大小或
  • 将列类型更改为TEXT而不是VARCHAR(255)