Reputation: 1575
I have Object class which is mapped to a oracle database table. in this class, one column has a default value in database. I used this: @Column(name = "FK_STATUS" ,nullable = false, columnDefinition = "int default 1") the column type in database is Number. but I get "can not insert null in FK_status" error. please help.
Upvotes: 0
Views: 354
Reputation: 4222
The columnDefinition will work
columnDefinition = "int default 1" (should be "default 1")
but you have to recreate DB tables first. It will set your column to set null values as int 1
Well, you should recreate DB via
persistence.xml
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
or do it manually
update {TABLE}
set FK_STATUS = 1
where status is null;
alter table {TABLE} modify (FK_STATUS default 1 NOT NULL);
Upvotes: 0
Reputation: 190
I would like to do small correction in your Hibernate-Mapping Column code , please see below
@Column(name = "FK_STATUS" ,nullable = false, columnDefinition = "int(10) default 1")
Used this and let me know , If this is not working with your code
Upvotes: 0
Reputation: 407
@NGS, The constraints such as unique, nullable, default etc.. are the keywords which the JPA/Hibernate will use during the table creation. It doesn't have any significance after table creation. For E.g., If you create a table first with a column as non-unique, and if you are planning to control the uniqueness of the column by providing annotation, will not work.
All those kind of metadata are provided for the table creation.
Upvotes: 0
Reputation: 121998
As i didn't see any syntax errors there in your line,Not much aware of H-3 as i'm using H-3.
This could be a fix,Not exactly solution for your problem.
Use a default value to your variable
.
private Integer fkStatus= 1;
Upvotes: 1