liveBook · Manning

Composition relationship

As explained in Java Persistence with Spring Data and Hibernate epub

Composition is an object-model relationship in which the lifecycle of a contained part is fully dependent on the lifecycle of its containing whole.

A composition relationship makes a part fully dependent on its containing whole. The part cannot conceptually exist independently, although objects may navigate the relationship in both directions. section 1.2 section 6.2

Use composition to decide identity, ownership, and persistence behavior:

  • Treat the contained object as a value type when it cannot be shared and does not need independent identity, as with Address owned by User. section 5.1.3
  • Promote a composed part to an entity when future associations create shared references, as Bid may require if users expose their bids. section 5.1.3
  • Bind collections of components to the owner’s lifecycle; an Item’s Image components belong to the Item and are not independent entities. section 8.2

Embedding a composed Address

A User can contain homeAddress and billingAddress as value-type components. The USERS table stores the User and all address columns in one row; saving or deleting the User saves or deletes the component without separate SQL. section 6.2.1

User and BillingDetails

A User may own many BillingDetails objects. A BILLINGDETAILS.USERNAME foreign key references USERS.USERNAME, preserving the containment relationship in the relational schema. section 1.2

Figure 6.1 Composition of User and Address

Use the UML diagram to identify the whole, the composed part, and the direction of lifecycle dependence. section 6.2

How Java Persistence with Spring Data and Hibernate epub covers it

How deep it goes
Explains lifecycle, identity, shared-reference consequences, embedded schemas, and component collections, stopping short of framework-specific configuration.
Examples
Uses User–BillingDetails, User–Address, Item–Bid, and Item–Image relationships with Java classes, UML diagrams, and SQL schemas.
What it assumes
Assumes basic object associations, UML composition notation, Java classes, and relational tables with foreign keys.
What you can do after
Evaluate whether a related class should remain a value type or become an entity when sharing, identity, or lifecycle requirements change.
How it connects
Connects aggregation, value types, entities, embeddable components, foreign keys, and object-relational mapping.

Also explained in

Compare how 2 books explain composition 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.