liveBook · Manning

Object-relational mapper

As explained in Java Persistence with Spring Data and Hibernate epub

Object-relational mapping (ORM) is the technique of representing and synchronizing object-oriented domain models with relational database structures.

An object-relational mapper (ORM) persists application objects in relational database tables and recreates them when needed. Mapping metadata describes how classes and properties correspond to the SQL schema; the ORM performs the transformation between objects in memory and stored data. This replaces much repetitive work, such as copying JDBC results into objects and writing SQL for common persistence operations. It does not remove the need to understand relational modeling or SQL. section 1.3 section 1.4

The central difficulty is the object-relational mismatch. An object network cannot simply be stored as one table row: its state must be represented in columns and relationships. Inheritance adds another problem. An object may refer to any subclass of a billing-details type, but a relational foreign key refers to one target table. Mapping choices must preserve the behavior the application needs while fitting the database structure. section 1.4 section 1.2.2

In a Java application, the main layers have distinct jobs:

  • JPA defines the standard persistence and mapping API; Hibernate implements it and also offers a native API. section 1.3 section 2.1
  • Spring Data JPA sits above a JPA provider and reduces data-access boilerplate, including through queries generated from method names. The same JPA-annotated class mappings can serve Hibernate and Spring Data JPA. section 1.3 section 5.0
  • Spring Data JDBC offers a simpler, more limited ORM. It uses SQL rather than JPQL and has no lazy loading, caching, sessions, or dirty tracking; entities must be saved explicitly. section 15.0

Mapping an auction domain

An auction application may persist users, items, bids, categories, and billing details. A user can have multiple billing details, while billing strategies can be subclasses of a common type. Basic properties of a persistent class are mapped by default when their types are recognized; JPA annotations customize exceptional cases. Collections, parent-child relationships, and associations require closer attention because object references must correspond to relationships between tables. section 3.1.3 section 6.1 section 8.0

SELECT * FROM USERS, BILLINGDETAILS
WHERE USERS.ID = 123 AND
BILLINGDETAILS.ID = USERS.ID

This SQL query retrieves a user together with billing details. An ORM still has to account for the same retrieval choice: loading associated data requires database work, even when Java code accesses it by navigating objects. section 1.2.5

Choose retrieval strategies with the intended object navigation in mind. Too many database requests hurt performance, but fetching an unnecessarily large network wastes memory and can produce large query results. Inspect the SQL emitted by an ORM and its performance implications rather than treating mapping as a substitute for query design. section 1.2.5 section 1.3

How Java Persistence with Spring Data and Hibernate epub covers it

How deep it goes
Defines metadata-driven persistence and examines mapping defaults, associations, inheritance, and retrieval tradeoffs rather than treating ORM as automatic SQL avoidance.
Examples
Uses an auction domain with users, items, bids, categories, and billing details, plus SQL retrieval of associated billing data.
What it assumes
Assumes familiarity with the relational model, SQL, and normalization; defines ORM itself rather than teaching database fundamentals from zero.
What you can do after
Apply it via JPA and Hibernate or Spring Data, while evaluating mapping choices and inspecting generated SQL for performance.
How it connects
Relates ORM to JPA, Hibernate, Spring Data JPA, JDBC, relational modeling, inheritance, and entity associations.

Also explained in

Compare how 7 books explain object-relational mapper →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Java Persistence with Spring Data and Hibernate epub. The section links point to the parts of the book it draws on.