我有下一个实体。每次我从db加载filmManager时会话数量是错误的。
@Table(name = "FILM_MANAGERS")
@Entity
public class FilmManager {
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "FILM_ID",updatable = false,insertable = false)
private Film film;
@OneToMany(cascade = CascadeType.REMOVE, fetch = FetchType.EAGER,mappedBy = "filmManager")
private List(FilmSession) sessions;
other fields...
}
@Entity
@Table(name = "FILMS")
public class Film {
@OneToOne(mappedBy = "film")
private FilmManager filmManager;
@OneToOne(mappedBy = "film")
private Ticket ticket;
other fields...
}
@Table(name = "TICKETS")
@Entity
public class Ticket {
@ManyToOne
@JoinColumn(name = "USER_ID", updatable = false, insertable = false)
@JsonBackReference
private User user;
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "FILM_ID", updatable = false, insertable = false)
private Film film;
}
@Table(name = "FILM_SESSIONS")
@Entity
public class FilmSession {
@ManyToOne
@JoinColumn(name = "FILM_MANAGER_ID",insertable = false, updatable = false)
private FilmManager filmManager;
}
@GetMapping("/films/{filmId}")
public String getFilm(ModelMap modelMap, @PathVariable("filmId") String filmId) {
FilmManager filmManager = filmManagerService.getFilm(Long.parseLong(filmId));
modelMap.addAttribute("filmManager", filmManager);
modelMap.addAttribute("screenshots", resourceExtractor.getScreenShots(filmManager));
modelMap.addAttribute("sessions", filmManager.getSessions().stream().map((e) -> e.getSessionTime()).collect(joining(",")));
System.out.println("amount of sessions: " + filmManager.getSessions().stream().count());
return "filmPage";
}
amount of sessions: 11
最后一个SOUT在屏幕上输出11个会话,你可以看到我只有一个。screen1
我发现,每当我为电影插入新票时,视图页面上的会话数量就会增加。因此,基本会话会与票证相乘,而不是1,它会加载11。 这是我的门票表screen2
的屏幕感谢大家的帮助。