I am struggling trying to make a method to get an object from database and load the dependencies.
In normal case I never want to load it so my two objects look like this :
public class Training implements Serializable {
[...]
// Default => LAZY
@OneToMany(mappedBy = "training")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
private Set<Video> videoLists = new HashSet<>();
[...]
}
public class Video implements Serializable {
[...]
// Default => EAGER
@ManyToOne(optional = false)
@NotNull
@JsonIgnoreProperties("videos")
private Training training;
[...]
}
To achieve a certain goal I need to load my training in eager mode so I've made this function :
@Repository
public interface TrainingRepository extends JpaRepository<Training, Long> {
@Query("from Training t left join fetch t.videoLists")
List<Training> findEager();
}
This code make a StackOverflow error because of this cyclic fetching. What is the best way to keep my EAGER/LAZY fetching while making this function working ?