user783160
user783160

Reputation: 175

org.hibernate.exception.ConstraintViolationException

i am making a sample program using this link i have following tables in sqlserver:

`CREATE TABLE Student2(
STUDENT_ID int NOT NULL,
LastName varchar(255) NOT NULL,
STUDENT_NAME varchar(255),
STUDENT_ADDRESS int,
UNIQUE (STUDENT_ADDRESS))

CREATE TABLE Address2(
ADDRESS_ID int NOT NULL,
LastName varchar(255) NOT NULL,
ADDRESS_STREET varchar(255),
ADDRESS_CITY varchar(255),
ADDRESS_STATE varchar(255),
ADDRESS_ZIPCODE varchar(255))

i have take studentId type int in Student.hbm.xml:

<id name="studentId" type="int" column="STUDENT_ID">
    <generator class="native" />
</id>

i have take addressId type int in Address.hbm.xml:

<id name="addressId" type="int" column="ADDRESS_ID">
    <generator class="native" />
</id>

i am facing following exception:

org.hibernate.exception.ConstraintViolationException: could not insert: [com.vaannila.student.Address]`
exception:`org.hibernate.exception.ConstraintViolationException: could not insert: [com.vaannila.student.Address]
Caused by: java.sql.SQLException: Cannot insert the value NULL into column 'ADDRESS_ID', table 'sample.dbo.Address2'; column does not allow nulls. INSERT fails.

please help.

Upvotes: 0

Views: 2798

Answers (1)

cproinger
cproinger

Reputation: 2288

your tables do not have an auto-assigned primary-key. in fact your create tables do not define a primary key at all.

Heres an example for MySql to create a table with a primary key which can be used with a native-generator configuration:

create table asdf ( pk integer primary key auto_increment )

you can drop your tables and let hibernate create them as well.

Upvotes: 1

Related Questions