Batch Fetch is not working in EclipseLink

主宰稳场 提交于 2019-12-08 01:04:13

问题


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.


回答1:


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");


来源:https://stackoverflow.com/questions/25383764/batch-fetch-is-not-working-in-eclipselink

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!