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
org.postgresql.util.PSQLException: ERROR: value too long for type character varying(255)
提问by Albin Gjoka
When I execute my project, I get this error:
The goal is to save json
text into a database using hibernate.
目标是json
使用hibernate将文本保存到数据库中。
Users.java
& UsersBooks.java
is likewise,
Users.java
&UsersBooks.java
同样是,
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 @Lob
for a really large text data.
您可以@Lob
用于非常大的文本数据。
Please, use xxx_users
in place of tblusers
.
请用xxx_users
代替tblusers
。
Use User
in place of Users
.
使用User
到位Users
。
Use CascadeType.ALL
on the @OneToMany
part of the association.
使用CascadeType.ALL
在@OneToMany
协会的一部分。
Use a lazy loading on the @ManyToOne
part 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
TEXT
instead ofVARCHAR(255)
.
- 增加列大小或
- 将列类型更改为
TEXT
而不是VARCHAR(255)
。