JPASetFirstResult.java
----------------------------------------------------------------------------
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;
public class JPASetFirstResult {
public static void main(String[] args) {
EntityManagerFactory emf=null;
EntityManager em=null;
try{
emf=Persistence.createEntityManagerFactory("test");
em=emf.createEntityManager();
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();
}catch(Exception e){
System.out.println("Exception ==> "+e);
}finally{
if(em != null)
em.close();
}
}
}
--------------------------------------------------------------------------
persistence.xml.....Inside "META-INF"...folder
------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
xmlns="
http://java.sun.com/xml/ns/persistence"
xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="test" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>Student</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"></property>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"></property>
<property name="hibernate.show_sql" value="true"></property>
<property name="hibernate.connection.username" value="test"></property>
<property name="hibernate.connection.password" value="test"></property>
<property name="hibernate.connection.url" value="jdbc:mysql:///test"></property>
</properties>
</persistence-unit>
</persistence>
------------------------------------------------------------
Student.java as below
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;
import java.io.Serializable;
@Entity
@Table(name="student")
public class Student implements Serializable {
@Id
@GeneratedValue
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name="sname", length=100,nullable=false)
private String sname;
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
@Column(name="sroll",nullable=false)
private int sroll;
public int getSroll() {
return sroll;
}
public void setSroll(int sroll) {
this.sroll = sroll;
}
@Column(name="scourse",length=10,nullable=false)
private String scourse;
public String getScourse() {
return scourse;
}
public void setScourse(String scourse) {
this.scourse = scourse;
}
}
----------------------------------------------------
[Message sent by forum member 'jpa_baby_2010']
http://forums.java.net/jive/thread.jspa?messageID=484092