JOIN vs JOIN FETCH in JPA hibernate

Ví dụ bạn có một Student Entity và một Country entity
trong Student entity có một khóa ngoại tới Country entity


JOIN khi dùng thì nó tương tự như câu lệnh JOIN trong SQL, nhưng khi Đối tượng Student được trả về thì nó sẽ hoặc load cả Country entity hoặc là không load Contry(Nếu bạn để là Lazy thì nó sẽ k load Country còn nếu để là Eager thì nó sẽ load Country)

do đó khi bạn dùng student.getCountry() nó có thể trả về Null hoặc là lỗi.


Còn nếu bạn dùng JOIN Fetch khi load Student Join fetch với Country thì nó sẽ load cả đối tượng Contry bất kể bạn để load là Lazy hay là Eager
vì vậy bạn có thể dùng student.getCountry().getName() là có giá trị


Comments

Popular posts from this blog

Fixing the DeepSpeed Import Error While Fine-Tuning the Qwen Model

Amazon Linux 2023 - User data configuration for launch templates to connect to the EKS cluster

How to create ISM policy and rotate logs in opensearch