Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Jpa Eclipsejpaexamples JPA setFirstResult Example

Related Tutorials


 
 

Share on Google+Share on Google+

JPA setFirstResult Example

Advertisement
In this section, you will learn how to use setFirstResult method in your jpa application.

JPA setFirstResult Example

     

In this section, you will learn how to use setFirstResult method in your jpa application. The setFirstResult(N) method is used to skip the first "N"  elements form the result set. Please note that the counting starts from the 0, so if you specify 2 then first 3 records will be skipped.

setFirstResult(int startingPosition): This is the method of Query interface. The setFirstResult method is used to set the position of first result for retrieving the data. It also return  same query instance. 

For this application you need the following files:

  1. Database Table: student
  2. Model Class: Student.java
  3. Main Class: JPASetFirstResult.java

Database Table: student

CREATE TABLE `student` ( 
`id` int(11) NOT NULL auto_increment, 
`sname` varchar(100) NOT NULL, 
`sroll` int(11) NOT NULL, 
`scourse` varchar(10) NOT NULL, 
PRIMARY KEY (`id`) 
)

Model Class: Student.java

/**

*/
package roseindia;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

/**
* @author Administrator
*
*/
@Entity
@Table(name="student")

public class Student {

@Id
@GeneratedValue
private int id;

/**
* @return the id
*/
public int getId() {
return id;
}

/**
* @param id the id to set
*/
public void setId(int id) {
this.id = id;
}

@Column(name="sname", length=100,nullable=false)
private String sname;

/**
* @return the sname
*/
public String getSname() {
return sname;
}

/**
* @param sname the sname to set
*/
public void setSname(String sname) {
this.sname = sname;
}

@Column(name="sroll",nullable=false)
private int sroll;

/**
* @return the sroll
*/
public int getSroll() {
return sroll;
}

/**
* @param sroll the sroll to set
*/
public void setSroll(int sroll) {
this.sroll = sroll;
}

@Column(name="scourse",length=10,nullable=false)
private String scourse;

/**
* @return the scourse
*/
public String getScourse() {
return scourse;
}

/**
* @param scourse the scourse to set
*/
public void setScourse(String scourse) {
this.scourse = scourse;
}

}

Main Class: JPASetFirstResult.java

/**

*

*/

package roseindia;

 

import java.util.Iterator;

import java.util.List;

 

import javax.persistence.EntityManager;

import javax.persistence.EntityManagerFactory;

import javax.persistence.EntityTransaction;

import javax.persistence.Persistence;

import javax.persistence.Query;

 

/**

* @author Administrator

*

*/

public class JPASetFirstResult {

 

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

 

EntityManagerFactory emf=Persistence.createEntityManagerFactory("jpa");

EntityManager em=emf.createEntityManager();

try{

EntityTransaction entr=em.getTransaction();

entr.begin();

Query query=em.createQuery("SELECT st FROM Student st");

query.setFirstResult(2);

List stuList=query.getResultList();

Iterator stuIterator=stuList.iterator();

while(stuIterator.hasNext()){

Student st=(Student)stuIterator.next();

System.out.print("sname:"+st.getSname());

System.out.print("sroll:"+st.getSroll());

System.out.print("scourse:"+st.getSname());

System.out.println();

}

entr.commit();

}

finally{

em.close();

}

}

}

Output:

log4j:WARN No appenders could be found for logger
(org.hibernate.cfg.annotations.Version).

log4j:WARN Please initialize the log4j system properly.

Hibernate: select student0_.id as id0_, student0_.scourse
as scourse0_, student0_.sname as sname0_, student0_.sroll
 as sroll0_ from student student0_

sname:Suman sroll:103 scourse:Suman

sname:Noor sroll:104 scourse:Noor

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 10, 2009

Related Tutorials

Discuss: JPA setFirstResult Example  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:6
Rajah
June 14, 2011
Example not complete

I see that the example does not include the configuration settings of persistence.xml which is a vital component for any starter in the JPA area the example is incomplete without the settings of persistence.xml and is also incomplete in what command line to run
prashant
July 4, 2011
jpa

example is goo i want this source code.
ravindra
August 23, 2011
good

it helps me to learn jpa...
Ananth
November 17, 2011
Exception

Hi while am try to run this program am getting this exception. Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named jpa at javax.persistence.Persistence.createEntityManagerFactory(Unknown Source) at javax.persistence.Persistence.createEntityManagerFactory(Unknown Source) at com.cognizant.Jpa.JPASetFirstResult.main(JPASetFirstResult.java:21)
Kevin Wright
December 9, 2012
Keep getting PersistenceException

Hi, I am trying your example, but keep getting: Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named jpa at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:69) at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47) at com.mycompany.TestJPADemo.App.main(App.java:15)
vijaya
January 3, 2013
java persistance api

thanks folks,it is very usefull to me
DMCA.com