Hello.
I have an XML I need to persist. The root element has several attributes (master values) and several children (detail rows), but those are in an encapsulating tag.
So, I need to save that into master-detail tables.
e.g.
Code:
<master attr1="val1" attr2="val2">
<children>
<detail dAttr1="dVal1" dAttr2="dVal2">
<detail dAttr1="dVal1" dAttr2="dVal2">
<detail dAttr1="dVal1" dAttr2="dVal2">
<detail dAttr1="dVal1" dAttr2="dVal2">
</children>
</master>
And I need to save that into two tables:
Master with fields: Id, attr1, attr2
Detail with fields: Id, dAttr1, dAttr2, fKey
Where fKey is the reference to its parent row in Master.
I created two mapping files, for master and detail. Problem is I not sure how to indicate in the master the relationship with Detail, so it will save both when parsing the XML. No mention the Children element in the middle.
I tried something like
Code:
<class entity-name="Master" node="master"
table="Master" catalog="MyDB">
<id name="Id" type="java.lang.Long">
<column name="Id" />
<generator class="identity" />
</id>
<property name="attr1" node="@attr1" type="string">
<column name="attr1" length="6" not-null="true" />
</property>
<property name="attr2" node="@attr2" type="string">
<column name="attr2" length="6" not-null="true" />
</property>
<component name="children" node="children">
<list name="detail" table="Something">
<key column="Id" unique="true"></key>
<list-index column="ID_IDX"></list-index>
<one-to-many entity-name="Detail"></one-to-many>
</list>
</component>
</class>
Code:
<class entity-name="Detail" node="detail"
table="Detail" catalog="MyDB">
<id name="Id" type="java.lang.Long">
<column name="Id" />
<generator class="identity" />
</id>
<property name="dAttr1" node="@dAttr1" type="string">
<column name="dAttr1" length="6" not-null="true" />
</property>
<property name="dAttr2" node="@dAttr2" type="string">
<column name="dAttr2" length="6" not-null="true" />
</property>
</class>
But when I do in Code
Code:
List list = document.selectNodes("//master");
Iterator iter = list.iterator();
while (iter.hasNext()) {
Object master= iter.next();
dom4jSession.save("Master", master);
}
It saves only to master, and nothing to detail.
Someone can help me identify what is wrong?
Thanks a lot.