I am using Hibernate 3.x's new ability to persist XML represented data.
I am enhancing existing code that currently works with POJOs to be
able to accept and persist XML represented data. Referencial
integrity (RI) has recently been turned on on our Sybase DB.
In other words, foreign key constraints are now being enforced.
I have a simple association between a warehouse table and a docks
table. Each has their respective primary keys, and the docks table
contains a foreign key to the warehouse. So, a warehouse can have
zero or more docks. I've modeled this as a set/one-to-many in the
Warehouse mapping below and a corresponding many-to-one in the
Docks mapping.
In the case of persisting POJO represented data, persisting with RI on the
DB works with no problems. When I save a new Warehouse POJO that has
a Set of two new Dock POJOs, all are saved to the DB and all keys are
properly set. Hibernate appears to be doing three insert statements,
one for each of the three POJOs. The mappings used with the POJOs are
only slightly different from the ones shown below for the XML represented
data. They have a class tag rather than the entity-name and node tags,
but all else is the same.
In the case of persisting XML represented data, I'm getting a foreign
key constraint error from the DB. The mappings below are the ones
I started with, taken from the POJO mappings and modified for XML.
The XML input data with one new Warehouse entity and two new
Dock entities is also shown below. I've ran tests with RI off. When
Hibernate persists the XML data, it is doing inserts for all three
entities, then it is doing an update on each of the two Dock records,
updating the foreign key to the Warehouse entity. When RI is turned on
this fails because when the insert of the first Dock entity occurs, the
foreign key to the Warehouse is null, and the foreign key constraint
error is produced by the DB.
Is there a feature in the new Hibernate 3.x XML data persist handling
that I'm missing. Why does Hibernate appear to be doing only inserts
in the POJO case and inserts/updates in the XML case. I've ran multiple
tests with many variations on my mappings, and still haven't been
successful. Any help would be appreciated.
Thanks!
Hibernate version: 3.0.5
Mapping documents:
Code:
<hibernate-mapping>
<class entity-name="Warehouse"
table="t-warehouse"
node="Warehouse"
dynamic-update="false"
dynamic-insert="false">
<id name="warehouseId" column="warehouseId" type="string">
<generator class="uuid.hex"/>
</id>
property name="location" node="location"
column="location" type="java.lang.String"/>
<set name="docks" node="docks"
inverse="true" lazy="false" cascade="all" sort="unsorted">
<key column="warehouseId"/>
<one-to-many entity-name="Dock" node="dock"/>
</set>
</class>
</hibernate-mapping>
Code:
<hibernate-mapping>
<class entity-name="Dock" table="t-dock" node="dock">
<id name="dockId" column="dockId" type="string">
<generator class="uuid.hex"/>
</id>
<many-to-one name="warehouseId" entity-name="Warehouse"
node="." column="warehouseId" insert="true"
update="true" cascade="none" not-null="true"/>
property name="dockLoadType" node="dockLoadType"
column="dockLoadType" type="java.lang.String"/>
</class>
</hibernate-mapping>
Input XML:Code:
<?xml version="1.0 encoding="UTF-8"?>
<Warehouse xmlns="http://project/external"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<location>West Street</location>
<docks>
<dock>
<dockLoadType>Heavy</dockLoadType>
</dock>
<dock>
<dockLoadType>Medium</dockLoadType>
</dock>
</docks>
</Warehouse>