Size of a Java String + Hibernate

4

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

If I map my class and not limit the field, hibernate automatically creates a 255-character limit. I could give a larger limit, but ideally the field would have no bounds.

Does anyone know how to help me?

    
asked by anonymous 19.09.2014 / 22:24

1 answer

5

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

One way to do this is by using the @Column annotation JPA, specifying the length attribute. 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 type VARCHAR to 255, others in 2000 or 4000 characters.

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

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

Note: The @Basic(fetch=LAZY) annotation serves to read this field from the database only when the getDescription() method is called. This prevents unnecessary reading of large amounts of data.

    
19.09.2014 / 22:52