Skip to content
Ameen Abozaid
Planned lessonJNR-030-L01

Many-to-one: the default relationship

  • Lesson 1 of 8
  • Junior
On This Lesson 4 topics

This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.

In this lesson

map the most common relationship correctly.

Planned topics

  • Many Products belong to one Category: @ManyToOne on the product.
  • The table holding the foreign key is the "many" side (product.category_id).
  • @JoinColumn names the foreign key column.
  • Use fetch = FetchType.LAZY; the default for @ManyToOne is EAGER, which often causes extra queries.
  • Start here; most designs need only this plus a query.
Draft outline details

Demonstration

java
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "category_id")
private Category category;

Check your understanding

Which table holds the foreign key in a many-to-one?

Show answer

The "many" side.

Practice

Apply this lesson with the module's lab, exercises and quiz.

Lab and exercises Quiz

Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.

Continue to next lesson

Type to search levels, phases, modules and lessons.