Shobbi
Shobbi

Reputation: 947

how to get query result as XML in hibernate?

I am using struts2 with hibernate. Does anyone know if it is possible to return query result as XML instead of ArrayList of domain objects?

Upvotes: 0

Views: 2186

Answers (3)

Ken Chan
Ken Chan

Reputation: 90457

Hibernate by default maps and persists a database record thought POJO , but in fact it also supports persisting , mapping and representing a database record in XML by using an experimental features called Dynamic models.

For example , to output a record in XML:

/**Get the a new session that is in the DOM4J EntityMode**/
Session dom4jSession = session.getSession(EntityMode.DOM4J);
Element outputXML=(Element) dom4jSession.get(Employee.class, employeeId);
XMLWriter writer = new XMLWriter( System.out, OutputFormat.createPrettyPrint() );
writer.write( outputXML);

To configure the format of the outputted XML , you can only do it by mapping the entity in XML . AFAIK ,there are no annotation equivalent .

Upvotes: 2

reach4thelasers
reach4thelasers

Reputation: 26909

Hibernate is an Object-Relational Mapper, meaning it maps a Relational database to objects. You want to use Hibernate to return an object and then use an XML Serializer to convert to XML.

The Simple Serializer is probably the best one to get started with. The Website contains a lot of tutorials and examples.

http://simple.sourceforge.net/

However there are a ton of XML Serializers for Java:

http://karussell.wordpress.com/2009/09/03/xml-serializers-for-java/

Upvotes: 1

npinti
npinti

Reputation: 52185

Maybe you could, once you have the result use XStream to parse the entire result to XML. A simple tutorial on XStream is available here.

Upvotes: 0

Related Questions