CHAPTER 13 Optimizing fetching and caching Global and (Web design templates)
CHAPTER 13 Optimizing fetching and caching Global and dynamic fetching strategies help you to solve the n+1 selects and Cartesian product problems. Hibernate offers another option to initialize a proxy or a collection that is sometimes useful. Forcing proxy and collection initialization A proxy or collection wrapper is automatically initialized whenever any of its methods are invoked (except for the identifier property getter, which may return the identifier value without fetching the underlying persistent object). Prefetching and eager join fetching are possible solutions to retrieve all the data you d need. You sometimes want to work with a network of objects in detached state. You retrieve all objects and collections that should be detached and then close the persistence context. In this scenario, it s sometimes useful to explicitly initialize an object before closing the persistence context, without resorting to a change in the global fetching strategy or a different query (which we consider the solution you should always prefer). You can use the static method Hibernate.initialize() for manual initialization of a proxy: Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); Item item = (Item) session.get(Item.class, new Long(1234)); Hibernate.initialize( item.getSeller() ); tx.commit(); session.close(); processDetached( item.getSeller() ); … Hibernate.initialize() may be passed a collection wrapper or a proxy. Note that if you pass a collection wrapper to initialize(), it doesn t initialize the target entity objects that are referenced by this collection. In the previous example, Hibernate.initalize( item.getBids() ) wouldn t load all the Bid objects inside that collection. It initializes the collection with proxies of Bid objects! Explicit initialization with this static helper method is rarely necessary; you should always prefer a dynamic fetch with HQL or Criteria. Now that you know all the options, problems, and possibilities, let s walk through a typical application optimization procedure.
We recommend cheap and reliable webhost to host and run your web applications: Coldfusion Web Hosting services.