I have been experimenting with OpenJPA and JAXB with regard to storing and retrieving XML from an DB2 9.7 database and I've run into a problem. I've gone through the steps of creating a simple XML schema and generating the Java classes using xjc. I have followed the steps in the OpenJPA manual so I have not defined a name space in the schema and I have added the @XmlRootElement annotation to the generated root class. I have created sample code that successfully creates an instance of my entity with the JAXB property and I can persist the entity to the database. I can also retrieve the entity from the database and the XML is unmarshalled successfully as long as I use a single instance of the EntityManager to do both tasks. What I've found is if I use a separate instance on EntityManager to retrieve the entity, I receive an error along the line of this:
org.apache.openjpa.persistence.PersistenceException: javax.xml.bind.JAXBElement incompatible with..... This only happens if I use a instance of EntityManager that is different then the instance used to persist the original record. For example, if I do the following: EntityManagerFactory emFactory = ....... EntityManager em = emFactory............ em.getTransaction().begin();............ /*. Create the instance of the JAXB class and the entity. Populate both classes with data. Set the JAXB property of the entity class */ em.persist(.....); em.getTransaction().commit(); MyEntity entity = em.find(.......); The above code executes without error. I've checked the db record and it is correct. I have also validated the XML in the db against the schema used to generate the JAXB classes and there are no issues there either. If I run another program that tries to retrieve the same entity as above.... i.e. EntityManagerFactory emFactory = ....... EntityManager em = emFactory........... MyEntity entity = em.find(.......);.... the exception noted above is thrown. I am currently using OpenJPA 1.2.1 and JAXB 2.0.5. I have also tried this with JAXB 2.1.12 and I receive the same result. Am I doing something wrong? Could this be a bug in OpenJPA?