Hi,
maybe it was asked before, but I can't find it.
My problem is, that I have 2 tables which are linked by a foreign key. Nothing special on it. Both tables have auto_increment enabled. The mapping files are generated by Middlegen (MiddlegenIDE and MyEclipse 3.0). But when I populate my model objects and try to save it, I get an error that the foreign key column is null. I thought that Hibernate will populate the value automatically ?
I'm using MySQL 4 and Hibernate 3.
Here are the mapping files:
-- table calendar --
<class
name="model.Calendar"
table="calendar"
>
<id
name="id"
type="java.lang.Integer"
column="id"
>
<generator class="native" />
</id>
<!-- Associations -->
<!-- bi-directional one-to-many association to Event -->
<set
name="events"
lazy="true"
inverse="true"
cascade="all"
>
<key>
<column name="calendar_id" />
</key>
<one-to-many
class="model.Event"
/>
</set>
-- table event (with the foreign to calendar)--
<class
name="model.Event"
table="event"
>
<id
name="id"
type="java.lang.Integer"
column="id"
>
<generator class="native" />
</id>
<!-- Associations -->
<!-- bi-directional many-to-one association to Calendar -->
<many-to-one
name="calendar"
class="model.Calendar"
not-null="true"
>
<column name="calendar_id" />
</many-to-one>
-- hibernate output --
DEBUG - could not insert: [model.Event] [insert into event ...
java.sql.SQLException: Column 'calendar_id' cannot be null
-- class CalendarService.java --
public void save (Calendar calendar) {
HibernateUtil.getSession().save(calendar) ;
}
Thanks for any help
Lothar
|