Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
JPA setMaxResults Example 
 

In this section, you will learn how to use the setMaxResults() method in your JPA application.

 

JPA setMaxResults Example

                         

In this section, you will learn how to use the setMaxResults() method in your JPA application. The setMaxResults function is used to set a limit to restrict the number of objects returned from database.

setMaxResults(int maxResult): This method is used with query instance for setting the maximum number of results to be fetched from database. 

To test the setMaxResults with example follow the following steps:

  1. Database Table: student, create student table.
  2. Model Class: Student.java, develop the model class.
  3. Main Class: JPASetMaxResult.java, develop the program to run the application.

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`) 
)

Then enter few records in student table.

Model Class: Student.java

Here is the code of the model class, which is mapped to student table in the database.

/**

*/
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: JPASetMaxResult.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 JPASetMaxResult {

/**
* @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 WHERE st.sroll > ?1");
query.setParameter(1, 100);
query.setMaxResults(3);
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.getSname());
System.out.print(" scourse:"+st.getSname());
System.out.println();
}
entr.commit();
}
finally{
em.close();
}
}

}

You can run the example from eclipse ide.

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_ where student0_.sroll>? limit ?

sname:Vinod sroll:Vinod scourse:Vinod

sname:Ravi sroll:Ravi scourse:Ravi

sname:Suman sroll:Suman scourse:Suman

                         

» View all related tutorials
Related Tags: c jpa application io name this app queries ie to e il section li use in no m out ca

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.