Java Reference
In-Depth Information
<property name="message" column="message" type="string"
node="content"/>
<property name="title" column="title" type="string"
node="title"/>
<many-to-one name="user" column="aduser"
class="sample.entity.User" not-null="true"
foreign-key="fk_advert_user"
node="@user" embed-xml="false"/>
</class>
</hibernate-mapping>
Exporting XML Entities
With the entity mappings marked up for XML relational persistence, the generation of output
from an existing set of persistent entities is fairly simple. By obtaining a session with the entity
mode configured for Dom4J, the entity proxies retrieved from that session will be org.dom4j.Node
instances that can be manipulated with the normal Dom4J API.
To access a Session object in this mode, you first open a conventional session, and then
invoke the getSession() method on it, passing EntityMode.DOM4J as the sole parameter thus:
Session = sessionFactory.openSession();
Session xmlSession = session.getSession(EntityMode.DOM4J);
Once an appropriate Dom4J document has been populated with the entities (extracted
from Hibernate in the normal way), the session can be closed, and the Dom4J document can
be treated as a self-contained entity, as shown in Listing A-8.
Listing A-8. Exporting the Advert Entities Using Dom4J
package sample.xml;
import java.io.*;
import java.util.*;
import org.dom4j.*;
import org.hibernate.*;
import org.hibernate.cfg.Configuration;
public class ExportXML {
private static final SessionFactory sessionFactory = new Configuration()
.configure().buildSessionFactory();
public static void main(String[] args)
throws Exception
{
System.out.println("Preparing the DOM Document");
Document document = DocumentHelper.createDocument();
Element root = document.addElement("catalog");
Search WWH ::




Custom Search