Persisting XML with OpenJPA
Tags: db2, db2express-c, java, jpa, mapping, Open Source, openjpa, orm, persistence, purexml, xml
I’ve been looking at JPA - Java Persistence Architecture and decided to play a bit with OpenJPA using DB2 as the back-end. My goal: to persist and query XML data in DB2, making use of DB2’s pureXML capabilities to query the XML data using SQL/XML’s XMLQUERY() function.
However, while OpenJPA has an extensive documentation, the examples are not always complete and there isn’t a lot of information on the web regarding OpenJPA error determination and solving. So, here are some recommendations for some of the problems I have encountered. The class xml.Address is the one to be persisted as XML in the database using JPA and it is stored as the field shipAddress of the Order objects.
[java] Exception in thread “main” <openjpa-1.0.0-r420667:568756 fatal user error> org.apache.openjpa.persistence.ArgumentException: Type “class xml.Address” does not have persistence metadata.
Suggestion: Remove the reference to xml.Address from persistence.xml.
[java] Exception in thread “main” <openjpa-1.0.0-r420667:568756 nonfatal user error> org.apache.openjpa.persistence.InvalidStateException: Encountered unmanaged object “xml.Address@9b2a51″ in persistent field “xml.Order.shipAddress” of managed object “xml.Order@12b3349″ during flush. However, this field does not allow cascade persist. You cannot flush unmanaged objects. [java] FailedObject: xml.Address@9b2a51
Suggestion: Make sure you have no @Entity or @Embeddable annotations in xml.Address. The main annotation is @XmlRootElement.
[java] Exception in thread “main” <openjpa-1.0.0-r420667:568756 fatal general error> org.apache.openjpa.persistence.PersistenceException: “xml” doesnt contain ObjectFactory.class or jaxb.index
Suggestion: add a file jaxb.index into your xml package containing all the classes to be persisted as XML: a file containing Address in our case.
Popularity: 47% [?]





