saeed arash
saeed arash

Reputation: 935

how to set length of an column in hibernate with maximum length

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?

Upvotes: 47

Views: 145898

Answers (7)

Majid Asgharizadeh
Majid Asgharizadeh

Reputation: 1

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

Upvotes: 0

mychalvlcek
mychalvlcek

Reputation: 4046

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

Upvotes: 108

pancakesmaplesyrup
pancakesmaplesyrup

Reputation: 101

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)

Upvotes: 0

bhdrkn
bhdrkn

Reputation: 6712

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;
}

Upvotes: 9

user3090074
user3090074

Reputation: 101

@Column(name = Columns.COLUMN_NAME, columnDefinition = "NVARCHAR(MAX)")

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

Upvotes: 9

Subba
Subba

Reputation: 406

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

Upvotes: -1

Fritz
Fritz

Reputation: 10055

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

Upvotes: 1

Related Questions