Total members 11890 |It is currently Mon May 20, 2024 5:50 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





it is possible to make a paging for your JPA Query result.You can set the max results (which is the size of page ) ,and set the first result (which is the row to start from (pagesize*currentpage) ).Follow the example bellow .

Code:

package com
.codemiles.jpa;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

/**
 * @author codemiles.
 */
public abstract class JPAService {
    private 
EntityManager entityManager;
    private 
int pageSize;

    public List<
PersonfindAllPersons(String Id) {
    
EntityManagerFactory factory Persistence
            
.createEntityManagerFactory("persistenceUnitName");
    
entityManager factory.createEntityManager();
    
entityManager.getTransaction().begin();
    List<
PersonlistPersons entityManager.createQuery(
            
"SELECT p FROM Person p").getResultList();
    
entityManager.getTransaction().commit();
    
entityManager.close();
    
factory.close();
    if (
listPersons == null) {
        
System.out.println("No persons found . ");
    } else {
        for (
Person person listPersons) {
        
System.out.print("Person name= " person.getName()
                + 
", gender" person.getGender() + ", birthday="
                
person.getBirthday());
        }
    }

    return 
listPersons;
    }

    public List<
PersonfindAllPersonsWithPaging(int pagenumber) {
    
EntityManagerFactory factory Persistence
            
.createEntityManagerFactory("persistenceUnitName");
    
entityManager factory.createEntityManager();
    
entityManager.getTransaction().begin();
     
Query newqueryentityManager.createQuery(
            
"SELECT p FROM Person p");
     
newquery.setFirstResult(pagenumber*pageSize);
     
newquery.setMaxResults(pageSize);
     
    List<
PersonlistPersons  newquery.getResultList();
    
entityManager.getTransaction().commit();
    
entityManager.close();
    
factory.close();
    
    return 
listPersons;
    
    }

    public 
void setPageSize(int pageSize) {
    
this.pageSize pageSize;
    }

    public 
int getPageSize() {
    return 
pageSize;
    }

}

 




Person entity :
Code:

package com
.codemiles.jpa;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@
Entity
@Table(name="PERSON")
public class 
Person implements java.io.Serializable{
    
/**
     * 
     */
    
private static final long serialVersionUID 1L;
    @
Id
    
private String Id;
    private 
String name;
    private 
Date birthday;
    private 
String gender;
    
    @
OneToOne(mappedBy="person"
    private 
Bed bed;
    
    public 
String getId() {
        return 
Id;
    }
    public 
void setId(String id) {
        
Id id;
    }
    @
Column(name="NAME")
    public 
String getName() {
        return 
name;
    }
    public 
void setName(String name) {
        
this.name name;
    }
    @
Column(name="BIRTHDAT")
    public 
Date getBirthday() {
        return 
birthday;
    }
    public 
void setBirthday(Date birthday) {
        
this.birthday birthday;
    }
    @
Column(name="GENDER")
    public 
String getGender() {
        return 
gender;
    }
    public 
void setGender(String gender) {
        
this.gender gender;
    }
    public 
void setBed(Bed bed) {
    
this.bed bed;
    }
    public 
Bed getBed() {
    return 
bed;
    }
 
    
}

 




_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : paging JPA Query result
 Store query result in an Object in php     -  
 loop to read all query result in php     -  
 Paging in Java     -  
 XML Paging Code     -  
 Paging problem in php     -  
 EJB-QL IN where query     -  
 using parameter with name query     -  
 SQL AND OR in WHERE query Commands     -  
 select query example in php     -  
 more than one parameter with name query     -  



Topic Tags

Java JPA






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com