Size of a Java + Hibernate String

Asked

Viewed 3,184 times

4

Is there any way to persist a string with no size limit for the bank using Hibernate?

If I map my class and don’t limit the field, Hibernate automatically creates a limit of 255 characters. I could draw the line higher, but the ideal would be for the field to be limitless.

Someone out there knows how to help me?

1 answer

5


The default size of text fields is 255 characters, but you can switch to more as well as less.

One way to do this is by using the annotation @Column JPA, specifying the attribute length. Example:

@Column(name="DESC", nullable=false, length=512)
private String description;

However, text fields will always have a limit, but not from Hibernate, but from the database itself.

Some databases limit fields of the type VARCHAR in 255, others in 2000 or 4000 characters.

To store larger text, some databases support types such as TEXT or CLOB. In these cases, you can use the annotation @Lob from JPA. Example:

@Lob @Basic(fetch=LAZY)
@Column(name="REPORT")
private String report;

Remark: the annotation @Basic(fetch=LAZY) serves that this field is read from the bank only when the method getDescription() is called. This avoids unnecessary reading of large amounts of data.

  • 1

    Thanks @utluiz, clarified all my doubts!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.