5 Mapping persistent classes

This chapter covers

  • Understanding entities and value types
  • 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 for structuring classes in an application, no matter whether you work with Hibernate, Spring Data JPA, or some other persistence framework that implement the JPA specification. We’ll 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 or Spring Data JPA using Hibernate as a persistence provider will load and store instances of your domain model classes.

Spring Data JPA, as a data access abstraction, sits on top of a JPA provider (such as Hibernate) and will significantly reduce the boilerplate code required to interact with the database. That is why, once the mapping of persistent classes is made, it can be used both from Hibernate and from Spring Data JPA. Our examples will demonstrate this, and all our mapping examples will use JPA annotations.

5.1 Understanding entities and value types

5.1.1 Fine-grained domain models

5.1.2 Defining application concepts

5.1.3 Distinguishing entities and value types

5.2 Mapping entities with identity

5.2.1 Understanding Java identity and equality

5.2.2 A first entity class and mapping

5.2.3 Selecting a primary key

5.2.4 Configuring key generators

5.2.5 Identifier generator strategies

5.3 Entity-mapping options

5.3.1 Controlling names

5.3.2 Dynamic SQL generation

5.3.3 Making an entity immutable

sitemap