hi,
i wanna insert an object (Car in example) whose foreign key object (Company in example) may not exist. But i don't know how i should design mapping document. In other words Company may not exists, but i want to save cars into db.
Hibernate version: 3.1
Mapping documents:
Code:
Company.hbm
...
<bag name="carList" cascade="none" inverse="false" lazy="true">
<key column="`nCompanyId`"/>
<one-to-many class="Car"/>
</bag>
...
Car.hbm
...
<many-to-one name="company" column="`nCompanyId`"/>
...
Code between sessionFactory.openSession() and session.close():Code:
t = session.beginTransaction();
session.save(car);
t.commit();
Full stack trace of any exception that occurs:org.hibernate.exception.ConstraintViolationException: could not insert: [object.Car]
....
Caused by: java.sql.SQLException: Cannot insert the value NULL into column 'nCompanyId', table 'mydb.dbo.Car'; column does not allow nulls. INSERT fails.
.....
Name and version of the database you are using: MS SQL Server 2000 SP4
The generated SQL (show_sql=true):Code:
insert into Car ( [nCompanyId], [dAmount], [sDetail] ) values (?, ?, ?)
// values are (0, 0.0, 'anything') respectively