Search code examples
javaopenjpa

Why is a field with a JAXB object not recognized as persistent state by OpenJPA?


I want to persist the XML of a JAXB object in a CLOB column in the table of the owning entity. OpenJPA ships with support for such constructs using its XMLValueHandler.

I followed this tutorial from IBM.

My sample code is:

@Entity
@Access(AccessType.FIELD)
public class EntityContainingXml {

    @Id
    private Long id;

    @Persistent
    @Strategy("org.apache.openjpa.jdbc.meta.strats.XMLValueHandler")
    @Column(name = "xml")
    @Lob
    private SomeJaxbType xmlStuff;
    //...
}

However the field xmlStuff is not recognized as persistent state by the OpenJPA enhancer. It does not make a change if SomeJaxbType is contained in the persistence unit.

What do I need to do so that the OpenJPA enhancer recognizes the field xmlStuff as persistent state?


Solution

  • The problem was a classpath issue due to maven dependencies. I can't say what exactly caused this issue. The original classpath contained some combination of the org.apache.openjpa artifacts openjpa-persistence-jdbc, openjpa-persistence, openjpa and openjpa-all. That was causing the org.apache.openjpa.persistence.PersistenceMetaDataFactory to be used during build time enhancement. But the parser org.apache.openjpa.persistence.AnnotationPersistenceMetaDataParser.AnnotationPersistenceMetaDataParser created by this factory is not able to recognize @Strategy.

    Now only openjpa-persistence-jdbc is currently used as a compile time dependency. And the build time enhancement has a dependency on the artifact openjpa.

    I debugged through the maven build to find that out. In my case it was not possible to use the configuration property openjpa.MetaDataFactory to set the appropriate meta data factory, because it caused a class cast exception. I then started to throw out OpenJPA. During that I build and ran the application once more and it suddenly worked.