I'd like to export objects to xml with a Hibernate-dom4j-session.
Here is my problem: There are objects that contain collections, that can contain other objects of any type, e.g. a 'Folder' contains objects, that can be of type 'Folder' or 'File'.
When exporting a folder with a folder and a file in its objects-collection I would get xml like this:
Code:
<Folder ...>
<Objects>
<Object>
... (actually a Folder)
</Object>
<Object>
... (actually a File)
</Object>
</Objects>
</Folder>
But actually I want the elements to be named accordingly to their types, similar to this:
Code:
<Folder ...>
<Objects>
<Folder>
...
</Folder>
<File>
...
</File>
</Objects>
</Folder>
Can I tell Hibernate somehow to derive element names by class?
Where would I have to implement something like that?For instance Castor
http://www.castor.org/index.html already implements this functionality (<bind-xml auto-naming="deriveByClass" node="element" />)
Thanks a lot for any help.
Here is some source:
Code:
public void exportToXml(PersistentObject persistentObject, File file)
throws PersistenceManagerException {
Session dom4jSession = getSessionFactory().openSession().getSession(
EntityMode.DOM4J);
Transaction tx = dom4jSession.beginTransaction();
Object result = dom4jSession.load(PersistentObject.class,
persistentObject.getId());
tx.commit();
Element element = (Element) result;
Document document = DocumentHelper.createDocument();
document.add(element);
try {
FileOutputStream fileOutputStream = new FileOutputStream(file);
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter writer = new XMLWriter(fileOutputStream, format);
writer.write(document);
fileOutputStream.close();
} catch (Exception exception) {
throw new PersistenceManagerException(exception);
} finally {
dom4jSession.close();
}
}
And some mapping-documents:
Code:
<hibernate-mapping package="..." default-cascade="save-update" >
<joined-subclass name="Folder" table="Folders" extends="...PersistentObject" node="Ordner">
<key column="Id" />
<property name="Name" type="string" node="@Name" />
<list name="Objects" table="FoldersObjects" node="Objekte">
<key column="FolderId" />
<list-index column="Position" />
<many-to-many column="ObjectId" class="...PersistentObject" />
</list>
</joined-subclass>
</hibernate-mapping>
<hibernate-mapping package="..." default-cascade="save-update">
<class name="PersistentObject" table="Objects"
lazy="true" node="Objekt">
<id name="Id" column="Id" unsaved-value="null" node="@Id">
<generator class="uuid"/>
</id>
<version name="Version" type="long" node="@Version"/>
...
</class>
</hibernate-mapping>
Hibernate version: 3.2.0.ga?