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.
4 Answers
1
votes
0
votes
@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.
0
votes
0
votes
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);
@NotNull? - sp00m