Wednesday April 08, 2009
TOTD #78: GlassFish, EclipseLink, and MySQL efficient pagination using LIMIT
EclipseLink
JPA replaces TopLink
Essentials as the JPA implementation in GlassFish v3. One
of the benefits of using EclipseLink is that it provides efficient
pagination support for the MySQL database by generating
native SQL statements such as "SELECT ... FROM <table>
LIMIT <offset>, <rowcount>".
The MySQL LIMIT clause definition
says:
The
LIMIT clause can be used to constrain the number of rows returned by
the SELECT statement. LIMIT takes one or two numeric arguments, which
must both be non-negative integer constants (except when using prepared
statements).
With two arguments, the
first argument specifies
the offset of the first row to return, and the second specifies the
maximum number of rows to return. The offset of the initial row is 0
(not 1):
SELECT * FROM tbl LIMIT
5,10; # Retrieve rows 6-15
So instead of fetching all rows from the database and then filtering
from row 6-15, only rows 6 through 15 are fetched.
This TOTD (Tip
Of The Day) explains how to
create a JPA Persistence Unit for sakila
(MySQL sample database) using NetBeans,
use EclipseLink as the Persistence Provider, and then write a JPA query
to leverage the pagination support - all on GlassFish v3.

| <properties> <property name="eclipselink.logging.level" value="FINE"/> </properties> |
| ./asadmin create-jdbc-connection-pool
--datasourceclassname
com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource --property
user=duke:password=glassfish:ServerName=localhost:portNumber=3306:databaseName=sakila
jdbc-mysql-pool |
| ./asadmin create-jdbc-resource --connectionpoolid jdbc-mysql-pool jndi/sakila |
| @PersistenceUnit EntityManagerFactory emf; |
|
EntityManager em = emf.createEntityManager(); response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { int startRow = Integer.valueOf(request.getParameter("start_row")); int howMany = Integer.valueOf(request.getParameter("how_many")); Query q = em.createNamedQuery("Film.findAll"); q.setFirstResult(startRow); q.setMaxResults(startRow + howMany); for (Object film : q.getResultList()) { out.print(((Film)film).toString() + "<br/>"); } } finally { out.close(); } |

| [#|2009-04-07T14:01:12.815-0700|FINE|glassfish|org.eclipse.persistence.session.file:
/Users/arungupta/tools/glassfish/v3/b43/glassfishv3/glassfish/domains/domain1/applications/Pagination/WEB-INF/classes/-PaginationPU.sql|
_ThreadID=15;_ThreadName=Thread-1;ClassName=null;MethodName=null;|SELECT
film_id AS film_id1, special_features AS special_features2, last_update
AS last_update3, rental_duration AS rental_duration4, release_year AS
release_year5, title AS title6, description AS description7,
replacement_cost AS replacement_cost8, length AS length9, rating AS
rating10, rental_rate AS rental_rate11, language_id AS language_id12,
original_language_id AS original_language_id13 FROM film LIMIT ?, ? bind => [1, 11]|#] |
| [#|2009-04-07T17:00:34.210-0700|FINE|glassfish|org.eclipse.persistence.session.file: /Users/arungupta/tools/glassfish/v3/b43/glassfishv3/glassfish/domains/domain1/applications/Pagination/WEB-INF/classes/-PaginationPU.sql| _ThreadID=15;_ThreadName=Thread-1;ClassName=null;MethodName=null;|SELECT film_id, special_features, last_update, rental_duration, release_year, title, description, replacement_cost, length, rating, rental_rate, language_id, original_language_id FROM film|#] |
Posted by Arun Gupta in web2.0 | Comments[6]
|
|
|
|
|
Today's Page Hits: 2466
Total # blog entries: 994
| « November 2009 | ||||||
| Sun | Mon | Tue | Wed | Thu | Fri | Sat |
|---|---|---|---|---|---|---|
1 | 2 | 4 | 6 | 7 | ||
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | |||||
| Today | ||||||
Posted by Arun Gupta's Blog on April 21, 2009 at 09:53 PM PDT #
thank you
Posted by neon on April 26, 2009 at 02:10 PM PDT #
thank you
Posted by neon tabela on April 26, 2009 at 02:10 PM PDT #
thank youu
Posted by oyun zamani on May 22, 2009 at 02:37 PM PDT #
thanks,nice
Posted by HD LCD monitor on June 26, 2009 at 07:28 AM PDT #
I can't make it work, are sure it's 1005 right?
Posted by how to grow taller on August 27, 2009 at 02:13 PM PDT #