0
0
Spring Bootframework~10 mins

@ManyToOne relationship in Spring Boot - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - @ManyToOne relationship
Define Entity A
Add @ManyToOne field to Entity A
Entity A references Entity B
Save Entity A with Entity B linked
Database stores foreign key in Entity A's table
Fetch Entity A retrieves linked Entity B
Shows how one entity holds a reference to another using @ManyToOne, storing a foreign key and linking data.
Execution Sample
Spring Boot
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;

@Entity
public class Order {
  @Id
  private Long id;

  @ManyToOne
  private Customer customer;

  // getters and setters
}

@Entity
public class Customer {
  @Id
  private Long id;
  private String name;

  // getters and setters
}
Defines Order entity with a ManyToOne link to Customer entity.
Execution Table
StepActionEntity StateDatabase EffectResult
1Create Customer objectCustomer{name='Alice'}No DB action yetCustomer ready
2Create Order objectOrder{customer=null}No DB action yetOrder ready
3Set Order.customer = CustomerOrder{customer=Alice}No DB action yetOrder linked to Customer
4Save Customer to DBCustomer{id=1, name='Alice'}Insert Customer rowCustomer stored with id=1
5Save Order to DBOrder{id=10, customer=Alice}Insert Order row with customer_id=1Order stored with FK to Customer
6Fetch Order from DBOrder{id=10, customer_id=1}Select Order rowOrder loaded with customer_id=1
7Fetch Customer from DB via OrderCustomer{id=1, name='Alice'}Select Customer rowCustomer loaded and linked
8End--Execution complete
💡 Execution stops after Order and Customer are saved and linked via foreign key.
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 4After Step 5Final
customernullCustomer{name='Alice'}Customer{name='Alice'}Customer{id=1, name='Alice'}Customer{id=1, name='Alice'}Customer{id=1, name='Alice'}
ordernullnullOrder{customer=Alice}Order{customer=Alice}Order{id=10, customer=Alice}Order{id=10, customer=Alice}
Key Moments - 3 Insights
Why does the Order entity store a foreign key instead of the whole Customer object in the database?
Because @ManyToOne creates a link via a foreign key column in Order's table pointing to Customer's primary key, not embedding the whole Customer data. See execution_table step 5.
What happens if you save Order before saving Customer?
The foreign key in Order would reference a Customer that doesn't exist yet, causing a database error. Step 4 must happen before step 5.
How does fetching Order also get the linked Customer?
When fetching Order, the foreign key customer_id is used to fetch the Customer entity separately, linking them in memory. See steps 6 and 7.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 5, what database action happens?
AUpdate Customer row
BInsert Order row with customer_id foreign key
CInsert Customer row
DDelete Order row
💡 Hint
Check the 'Database Effect' column at step 5 in execution_table.
At which step is the Customer entity assigned to the Order entity in memory?
AStep 1
BStep 5
CStep 3
DStep 7
💡 Hint
Look at the 'Entity State' column to see when Order.customer changes from null.
If you skip saving Customer before Order, what likely happens?
ADatabase error due to missing foreign key reference
BOrder saves successfully with null customer
CCustomer is auto-created
DOrder saves with customer name embedded
💡 Hint
Refer to key_moments about saving order before customer.
Concept Snapshot
@ManyToOne links one entity to another by storing a foreign key.
Use @ManyToOne on a field in the 'many' side entity.
Saving the 'one' side first is required.
Database stores foreign key in 'many' side table.
Fetching 'many' side can load linked 'one' side entity.
Full Transcript
The @ManyToOne relationship in Spring Boot connects one entity to another by storing a foreign key in the database. First, you create the 'one' side entity (Customer) and the 'many' side entity (Order). You add @ManyToOne annotation on the Order's customer field. When saving, you must save the Customer first to get its ID. Then save the Order with the customer linked. The database stores the foreign key in the Order table. When fetching an Order, the linked Customer is loaded using that foreign key. This flow ensures data integrity and proper linking between entities.