1

We're using JPA, and when a collection of objects returns from a query, a separate query is executed for each "child" object related through a foreign key.

For example, in our Authorization entity class, we have the following Client object mapped:

@JoinColumn(name = "clientId", referencedColumnName = "clientId")
@ManyToOne (fetch = FetchType.LAZY)
@NotNull(groups = Default.class)
private Client client;

When 10 Authorizations are returned, 10 Client queries are executed. In TopLink, I was able to bring this number to one with the ReadAllQuery class's addBatchReadAttribute() method. According to the TopLink docs, "when any of the batched parts is accessed, the parts will all be read in a single query, this allows all of the data required for the parts to be read in a single query instead of (n) queries."

This worked perfectly, giving us a single query using an IN clause with 10 ids.

What I read about re: JPA pointed me toward a batch join or something like:

hints = {@QueryHint(name = "eclipselink.batch", value = "p.client"), ... 

This strategy helps reduce the number of queries,

  • but it also gave me more joins, possibly slowing things down (?) on some queries
  • and it didn't seem to help as drastically as the TopLink call.

Is there a way to get the strategy that uses a single query with IN in the WHERE clause?

Thanks in advance. Dave

4

2 回答 2

1

EclipseLink 支持多种类型的批处理获取。

见, http://java-persistence-performance.blogspot.com/2010/08/batch-fetching-optimizing-object-graph.html

于 2011-09-28T14:17:41.957 回答
1

在内部,QueryHint "eclipselink.batch" 被转换为 addBatchAttribute() 所以你看到的行为应该是相同的。您创建的 JPQL 是否产生与本机 TopLink API 相同的查询?您有可能在 JPQL 中有 Fetch 或其他连接吗?

于 2011-09-28T13:05:39.643 回答