Querying with criteria and example to work with (Web file server)

Querying with criteria and example to work with these APIs, and how to express selection, restriction, joins, and projection. We assume that you ve read the previous chapter and that you know how these operations are translated into SQL. Even if you decide to use the Criteria and Example APIs as your primary way to write queries, keep in mind that HQL and JPA QL are always more flexible due to their string-based nature. Let s start with some basic selection and restriction examples. 15.1.1 Basic criteria queries The simplest criteria query looks like this: session.createCriteria(Item.class); It retrieves all persistent instances of the Itemclass. This is also called the root entity of the criteria query. Criteria queries also support polymorphism: session.createCriteria(BillingDetails.class); This query returns instances of BillingDetails and its subclasses. Likewise, the following criteria query returns all persistent objects: session.createCriteria(java.lang.Object.class); The Criteria interface also supports ordering of results with the addOrder() method and the Order criterion: session.createCriteria(User.class) .addOrder( Order.asc(”lastname”) ) .addOrder( Order.asc(”firstname”) ); You don t need to have an open Session to create a criteria object; a Detached- Criteria can be instantiated and later attached to a Session for execution (or to another Criteria as a subquery): DetachedCriteria crit = DetachedCriteria.forClass(User.class) .addOrder( Order.asc(”lastname”) ) .addOrder( Order.asc(”firstname”) ); List result = crit.getExecutableCriteria(session).list(); Usually you want to restrict the result and don t retrieve all instances of a class. Applying restrictions For a criteria query, you must construct a Criterion object to express a constraint. The Restrictions class provides factory methods for built-in Criterion types. Let s search for User objects with a particular email address:
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision J2ee Web Hosting services.

Leave a Reply