Basics of Relational Databases 2

Sungju Kim·2024년 9월 5일

Sparta_Coding_Camp_TIL

목록 보기
30/53

Data Fetch Type (Eager vs. Lazy)

In the @ManyToOne or @OneToMany relationships, the joined column is automatically called when loading the main/original table's information if it is "one" entity in the relationship. This is called eager loading. However, often this loading is unnecessary and slows down the program, in that case we can alter the fetch type to lazy loading which will differ the loading of the joined column unless it is specifically called for.

Many: default loading method is LAZY
One: default loading method is EAGER

You can see in the example below that the user column is called automatically. This occured while fetching the food's name and price.

Changing the fetch type

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private User user;
profile
Fully ✨committed✨ developer, always eager to learn!

0개의 댓글