-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Export To Xml, How does Hibernate name elements
PostPosted: Sun Sep 23, 2007 12:35 pm 
Newbie

Joined: Tue Sep 18, 2007 11:54 am
Posts: 2
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?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.