I have a text field in my Hibernate entity as shown below:
@Column(nullable = false, columnDefinition = "TEXT")
private String content;
So, what is the default and suggested length for this field (it is a content of a blog)? And should I set it using length
as shown below?
@Column(nullable = false, columnDefinition = "TEXT", length = 4000)
private String content;
Note: My database is PostgreSQL.
2
Answers
As documented in the manual the data type
text
accepts no length limit and has a maximum size of 1GB.If you want a constraint to limit the length, use a check constraint on a
text
column or use thevarchar(4000)
.Max column length of text type is your RDBMS’s max length for text datatype, in PostgreSQL case is 1Gb. If you want to limit size use
@Column annotation indicates specific characteristics of the physical database column, and will be enforced by database (PostgreSQL checks and throws an error)
Or
That will adjust to the limit aforementioned, where length attribute may not work and won’t be necessary.