Reputation: 4609
I want to set default value of integer value in my table using hibernate annotations.It works but when i give default value as 1 it always set it to zero in the database.I don't know what i am doing wrong.Any help will be appreciated
This is what i am using
@Column(name = "interval",nullable = false, columnDefinition = “int default 1")
private int interval;
I have checked almost all the links i dont know when i am setting the value to be 1 then why 0 is getting stored in the database
I know we can do it as
@Column(name = "interval",nullable = false, columnDefinition = “int default 1")
private int interval = 1;
but i dont want to set the value as above I want to know to any way to set the default value using hibernate
I had already seen these links How to set default value in Hibernate Set default values using hibernate annotations in MYSQL
Upvotes: 4
Views: 4931
Reputation: 9162
That's because the annotation describes the column, not the data.
Hibernate does not know if the primitive value was changed by user or stayed the same by default.
In java the primitive integer is by default initialized to 0. If you want the interval to be 1 by default, you need something like this.
@Column(name = "interval",nullable = false, columnDefinition = “int default 1")
private int interval = 1;
or another way is to declare interval to be Integer:
@Column(name = "interval",nullable = false, columnDefinition = “int default 1")
private Integer interval;
Upvotes: 2