Querying with criteria and example Dynamic fetching with (Web hosting plans)
Querying with criteria and example Dynamic fetching with criteria queries In HQL and JPA QL, you use the join fetch operation to eagerly fill a collection or to initialize an object that is mapped as lazy and would otherwise be proxied. You can do the same using the Criteria API: session.createCriteria(Item.class) .setFetchMode(”bids”, FetchMode.JOIN) .add( Restrictions.like(”description”, “%Foo%”) ); This query returns all Item instance with a particular collection and eagerly loads the bids collection for each Item. A FetchMode.JOIN enables eager fetching through an SQL outer join. If you want to use an inner join instead (rare, because it wouldn t return items that don t have bids), you can force it: session.createCriteria(Item.class) .createAlias(”bids”, “b”, CriteriaSpecification.INNER_JOIN) .setFetchMode(”b”, FetchMode.JOIN) .add( Restrictions.like(”description”, “%Foo%”) ); You can also prefetch many-to-one and one-to-one associations: session.createCriteria(Item.class) .setFetchMode(”bids”, FetchMode.JOIN) .setFetchMode(”seller”, FetchMode.JOIN) .add( Restrictions.like(”description”, “%Foo%”) ); Be careful, though. The same caveats as in HQL and JPA QL apply here: Eager fetching more than one collection in parallel (such as bids and images) results in an SQL Cartesian product that is probably slower than two separate queries. Limiting the resultset for pagination, if you use eager fetching for collections, is also done in-memory. However, dynamic fetching with Criteria and FetchMode is slightly different than in HQL and JPA QL: A Criteria query doesn t ignore the global fetching strategies as defined in the mapping metadata. For example, if the bids collection is mapped with fetch=”join” or FetchType.EAGER, the following query results in an outer join of the ITEM and BID table: session.createCriteria(Item.class) .add( Restrictions.like(”description”, “%Foo%”) ); The returned Item instances have their bids collections initialized and fully loaded. This doesn t happen with HQL or JPA QL unless you manually query with LEFTJOINFETCH (or, of course, map the collection as lazy=”false”, which results in a second SQL query).
From our experience, we are can tell you that you can find a reliable and cheap webhost service at Java Web Hosting services.