I want to save an object in data base.
I'm using MySQL data base, but when I try it I get an exception that says: "data is bigger than Column length".
How can I increase the length of my column using hibernate?

- 3,920
- 4
- 31
- 54

- 865
- 2
- 8
- 14
-
2Are you creating the DB from the Hibernate mappings? – madth3 Nov 14 '12 at 02:12
7 Answers
if your column is varchar
use annotation length
@Column(length = 255)
or use another column type
@Column(columnDefinition="TEXT")
updating answer based on comment:
@Lob

- 3,956
- 1
- 19
- 34
-
1
-
7`columnDefinition` is DB dependent what works in PostgreSQL may not work in MySQL, see this SO question: http://stackoverflow.com/questions/16414215/jpa-how-to-set-string-column-to-varcharmax-in-ddl – csharpfolk Jun 26 '16 at 11:06
@Column(name = Columns.COLUMN_NAME, columnDefinition = "NVARCHAR(MAX)")
max
indicates that the maximum storage size is 2^31-1 bytes (2 GB)

- 35,493
- 19
- 190
- 259

- 101
- 1
- 2
-
This works only under SQL Server, PostgreSQL don't have NVARCHAR(MAX). Instead use @Lob annotation as answered in: http://stackoverflow.com/questions/16414215/jpa-how-to-set-string-column-to-varcharmax-in-ddl to get variable character type of unlimited length – csharpfolk Jun 26 '16 at 11:05
You can use Length annotation for a column. By using it you can maximize or minimize column length. Length annotation only be used for Strings
.
@Column(name = "NAME", nullable = false, length = 50)
@Length(max = 50)
public String getName() {
return this.name;
}

- 35,493
- 19
- 190
- 259

- 6,244
- 5
- 35
- 42
Similarly to mychalvlcek's answer, you can use the length attribute for String values, however if you're wanting to set to VARCHAR(MAX) in the database, this solution worked for me in MS SQL:
@Column(length = Integer.MAX_VALUE)

- 101
- 5
You can use both of @Column(size = 50) and @Size(max = 50) annotation, but using @Size(max = 50) is better than @Column(size = 50), because in @Size annotation, Hibernate automatically triggers a validation before it inserts or updates the entity.
@Column(size = 50);
or
@Size(max = 50); //it's better because Hibernate check automatically validation
You need to alter your table. Increase the column width using a DDL statement.
please see here
http://dba-oracle.com/t_alter_table_modify_column_syntax_example.htm

- 396
- 2
- 13
-
2The command might be the same but is confusing that the page is for Oracle while the OP mentioned MySQL – madth3 Nov 14 '12 at 02:11