Hi,
I am working on a project which involves translating a set of DTD's into hibernate mappings, then using the mappings to create DB2 tables and load in data from some large XML files.
I was inspired to use hibernate after learning that you could go straight from XML->DB without the need for POJOs. This tutorial helped:
http://javaboutique.internet.com/tutorials/mapping/
So far, everything has been working well. I am able to map simple XML elements and attributes without a problem.
However, I am now up against a problem that I thought would be simple. Namely - how do you map a PCDATA element which occurs multiple times in a parent element?
On other words, I would like to make this:
Code:
<Address primary="true">
<AddressLine>6126</AddressLine>
<AddressLine>Crossroads Lane</AddressLine>
<AddressLine>fifth line</AddressLine>
<City>Los Angeles</City>
<State>California</State>
<ZipCode>91608</ZipCode>
<Country>US</Country>
</Address>
into these tables:
ADDRESS (one)
-----------------
id
City
State
ZipCode
Country
ADDRLINE table (many)
-----------------
id
AddressLine
ADDRESS_ID (points back to "id" within Address table)
I have tried the following in my mapping:
Code:
<class entity-name="Address" node="Address" table="WORKHEA.ADDRESS">
<id name="id" type="int" column="id" length="6">
<generator class="native"/>
</id>
<property name="primary" column="PRIMADDRESS" node="@primary" type="boolean" length="5"/>
<set name="AddressLine" table="WORKHEA.ADDRLINE" inverse="true">
<key column="ADDRESS_ID"/>
<one-to-many entity-name="AddressLine"
embed-xml="false"
node="AddressLine"/>
</set>
<property name="City" column="CITY" node="City" type="string" length="128"/>
<property name="State" column="STATE" node="State" type="string" length="128"/>
<property name="ZipCode" column="ZIPCODE" node="ZipCode" type="string" length="40"/>
<property name="Country" column="COUNTRY" node="Country" type="string" length="128"/>
</class>
This creates the tables. However nothing is inserted into the ADDRLINE table.
This was my other attempt:
Code:
<class entity-name="Address" node="Address" table="WORKHEA.ADDRESS">
<id name="id" type="int" column="id" length="6">
<generator class="native"/>
</id>
<property name="primary" column="PRIMADDRESS" node="@primary" type="boolean" length="5"/>
<property name="City" column="CITY" node="City" type="string" length="128"/>
<property name="State" column="STATE" node="State" type="string" length="128"/>
<property name="ZipCode" column="ZIPCODE" node="ZipCode" type="string" length="40"/>
<property name="Country" column="COUNTRY" node="Country" type="string" length="128"/>
</class>
<class entity-name="AddressLine" node="AddressLine" table="WORKHEA.ADDRLINE">
<id name="id" type="int" column="id" length="6">
<generator class="native"/>
</id>
<property name="AddressLine" column="ADDRLINE" node="AddressLine" type="string" length="50"/>
<many-to-one name="Address"
column="ADDRESS_ID"
node="Address"
entity-name="Address"/>
</class>
Here, the tables are created. The correct amount of ADDRLINE records are created (corresponding to the XML doc), however they contain nulls in the ADDRLINE and ADDRESS_ID fields.
Is there anyone who can help me to do a simple many-to-one (or is it one-to-many?) relationship without the use of POJO's?
thanks,
Heather Buch