tailieunhanh - Java Persistence with Hibernate 2nd phần 8
Prefetching bằng cách sử dụng một subselect là một tối ưu hóa mạnh mẽ, chúng tôi sẽ chỉ cho bạn một vài chi tiết về nó sau đó, khi chúng tôi đi bộ qua một kịch bản điển hình. Subselect lấy, tại thời điểm văn bản, chỉ có các bộ sưu tập, không phải cho các proxy thực thể. | 578 CHAPTER 13 Optimizing fetching and caching select i. from ITEM i select b. from BID b where in select from ITEM i In annotations you again have to use a Hibernate extension to enable this optimization @OneToMany @ private Set Bid bids new HashSet Bid Prefetching using a subselect is a powerful optimization we ll show you a few more details about it later when we walk through a typical scenario. Subselect fetching is at the time of writing available only for collections not for entity proxies. Also note that the original query that is rerun as a subselect is only remembered by Hibernate for a particular Session. If you detach an Item instance without initializing the collection of bids and then reattach it and start iterating through the collection no prefetching of other collections occurs. All the previous fetching strategies are helpful if you try to reduce the number of additional SELECTs that are natural if you work with lazy loading and retrieve objects and collections on demand. The final fetching strategy is the opposite of on-demand retrieval. Often you want to retrieve associated objects or collections in the same initial SELECT with a JOIN. Eager fetching with joins Lazy loading is an excellent default strategy. On other hand you can often look at your domain and data model and say Every time I need an Item I also need the seller of that Item. If you can make that statement you should go into your mapping metadata enable eager fetching for the seller association and utilize SQL joins class name Item table ITEM . many-to-one name seller class User column SELLER_ID update false fetch join class Hibernate now loads both an Item and its seller in a single SQL statement. For example Selecting a fetch strategy 579 Item item Item new Long 123 This operation triggers the following SQL SELECT select i. u. from ITEM i left outer join .
đang nạp các trang xem trước