Java Reference
In-Depth Information
be checked for changes. If the fields being checked have changed prior to the commit, then
the commit will fail.
Versioning is generally a simpler and more reliable approach, so we suggest that you use
this whenever you need optimistic locking features.
XML Relational Persistence
Hibernate provides a feature that allows XML data to be mapped into the entity model for
access using the normal session methods. This functionality is provided primarily so that
data can be imported into and exported from the underlying relational data store—it is not
intended as a replacement for relational databases!
The feature can be used for various purposes—archiving data, implementing SOAP
interfaces, and so on—but the most common use is for the purposes of processing (and
providing) external data feeds such as product catalogs. We show here how the example
application in Chapter 3 (an advertisements database) can be configured to read and write
appropriate XML feeds for the mapped entities.
Hibernate requires the use of Dom4J as the API for XML access because Hibernate's inter-
nals already rely upon Dom4J to read configuration and mapping files.
Adding Node Information to Mappings
Two attributes are used to add all the XML-specific information to your existing mapping files:
node and embed-xml .
The node attribute applies to most tags that correspond to tables or columns in the data-
base. The value can be a single string, in which case it represents an element name in the XML
markup; or it can be preceded by a commercial at symbol ( @ ), in which case it represents the
attribute of an element. Paths can be indicated using forward slashes. This is the standard
XPath syntax for identifying elements in an XML document.
If set to true , the embed-xml attribute indicates that the property or entity referenced
should be included inline as XML. If set to false , it indicates that a reference to the primary
key should be substituted instead. This is necessary because the DOM generation does not
perform reference handling automatically—so loops in the entity model references would
result in infinite loops in XML generation if this option could not be set to true .
Note that if embed-xml is set to false for an association, generating XML output that refer-
ences another entity will not automatically include a representation of the entity elsewhere in
the generated document. This is your responsibility. Listing A-7 shows how our example Advert
class from Chapter 3 might be marked up with node and embed-xml attributes.
Listing A-7. The Advert Example Classes from Chapter 3 Marked Up for XML Persistence
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
Search WWH ::




Custom Search