Search code examples
javahibernatejoinfetchhibernate-criteria

@Fetch(FetchMode.JOIN) in Hibernate violates the FetchType.LAZY


I am working with the hibernate >5.2.10.Final and in that using the FETCH JOIN to eagerly load the inner entities.

@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(name = "XYX", joinColumns = @JoinColumn(name = "TT"), inverseJoinColumns = @JoinColumn(name = "TT"))
@Fetch(FetchMode.JOIN)
private Set<PWR> pwrs;

As the FetchType is set to Lazy, and FetchMode to JOIN, in the related query the pwrs are getting loaded eagerly.

So, my question is shall we not user FetchType if we are using the FetchMode as JOIN?


Solution

  • @Fetch(FetchMode.JOIN) will override @OneToMany(fetch = FetchType.LAZY) you can't use both together.