Hibernate To Represent The DB Data In XML

One of the feature about hibernate is its ability to represent the db data in XML. Without much work, the entity data will be converted into XML. One can fetch table data similar to normal loading and can get its representation in XML.

Though XML can’t be used as data storage, but its useful it terms of communication of data as XML files are simple text files. Also in java we can always transform XML to other, which is the best use of XML.

Hibernate doesn’t store data in XML, but it can represent data in XML. The tag names will be the names of the attribute used to represent column of table.

Consider an entity class representing a table user_tasks in db. Following is a rough mapping file: task.cfg.xml:

<?xml version=”1.0??>
<!DOCTYPE hibernate-mapping PUBLIC
“-//Hibernate/Hibernate Mapping DTD 3.0//EN”
“http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd“>

<hibernate-mapping>
<class name=”com.myapp.eo.TaskEO” table=”user_tasks”>
<id name=”task_id” type=”int” column=”user_task_id” >
<generator/>
</id>

<property name=”taskName”>
<column name=”task_name_desc” />
</property>
<property name=”startTime”>
<column name=”start_time”/>
</property>
<property name=”endTime”>
<column name=”end_time”/>
</property>
<property name=”totalTaskTime”>
<column name=”total_task_time”/>
</property>
</class>
</hibernate-mapping>

Now if you want to retrieve the data in form of XML then you can do so like below:

Transaction tx = null;
Element taskObj = null;
Document xmldoc;
Session session = HibernateSessionUtil.getInstance().getCurrentSession();

try {
tx = session.beginTransaction();
Session xmlSession = session.getSession(EntityMode.DOM4J);
taskObj = (Element)xmlSession.load(TaskEO.class, taskId);
xmldoc = DocumentHelper.createDocument();
Element root = xmldoc.addElement( “data” );
root.add(taskObj);
tx.commit();
}

(Note: the above code is using DOM4j elements)

Above code creates XML document with the entity data.

We can later print the xml like below:

System.out.println(xmldoc.asXML());

It will print following:

<?xml version=”1.0? encoding=”UTF-8??>
<data>
<TaskEO>
<task_id>19</task_id>
<taskName>Task 3 – light up</taskName>
<startTime>2009-10-08 00:00:00</startTime>
</TaskEO>
</data>

Here the tags names are the names of the bean variable names.(or property names)

<id name=”task_id” type=”int” column=”user_task_id” >
<generator/>
</id>
<property name=”taskName”>
<column name=”task_name_desc” />
</property>
<property name=”startTime”>
<column name=”start_time”/>
</property> 

Also the generated XML is not having any column value which is null. In above example table has endTime and totalTaskTime properties, but its not been printed as there are null.

 If you want to change the tag names you can do so by adding node attribute something like below: (changing the task.cfg.xml )

<hibernate-mapping>
<class name=”com.myapp.eo.TaskEO” table=”user_tasks” node=”task”>
<id name=”task_id” type=”int” column=”user_task_id” node=”id”>
<generator/>
</id>

<property name=”taskName” node=”name”>
<column name=”task_name_desc” />
</property>
<property name=”startTime” node=”start”>
<column name=”start_time”/>
</property>

then output will be:

<data>
<task>
<id>19</id>
<name>Task 3 – light up</name>
<start>2009-10-08 00:00:00</start>
</task>
</data>

You can also make the id, start, name etc as attribute to main node of task by adding a ‘@’ like node=”@id”:

<class name=”com.myapp.eo.TaskEO” table=”user_tasks” node=”task”>
<id name=”task_id” type=”int” column=”user_task_id” node=”@id”>
<generator/>
</id>

<property name=”taskName” node=”@name”>
<column name=”task_name_desc” />
</property>
<property name=”startTime” node=”@start”>
<column name=”start_time”/>
</property>

Then output will be:
<data>
<task id=”19? name=”Task 3 – light up” start=”2009-10-08 00:00:00?/>
</data>

Do you have a Java Problem?
Ask It in The Java Forum

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Return to : Java Programming Hints and Tips

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site www.erpgreat.com is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.