Quick tutorial

XMLPersistence usage is really simple. You can work on one instance of a document and then save it in persistence storage. Hibernate will only save the inserted/updated/deleted nodes (meaning optimal performance).

You can create your own document and save it in database like this:


Document document = builder.parse(xmlFile);

String docName = "MyDoc";
DocumentImpl doc = new DocumentImpl( docName, document );

Transaction tx = session.beginTransaction();
session.save( doc );
tx.commit();



You can perform lookup in database to get a save document (using hibernate session load method):

Document document = (Document)session.load( DocumentImpl.class, new Long( 1 ) );

or

List list = session.find( "from DocumentImpl as where name='" + docName+"'" );
Document document = (DocumentImpl)list.get(0);



FYI, Hibernate does not work with instance pooling so working on one instance of document can be considered as threadsafe.



To delete a document, just perform:

Transaction tx = session.beginTransaction();
session.delete( document );
tx.commit();



XPath/XQuery/XUpdate

To perform XPath/XQuery/XUpdate queries see the examples here.