I have Lazy Loaded OneToMany, ManyToMany and ManyToOne collections in my Spring JPA entities. Whenever I want to load my OneToMany and MantToMany collections into my transaction, I use this trick
school.getStudents().size()
In a similiar fashion, I would like to load my ManyToOne collection (say School->City). How would I eagerly load my City data into my transaction in some 'simple way' similiar to the OneToMany collection show above.
My alternatives right now are
- Change the ManyToOne to Eager loading
- Write a JPQL query fetching the city while fetching the school data. But this is not very flexible if I have multiple ManyToOne entities and any additional fields involves changing the JPQL query.
Would appreciate any suggestions.