Mapping metadata tells an ORM tool how to persist a Java domain model in a relational database. It links classes to tables, properties to columns, associations to foreign keys, and Java types to SQL types. The tool uses those descriptions to transform data between objects and database rows; the mappings must be created and maintained as the model changes. section 1.3 section 3.3
JPA offers two standard ways to supply mappings:
- Put annotations beside the Java declarations they describe. Annotations are type-safe, and Java development tools can validate them and include them in refactoring operations. section 3.3 section 3.3.1
- Keep mappings in external XML descriptor files deployed with the compiled classes. This separates mapping declarations from Java source, but typical Java refactoring tools cannot update XML names as reliably as annotations. section 3.0 section 3.3.1
import javax.persistence.Entity;
@Entity
public class Item {
}
A minimal mapping: @Entity marks Item as persistent. With the default strategy, its attributes become persistent managed state, so instances can be loaded and stored. section 3.3.1
For annotation-based mappings, JPA metadata is compiled into class files. Hibernate reads it through reflection when the application starts. Compiling an annotated class requires JPA libraries; merely creating an instance without accessing the annotations does not. Hibernate-specific annotations can supplement standard JPA mappings when a needed option is not standardized. section 3.3.1 section 3.3.1
Mapping metadata does not remove the need to understand the relational schema or inspect generated SQL. The JPA Metamodel API can expose the persistent entities, attributes, and mapping metadata of a persistence unit when code needs to examine them. section 1.3 section 3.0
