XML Mapping
Note that this is an experimental feature in Hibernate 3.0 and is under
extremely active development.
Working with XML data
Hibernate lets you work with persistent XML data in much the same way
you work with persistent POJOs. A parsed XML tree can be thought of
as just another way to represent the relational data at the object level,
instead of POJOs.
Hibernate supports dom4j as API for manipulating XML trees. You can write
queries that retrieve dom4j trees from the database and have any
modification you make to the tree automatically synchronized to the
database. You can even take an XML document, parse it using dom4j, and
write it to the database with any of Hibernate's basic operations:
persist(), saveOrUpdate(), merge(), delete(), replicate()
(merging is not yet supported).
This feature has many applications including data import/export,
externalization of entity data via JMS or SOAP and XSLT-based reporting.
A single mapping may be used to simultaneously map properties of a class
and nodes of an XML document to the database, or, if there is no class to map,
it may be used to map just the XML.
Specifying XML and class mapping together
Here is an example of mapping a POJO and XML simultaneously:
...
]]>
Specifying only an XML mapping
Here is an example where there is no POJO class:
...
]]>
This mapping allows you to access the data as a dom4j tree, or as a graph of
property name/value pairs (java Maps). The property names
are purely logical constructs that may be referred to in HQL queries.
XML mapping metadata
Many Hibernate mapping elements accept the node attribute.
This let's you specify the name of an XML attribute or element that holds the
property or entity data. The format of the node attribute
must be one of the following:
"element-name" - map to the named XML element
"@attribute-name" - map to the named XML attribute
"." - map to the parent element
"element-name/@attribute-name" -
map to the named attribute of the named element
For collections and single valued associations, there is an additional
embed-xml attribute. If embed-xml="true",
the default, the XML tree for the associated entity (or collection of value type)
will be embedded directly in the XML tree for the entity that owns the association.
Otherwise, if embed-xml="false", then only the referenced
identifier value will appear in the XML for single point associations and
collections will simply not appear at all.
You should be careful not to leave embed-xml="true" for
too many associations, since XML does not deal well with circularity!
...
]]>
in this case, we have decided to embed the collection of account ids, but not
the actual account data. The following HQL query:
Would return datasets such as this:
987632567
985612323
Gavin
A
King
...
]]>
If you set embed-xml="true" on the <one-to-many>
mapping, the data might look more like this:
100.29
-2370.34
Gavin
A
King
...
]]>
Manipulating XML data
Let's rearead and update XML documents in the application. We do this by
obtaining a dom4j session:
It is extremely useful to combine this feature with Hibernate's replicate()
operation to implement XML-based data import/export.