开发者

java.sql.SQLException: Cannot insert the value NULL into column

开发者 https://www.devze.com 2023-03-22 06:24 出处:网络
i am making a sample program by using this tutorial. http://www.vaannila.com/hibernate/hibernate-example/hibernate-mapping-many-to-one-1.html

i am making a sample program by using this tutorial. http://www.vaannila.com/hibernate/hibernate-example/hibernate-mapping-many-to-one-1.html

i am facing error:

Caused by: java.sql.SQLException: 
Cannot insert the value NULL into column 'ADDRESS_ID', 
                                       table 'sample.dbo.Address'; 
column does not allow nulls. INSERT fails.

at the line:

session.save(student1);

in Main.java

in Address and Student tables, i used ADDRESS_ID, STUDENT_ID is of type int.

and in sample program it was l开发者_开发问答ong. so i changed now to int type in corrosponding classes field's and in hbm files.

now it is giving the same error:

Caused by: java.sql.SQLException: Cannot insert the value NULL into column 'ADDRESS_ID', table 'sample.dbo.Address'; column does not allow nulls. INSERT fails.

Any Help would be appreciated! Thanks


make sure the address_id contains a valid number (pointing to the address table entry for that student)


You have to alter the ADDRESS_ID column in the database to accept null values.


HEY DUDE AS MUCH AS I KNOW ABOUT HIBERNATE AND AFTER SEEING THAT EXAMPLE AND MAPPING FILE I COME WITH ONLY ONE CONCLUSION THAT U R SETTING ADDRESS COLUMN TO NOT-NULL=TRUE; THAT'S Y ITS NOT ALLOWING YOU TO INSERT NULL. AND ONE MORE THING ABOUT HIBERNATE IS THAT IT WILL ALLOW YOU TO ADD ONLY THOSE foreign KEY WHICH ARE PRESENT AS PRIMARY KEY IN RELATED TABLE.

IF NULL IS NOT A PRIMARY KEY THEN IT NOT GONNA ALLOW YOU TO ADD FOREIGN KEY OF VALUE NULL.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号