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)