JPA Named Queries

In this section, you will know about the JPA Named Queries and how to use in JPA application.

JPA Named Queries

JPA Named Queries

     

In this section, you will know about the JPA Named Queries and how to use in JPA application.

@NamedQueries: If you need to use more than one @NamedQuery, Then you must be use @NamedQueries annotation.

@NamedQueries({
@NamedQuery(name="allStudentRecords",query="SELECT st FROM Student st WHERE st.sroll > ?1"),
@NamedQuery(name="updateStudentRecords", query="UPDATE Student st SET st.sname= ?1 WHERE st.sname= ?2")
})

For developing JPA Named Queries, you need the following files:

  1. Database Table: student
  2. Model Class: Student.java
  3. Main Class: JPANamedQueries.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")

@NamedQueries({
@NamedQuery(name="allStudentRecords",query="SELECT st FROM Student st WHERE st.sroll > ?1"),
@NamedQuery(name="updateStudentRecords", query="UPDATE Student st SET st.sname= ?1 WHERE st.sname= ?2")
})

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: JPANamedQueries.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 JPANamedQueries {

/**
* @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();
//first query
Query query=em.createNamedQuery("allStudentRecords");
query.setParameter(1, 100);
List stList=query.getResultList();
Iterator stIterator=stList.iterator();
while(stIterator.hasNext()){
Student st=(Student)stIterator.next();
System.out.print("sname:"+st.getSname());
System.out.print(" sroll:"+st.getSroll());
System.out.print(" scourse:"+st.getScourse());
System.out.println();
}
//second query 
Query update_query=em.createNamedQuery("updateStudentRecords");
update_query.setParameter(1, "Suman Saurabh");
update_query.setParameter(2, "Suman");
int updateData=update_query.executeUpdate();
System.out.println(updateData +" record updated");
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_ where student0_.sroll>?

sname:Vinod sroll:101 scourse:MCA

sname:Ravi sroll:102 scourse:M.Tech.

sname:Suman sroll:103 scourse:PHD

Hibernate: update student set sname=? where sname=?

1 record updated