5 Mapping persistent classes
This chapter covers
- Understanding entities and value type concepts
- Mapping entity classes with identity
- Controlling entity-level mapping options
This chapter presents some fundamental mapping options and explains how to map entity classes to SQL tables. This is essential knowledge to start with the structure of the classes in the application, no matter if you work with Hibernate, with Spring Data JPA, or some other persistence framework implementing the JPA specification. We demonstrate and analyze how you can handle database identity and primary keys and how you can use various other metadata settings to customize how Hibernate and Spring Data JPA using Hibernate as a persistence provider load and store instances of your domain model classes. Hibernate is a JPA implementation, while Spring Data JPA, as data access abstraction, comes on top of a JPA provider (Hibernate, for example) and will significantly reduce the boilerplate code to interact with the database. That is why, once the mapping of persistent classes is made, it may be used both from Hibernate and from Spring Data JPA. Our demonstrations emphasize these things.
All mapping examples use JPA annotations. First, though, we define the essential distinction between entities and value types and explain how you should approach the object/relational mapping of your domain model.