JPA Named Query

JPA Query is powerful that means it has more additional criteria can be include in run time during executing the query.

JPA Named Query


In this section, you will read about the JPA Query, its types and how to use the JPA Named Query in your JPA application. JPA Query is powerful that means it has more additional criteria can be include in run time during executing the query.

Types of query:

  1. Static query (Named query)
  2. Dynamic query

Here, we are going to define static query (Named query). Native query is helpful for me. Following reason:

  1. Named queries is a persistence provider might be able to pre-compile them. 
  2. Pre-compiled queries find bugs before deployment.
  3. Named queries are thread-safe. They are reused by many classes and instances.

For developing JPA Named Query, you need the following artifacts:

  1. Database Table: student
  2. Model Class:
  3. Main Class:

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, 

Model Class:

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

query="SELECT st FROM Student st WHERE st.sname= ?1 OR st.scourse= ?2")

public class Student {

private int id;

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

* @param id the id to set
public void setId(int 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;

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;

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:

createNamedQuery(String name): This method creates an instance of Query to execute a named query. The named query have Java Persistence Query Language or Native SQL.


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 JPANamedQuery {

* @param args
public static void main(String[] args) {
// TODO Auto-generated method stub

EntityManagerFactory emf=Persistence.createEntityManagerFactory("jpa");
EntityManager em=emf.createEntityManager();
EntityTransaction entr=em.getTransaction();
Query query=em.createNamedQuery("studentRecords");
query.setParameter(1, "vinod");
query.setParameter(2, "PHD");
List stList=query.getResultList();
Iterator stIterator=stList.iterator();
Student st=(Student);
System.out.print(" sname:"+st.getSname());
System.out.print(" sroll:"+st.getSroll());
System.out.print(" scourse:"+st.getScourse());



log4j:WARN No appenders could be found for logger

log4j:WARN Please initialize the log4j system properly.

Hibernate: select as id0_, student0_.scourse as
scourse0_, student0_.sname as sname0_, student0_.sroll as
sroll0_ from student student0_ where student0_.sname=? or

id:1 sname:Vinod sroll:101 scourse:MCA

id:3 sname:Suman sroll:103 scourse:PHD


Share on Google+Share on Google+

JPA Named Query

Posted on: March 22, 2005 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: JPA Named Query  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
July 1, 2011

what jar files are required in case of Jpaappls using Hibernate and toplink