Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set default values using hibernate annotations in MYSQL

Tags:

java

hibernate

I have searched on internet and seen ideas about setting default values in entity class using hibernate annotations in mysql and I have done with setting default values to column with datatype integer only as follows.

@Column(name = "COLUMN_NAME", insertable=false, updatable = false, nullable = false, columnDefinition = "int default 1")
protected Integer test_id;

and is working fine.

But I haven't seen any example for varchar/string datatype and I have try with different types in columnDefinition like,

columnDefinition = "TEXT default `TEST`"  
columnDefinition = "TEXT default TEST"  
columnDefinition = "varchar(255) default `15-JUL-1980`")  
columnDefinition = "varchar(255) default 15-JUL-1980")  
columnDefinition = "varchar default 15-JUL-1980")  
columnDefinition = "varchar default `15-JUL-1980`")  
columnDefinition = "CHAR(100) default `15JUL1980`")  
columnDefinition = "CHAR default `15JUL1980`")

With length attribute:-

`length=100, columnDefinition = "CHAR default `15JUL1980`")` 

etc.

but in this case table is not created by hibernate, that may be due to wrong syntax. But if I am creating the table with only integer values as above table is created.

like image 883
Tulshiram Pawde Avatar asked Oct 26 '25 22:10

Tulshiram Pawde


1 Answers

Below line of code is working fine

@Column(name = "cname", insertable=false, updatable = false, nullable = false, columnDefinition = "varchar(255) default '15-JUL-1980'") 
like image 190
Tulshiram Pawde Avatar answered Oct 28 '25 14:10

Tulshiram Pawde



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!