42

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?

Paco Abato
  • 3,920
  • 4
  • 31
  • 54
saeed arash
  • 865
  • 2
  • 8
  • 14

7 Answers7

99

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
mychalvlcek
  • 3,956
  • 1
  • 19
  • 34
  • 1
    `type not found or user lacks privilege: TEXT` – xedo Mar 31 '15 at 09:03
  • 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
9
@Column(name = Columns.COLUMN_NAME, columnDefinition = "NVARCHAR(MAX)")

max indicates that the maximum storage size is 2^31-1 bytes (2 GB)

naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
user3090074
  • 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
7

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;
}
naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
bhdrkn
  • 6,244
  • 5
  • 35
  • 42
0

Use @Column annotation and define its length attribute. Check Hibernate's documentation for references (section 2.2.2.3).

Fritz
  • 9,987
  • 4
  • 30
  • 49
0

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)
0

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
-1

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
Subba
  • 396
  • 2
  • 13
  • 2
    The 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