0

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.

2
  • Do you have any validation annotation over your Java field, such as @NotNull? Commented Jul 29, 2013 at 7:21
  • No I did not use @notNull or anyother validation annotation Commented Jul 29, 2013 at 7:27

4 Answers 4

1

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;
Sign up to request clarification or add additional context in comments.

2 Comments

this is true but not a default value handled in database. when I change the default value in database, I have to change the source code
@NGS True , but @Column(name = "FK_STATUS" ,nullable = false, columnDefinition = "int default 1") also in source code write ?
0

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

1 Comment

I don't want to change the structure, I have made my table and a field of it has a default value generated by DB. I am going to set value in it using hibernate, but "can not insert null value" error appear.
0

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

1 Comment

Please provide us your entity class code and the stack trace of your exception , you are getting.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.