Hibernate version:
2.1.6
Mapping documents:
<class name="Box" >
<!--Key mapping -->
<key column= "KEY_BOX" />
<property name="name" type="string">
<bag name="messages" lazy="true" cascade="all-delete-orphan" inverse="false" >
<!-- unidirection -->
<key foreign-key="BOX_MESSAGE">
<column name="KEY_BOX" not-null="true"/>
</key>
<one-to-many class="Message"/>
</bag>
</class>
<class name="Message" >
<key column= "KEY_MESSAGE" />
<property name="text" type="string">
</class>
I need a confirmation, is it true that with a unidirection association, the above mapping will always cause a ora-1400.
caused by: java.sql.BatchUpdateException: ORA-01400: cannot insert NULL into ("XXXX"."MESSAGE"."KEY_BOX")
example codes:
Session session = null;
session = HUtil.currentSession();
Transaction tx = null;
tx = session.beginTransaction();
Box box = (Box)session.load(Box.class, new Long(1));
Message m = new Message();
m.setText("asdasd");
box.getMessages().add(m);
tx.commit();
And conclusion is that I must set
<key foreign-key="BOX_MESSAGE">
<column name="KEY_BOX" not-null="false"/>
</key>
which means unidirection association cannot be mandatory.
comments ?
/Kwan
|