liveBook · Manning

One-to-one relationship

As explained in Java Persistence with Spring Data and Hibernate epub

A one-to-one association is an association in which each entity instance is related to at most one instance of the other entity type.

A one-to-one relationship associates each entity instance with at most one instance of another entity type. In persistence models, choose it when the database and domain require that cardinality, rather than introducing a collection unnecessarily. section 9.1 section 9.5

Select the mapping strategy according to ownership, identity, and optionality:

  • Use an embeddable component when the related value has a dependent lifecycle, as with a user's billing, home, or shipping address. section 9.1
  • Use a dedicated entity when references can be shared, requiring the related object to have its own identity. section 9.1
  • Choose a shared primary key when one entity is always stored before the other and can supply the primary key. section 9.1.4
  • Choose a foreign-key association in other cases, or use a join table when the relationship is represented through an intermediate link. section 9.1.4

Optional shipment-to-item association

An escrow application can model a Shipment with an optional one-to-one Item association. The ITEM_SHIPMENT table uses SHIPMENT_ID as its primary key and makes ITEM_ID unique, preventing either shipment or item from participating in more than one link. A JPA mapping uses @OneToOne and @JoinTable; lazy loading still checks the join table when loading Shipment. section 9.1.4

Spring Data JDBC user and address

Spring Data JDBC models one Address inside User with @MappedCollection(idColumn = "USER_ID"). The USERS table's ID becomes the foreign key in ADDRESSES, while separate CrudRepository interfaces persist and test both entities. section 15.3.1

Figure 9.1 Address as an entity with two associations, supporting shared references

Use the entity diagram to distinguish an Address entity with its own identity from a component-valued address. section 9.1

How Java Persistence with Spring Data and Hibernate epub covers it

How deep it goes
Compares component, shared-primary-key, foreign-key, and join-table mappings, including schema constraints and lazy-loading behavior.
Examples
Uses User–Address mappings, an optional Shipment–Item link, SQL schemas, repository interfaces, annotations, and persistence tests.
What it assumes
Assumes basic entity, table, primary-key, foreign-key, repository, and Spring Data concepts while distinguishing components from entities.
What you can do after
Apply it via JPA annotations or Spring Data JDBC mappings while selecting storage based on lifecycle, identity, and optionality.
How it connects
Connects one-to-one relationships with embeddable components, entity identity, primary keys, foreign keys, join tables, JPA, and Spring Data JDBC.

Also explained in

Compare how 3 books explain one-to-one relationship →

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.