JPA : Metadata Overview

JPA requires the persistence of classes to be defined via Metadata. This Metadata can be provided in the following forms

  • XML : the traditional mechanism, with XML files containing information for each class to be persisted.
  • Annotations : using JDK1.5+ annotations in the classes to be persisted
We recommend that you use either XML or annotations for the basic persistence information, but always use XML for ORM information. This is because it is liable to change at deployment time and hence is accessible when in XML form whereas in annotations you add an extra compile cycle (and also you may need to deploy to some other datastore at some point, hence needing a different deployment).
Metadata priority

JPA defines the priority order for metadata as being

  • JPA XML Metadata
  • Annotations

If a class has annotations and JPA XML Metadata then the XML Metadata will take precedence over the annotations (or rather be merged on top of the annotations).

XML Metadata validation

By default any XML Metadata will be validated for accuracy when loading it. Obviously XML is defined by a DTD or XSD schema and so should follow that. You can turn off such validations by setting the persistence property datanucleus.metadata.validate to false when creating your PMF. Note that this only turns off the XML strictness validation, and not the checks on inconsistency of specification of relations etc.