Selecting a fetch strategy select u.* from USERS

Selecting a fetch strategy select u.* from USERS u where u.USER_ID = ? … Hibernate offers some algorithms that can prefetch User objects. The first optimization we now discuss is called batch fetching, and it works as follows: If one proxy of a Usermust be initialized, go ahead and initialize several in the same SELECT. In other words, if you already know that there are three Item instances in the persistence context, and that they all have a proxy applied to their seller association, you may as well initialize all the proxies instead of just one. Batch fetching is often called a blind-guess optimization, because you don t know how many uninitialized User proxies may be in a particular persistence context. In the previous example, this number depends on the number of Item objects returned. You make a guess and apply a batch-size fetching strategy to your User class mapping: You re telling Hibernate to prefetch up to 10 uninitialized proxies in a single SQL SELECT, if one proxy must be initialized. The resulting SQL for the earlier query and procedure may now look as follows: select items… select u.* from USERS u where u.USER_ID in (?, ?, ?) The first statement that retrieves all Item objects is executed when you list() the query. The next statement, retrieving three User objects, is triggered as soon as you initialize the first proxy returned by allItems.get(0).getSeller(). This query loads three sellers at once because this is how many items the initial query returned and how many proxies are uninitialized in the current persistence context. You defined the batch size as up to 10. If more than 10 items are returned, you see how the second query retrieves 10 sellers in one batch. If the application hits another proxy that hasn t been initialized, a batch of another 10 is retrieved and so on, until no more uninitialized proxies are left in the persistence context or the application stops accessing proxied objects. FAQ What is the real batch-fetching algorithm? You can think about batch fetching as explained earlier, but you may see a slightly different algorithm if you experiment with it in practice. It s up to you if you want to know and understand this algorithm, or if you trust Hibernate to do the
In case you need affordable webhost to host your website, our recommendation is ecommerce web host services.

Leave a Reply