yet another question about Hibernate lazy loading not working, but this time I have done my research beforehand.
From what I understood from Hibernate docs and so many other posts here on SO, on Hibernate 6.4 the @ManyToOne with FetchType.LAZY should work on the owning side, and that’s what I have. This is my entity:
public final class Club {
@Id
private UUID id;
@Column(name = "name", nullable = false)
private String name;
@Column(name = "abbreviation", nullable = false)
private String abbreviation;
@Column(name = "crest", columnDefinition = "jsonb")
@Type(JsonBinaryType.class)
private Crest crest;
@Column(name = "is_bot", nullable = false)
private boolean isBot;
@ManyToOne(optional = false, fetch = FetchType.LAZY)
@JoinColumn(name = "country_id", nullable = false)
private Country country;
@OneToOne(optional = false, fetch = FetchType.LAZY)
@JoinColumn(name = "region_id", nullable = false)
private Region region;
@ManyToOne(optional = false, fetch = FetchType.LAZY)
@JoinColumn(name = "league_id", nullable = false)
private League league;
}
The relationships are mandatory and from what I’ve seen using the parameter optional = false
together with fetch = FetchType.LAZY
should do the trick to lazily load the relationships, but that’s not happening. On the other side of the relationship – just to be sure – I’m also adding the fetch = FetchType.LAZY
even though on @OneToMany the default is to lazy load.
public final class Country {
@Id
private UUID id;
@Column(name = "international_name", nullable = false)
private String internationalName;
@Column(name = "name", nullable = false)
private String name;
@Column(name = "code", nullable = false)
private String code;
@Column(name = "language", nullable = false)
private String language;
@Column(name = "timezone", nullable = false)
private String timezone;
@Column(name = "flag", nullable = false)
private String flag;
@Column(name = "match_date_times", columnDefinition = "jsonb", nullable = false)
@Type(JsonBinaryType.class)
private Map<MatchDate.MatchDateType, List<MatchDay>> matchDateTimes;
@OneToMany(mappedBy = "country")
private List<Region> regions;
@Column(name = "enabled", nullable = false)
private boolean enabled;
@OneToMany(mappedBy = "country", fetch = FetchType.LAZY)
private List<Club> clubs;
}
Am I missing something here?
I’m using Spring Boot 3.2.2 and Hibernate 6.4.1.Final.
Thanks for your help.