Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

The goal is to save json text into a database using hibernate.

Users.java & UsersBooks.java is likewise,

enter image description here

Books.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
like image 528
Albin Gjoka Avatar asked Apr 06 '16 08:04

Albin Gjoka


3 Answers

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

@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.

You can use @Lob for a really large text data.

Please, use xxx_users in place of tblusers.

Use User in place of Users.

Use CascadeType.ALL on the @OneToMany part of the association.

Use a lazy loading on the @ManyToOne part of the association.

@ManyToOne(fetch = FetchType.Lazy)
pravate User user;
like image 115
v.ladynev Avatar answered Nov 16 '22 22:11

v.ladynev


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

@Column(length = 2048)
private String column;

For large size :

@Lob
private String column;

For unlimited size :

@Column(columnDefinition="text")
private String column;
like image 22
veben Avatar answered Nov 16 '22 22:11

veben


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

You can either :

  • Increase the column size or
  • Change the column type to TEXT instead of VARCHAR(255).
like image 1
Arnaud Denoyelle Avatar answered Nov 16 '22 22:11

Arnaud Denoyelle