liveBook · Manning

@manytoone

As explained in Java Persistence with Spring Data and Hibernate epub

@ManyToOne is a Jakarta Persistence annotation that maps an association from multiple source entities to one target entity.

@ManyToOne maps a unidirectional association in which multiple source entities reference one target entity. A foreign-key column in the source table stores the target identifier; the association can stand alone without a mapped collection on the target. section 8.3.1

Configure the association by combining the required annotation with optional constraints and loading settings:

  • Place @ManyToOne on the source property, such as Bid.item. section 8.3.1
  • Use @JoinColumn to name the foreign-key column or make it NOT NULL; alternatively, use optional = false or @NotNull for non-nullability. section 8.3.1
  • Treat @ManyToOne as the owning side when the relationship is bidirectional; @OneToMany provides the mappedBy side. section 9.2.2

Bid referencing an Item

A Bid can require an Item through a non-null join column: @ManyToOne(fetch = FetchType.LAZY), followed by @JoinColumn(name = "ITEM_ID", nullable = false). Calling someBid.getItem() dereferences the association, while a JPQL query such as select b from Bid b where b.item = :itemParameter retrieves an Item's bids.

For ordered one-to-many collections, the @ManyToOne side still controls the join column and generated schema. In an embeddable link component, multiple @ManyToOne associations can instead form a ternary relationship, but the embeddable cannot provide bidirectional navigation. section 9.2.2 section 9.3.3

How Java Persistence with Spring Data and Hibernate epub covers it

How deep it goes
Explains join columns, nullability, fetching, ownership, queries, and ternary variants, but not provider internals beyond schema generation.
Examples
Uses Bid-to-Item mappings, ordered bid collections, and CategorizedItem links containing Item and User references.
What it assumes
Assumes entity classes and relational foreign keys while teaching the basic association without requiring a mapped inverse collection.
What you can do after
Apply it via JPA annotations, enforce association constraints, select loading behavior, and query entities through JPQL or repositories.
How it connects
Connects @ManyToOne with foreign keys, @JoinColumn, @OneToMany, JPQL, @NotNull, FetchType, and @Embeddable.

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.