Search code examples
javahibernatehibernate-mapping

Eager loading at Collection level with Annotation in Hibernate


Uable to achieve aggressive/eager loading at Collection level.

I know eager loading at collection level with XML

enter image description here

but How I could achieve the same with annotation at collection level.?

Student Persistence Class

enter image description here

Here I need to do eager loading for Collection Type semails field.


Solution

  • If you are using hibernate you can use @Fetch annotation with FetchMode.JOIN. This will make hibernate use join and fetch collection with a single select query joining your element collection table. Make sure your collection has two annotations:

    @ElementCollection(fetch=FetchType.EAGER)
    @Fetch(FetchMode.JOIN)