2

Consider this simple association:

@Entity
public class Employee
{
    @OneToMany(fetch=FetchType.LAZY)
    private Set<Address> addresses;
}

Using this code the addresses are not fetched in the result:

Query query=entityManager.createQuery("select e from Employee e");
query.setHint("eclipselink.batch.type", "JOIN");
query.setHint("eclipselink.batch", "e.addresses");
List list=query.getResultList();

While in this one the addresses are fetched:

Query query=entityManager.createQuery("select e from Employee e");
query.setHint("eclipselink.join-fetch", "e.addresses");
List list=query.getResultList();

Why the batch fetch is not working in the first? I'm using EclipseLink 2.5.1. I also tried the @BatchFetch annotation and neither of those approaches did work.

Thunder
  • 2,994
  • 1
  • 24
  • 19

1 Answers1

3

The batch fetch hint tells EclipseLink to use batching when it fetches the relationship, but doesn't influence when to fetch. Because the relationship is marked as lazy, it still waits for the relationship to be accessed, but when it does, it will use a batch query to return all associated entities for all Employee's brought in through the initial query. Join fetch is immediate because the information is brought in with the initial query, so there is no value in putting indirection in between.

If you want to load the relationship immediately, use

query.setHint(QueryHints.LOAD_GROUP_ATTRIBUTE, "addresses");
Chris
  • 20,138
  • 2
  • 29
  • 43