How FetchType.EAGER and FetchType.LAZY works in JPA/Hibernate

I am new to Java EE, using Spring Boot, JPA and Hibernate. I have two entities, Game and Genre with ManyToMany relationship here.

Game:


@Data
@Entity
@Table(name = "games")
public class Game implements Serializable {
    private static final long serialVersionUID = 1L;
    
    @Id
    @Column(name = "game_id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    
    @Column(name = "game_title")
    private String gameTitle;
    
    @Column(name = "price")
    private double price;
    
    @Column(name = "description")
    private String description;
    
    @Column(name = "game_picture")
    private String picture;
    
    @Column(name = "platform")
    private String platform;
    
    @Column(name = "release_date")
    private Date releaseDate;
    
    @ManyToMany
    @JoinTable(name = "game_genre", joinColumns = @JoinColumn(name = "game_id"), inverseJoinColumns = @JoinColumn(name = "genre_id"))
    private Set<Genre> genres;

    public String getGenreNames() {
        return genres.stream().map(Genre::getTitle).collect(Collectors.joining(", "));
    }
}

Genre:

@Data
@Entity
@Table(name = "genre")
public class Genre {

    @Id
    @Column(name = "genre_id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @Column(name = "title")
    private String title;

    @ManyToMany(mappedBy = "genres", fetch = FetchType.EAGER)
    private Set<Game> games;

}

TestController

@Controller
public class TestController {

    @Autowired
    private GameService gameService;
    @Autowired
    private GenreService genreService;

    @GetMapping("/test")
    public String testPage(@RequestParam(name = "genreId", required = false) Integer genreId, Model model) {
        List<Game> gameList;
        if (genreId != null) {
            Genre genre = genreService.getGenreById(genreId);
            String genreTitle = genre.getTitle(); // Gọi getTitle() trực tiếp
            gameList = gameService.selectByGenre(List.of(genreTitle)); 
        } else {
            gameList = gameService.selectAll();
        }
        model.addAttribute("genreList", genreService.getAllGenres());
        model.addAttribute("gameList", gameList);
        return "test";
    }

test.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Product List</title>
</head>
<body>
    <form action="${pageContext.request.contextPath}/test">
        <select name="genreId">
            <option value="">Choose genre</option>
            <c:forEach var="genre" items="${genreList}">
                <option value="${genre.id}">${genre.title}</option>
            </c:forEach>
        </select>
        <button type="submit">Apply Filter</button>
    </form>

    <c:forEach var="game" items="${gameList}">
        <div class="Product row bg-light Product___">
            <img alt="${game.gameTitle}" src="${pageContext.request.contextPath}/image/${game.picture}" class="col-3" width="90%">
            <div class="Product_info col-9">
                <h5>${game.gameTitle}</h5>
                <p>Genres: ${game.genreNames}</p>
                <p>Price: ${game.price}</p>
                <form action="${pageContext.request.contextPath}/Product_info?id=${game.id}">
                    <button>More Information</button>
                </form>
            </div>
        </div>
    </c:forEach>
</body>
</html>

But somehow, only the last item of gameList displays the genres, other items have the genres as NULL. I have tried to execute the genres separately, but it’s still NULL.

I’m sorry if there is already an answer out there, I cannot find it after crawling in the Internet and tried to fix it for 3 days. Please help!

I expect to get the Game’s “genres” attribute as String (such as Action, Role-playing), but it returns nothing, except the last game.
This is the result
In the beginning, it’s NULL for all games, until I add this for Genre
@ManyToMany(mappedBy = "genres", fetch = FetchType.EAGER)

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật