Persisting XML with OpenJPA

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.

VN:F [1.8.1_1037]
Rating: 0.0/10 (0 votes cast)
VN:F [1.8.1_1037]
Rating: 0 (from 0 votes)

Popularity: 21% [?]

Tags: , , , , , , , , , , . 

Related Entries:
  • No related posts


  • 3 Responses to “Persisting XML with OpenJPA”

    1. Patrick Linskey Says:

      Once you followed the suggestions you list above, did things work properly for you? Just wondering so we can improve our error messages for next time.

      UN:F [1.8.1_1037]
      Rating: 0.0/5 (0 votes cast)
      UN:F [1.8.1_1037]
      Rating: 0 (from 0 votes)
    2. vitor Says:

      Hi Patrick,
      yes, things worked properly. And yes, it was a bit hard to identify the problem just from the error messages. It was a bit of trial and error from other people’s suggestions until I was able to fix the problems one by one. I also think that adding these (and other) errors to an FAQ in the OpenJPA website would be helpful. For example, the message ““xml” doesnt contain ObjectFactory.class or jaxb.index” seems pretty obvious for me now, but before that, I had no idea why I needed a jaxb.index file and where it should be located.
      Thanks for stopping by.

      UA:F [1.8.1_1037]
      Rating: 0.0/5 (0 votes cast)
      UA:F [1.8.1_1037]
      Rating: 0 (from 0 votes)
    3. security+ Says:

      Very great theme ! i really enjoy it, thanks a lot for sharing the different step of creation, very interesting to know !

      UN:F [1.8.1_1037]
      Rating: 0.0/5 (0 votes cast)
      UN:F [1.8.1_1037]
      Rating: 0 (from 0 votes)

    Leave a Reply