Optimization of Hibernate (Optimization guidelines---cartesian product problem)
Cartesian product problem
In contrast to the n+1 query problem, a SELECT statement that captures too much data. If you try to crawl a few "parallel" sets, this Cartesian product problem will not occur. Suppose you have decided to apply a global fetch= "join" setting to a bids collection of item. The item class has other collections (for example, images). Also assume that you decide that all pictures of each item must be loaded immediately through the fetch= "join" policy:
<class name= "Item" > ... <set name= "bids" inverse= "true" fetch= "join" > <key column= "item_id"/> < One-to-many class= "Bid"/> </set> <set name= "Images" fetch= "join" > <key column= "item_id"/> < Composite-element class= "Image" > ... </set> </class>
If you map two parallel collections (same as their own entities) through an immediate external join crawl strategy, and load all the item objects, hibernate executes a SQL SELECT that creates the product of the two collections:
Select item.*,bid.*. Image.* from item left OUTER join BID BID on item. item_id = bid. item_id left OUTER join Item_image IMAGE on ITEM. item_id = image. item_id
This result set contains a large amount of redundant data.
If you map this parallel collection with fetch= "Subselect", you get 3 queries: This is the recommended optimization for a parallel set. However, there are exceptions to every rule. As long as the collection is small, the product is likely to be an acceptable crawl strategy. Note that parallel single value associations that are immediately crawled through an external join select Do not generally produce a product (that is, when using fetch= "join", in the case of <many-to-one>,<one-to-one> these single value associations, does not produce Cartesian product, if used in the set, there will be Cartesian product phenomenon. )
Finally, although Hibernate lets you create a Cartesian product in 2 (or even more) parallel collections by fetch= "Join", it throws an exception if you attempt to enable fetch= "join" on a parallel <bag> set. The result set of the product cannot be converted into a package collection because Hibernate cannot know which rows contain valid duplicates and which rows do not contain them. If you use a package collection, you do not enable the crawl policy that causes the product. Parallel instant crawl to the package set is crawled using a subquery or an immediate two-level query.