简体   繁体   中英

default value in hibernate4

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.

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;

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

@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 Eg, 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM