Florida web design - CHAPTER 14 Querying with HQL and JPA QL

CHAPTER 14 Querying with HQL and JPA QL Starting from the fortieth object, you retrieve the next 20 objects. Note that there is no standard way to express pagination in SQL Hibernate knows the tricks to make this work efficiently on your particular database. You can even add this flexible pagination option to an SQL query. Hibernate will rewrite your SQL for pagination: Query sqlQuery = session.createSQLQuery(”select {u.*} from USERS {u}”) .addEntity(”u”, User.class); sqlQuery.setFirstResult(40); sqlQuery.setMaxResults(20); You may use the method-chaining coding style (methods return the receiving object instead of void) with the Query and Criteria interfaces, rewriting the two previous examples as follows: Query query = session.createQuery(”from User u order by u.name asc”) .setMaxResults(10); Criteria crit = session.createCriteria(User.class) .addOrder( Order.asc(”name”) ) .setFirstResult(40) .setMaxResults(20); Chaining method calls is less verbose and is supported by many Hibernate APIs. The Java Persistence query interfaces also support pagination and method chaining for JPA QL and native SQL queries with the javax.persistence.Query interface: Query query = em.createQuery(”select u from User u order by u.name asc”) .setFirstResult(40) .setMaxResults(20); Next in preparing your query is the setting of any runtime parameters. Considering parameter binding Without runtime parameter binding, you have to write bad code: String queryString = “from Item i where i.description like ‘” + search + “‘”; List result = session.createQuery(queryString).list(); You should never write this code because a malicious user could search for the following item description that is, by entering the value of search in a search dialog box as foo’ and callSomeStoredProcedure() and ‘bar’ = ‘bar
You want to have a cheap webhost for your apache application, then check apache web hosting services.

Leave a Reply