[an error occurred while processing this directive]

Database Record findByName


In this section, you learn to develop a jpa application to find records according to its name (String value). Here we simply write a JPQL (Java Persistence Query Language) query in createQuery() method. This is provided by EntityManager interface. The createQuery() method returns a new query instance. Then Query interface provides a method "setParameter()" to set your conditions.

You need the following artifacts:

  1. Database table: student
  2. Model Class: Student.java
  3. Main Class: JPAFindByStudentName.java

Table: student

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

Model Class: Student.java

* To change this template, choose Tools | Templates
* and open the template in the editor.

package jpacrud;

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

@NamedQuery(name="readAllRecords",query="SELECT st FROM Student st"),
@NamedQuery(name="updateRecord",query="UPDATE Student st SET st.sname= ?1 WHERE st.sroll= ?2")
public class Student implements java.io.Serializable {

private int id;

public int getId() {
return id;

public void setId(int id) {
this.id = id;

private String sname;

public String getSname() {
return sname;

public void setSname(String sname) {
this.sname = sname;

private int sroll;

public int getSroll() {
return sroll;

public void setSroll(int sroll) {
this.sroll = sroll;

private String scourse;

public String getScourse() {
return scourse;

public void setScourse(String scourse) {
this.scourse = scourse;


Main Class: JPAFindByStudentName.java

createQuery(String jpqlString): This method creates an instance of Query to execute jpql (Java Persistence Query Language) statement.

jpqlString: "SELECT st FROM Student st WHERE st.sname= ?1"

setParameter(int position, Object val): This method binds an argument according to positional parameter. 

* To change this template, choose Tools | Templates
* and open the template in the editor.

package jpacrud;

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 JPAFindByStudentName {
public static void main(String arg[]){
EntityManagerFactory emf=Persistence.createEntityManagerFactory("netjpa");
EntityManager em=emf.createEntityManager();
EntityTransaction entr=em.getTransaction();
Query query=em.createQuery("SELECT st FROM Student st WHERE st.sname= ?1");
query.setParameter(1, "vinod");
List stList=query.getResultList();
Iterator stIterator=stList.iterator();
Student stu=(Student)stIterator.next();
System.out.print(" Roll:"+stu.getSroll());
System.out.println(" Course:"+stu.getScourse());
catch(Exception ex){



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_.sname=?
Name:Vinod Roll:101 Course:MCA
Name:Vinod Roll:103 Course:ccna
BUILD SUCCESSFUL (total time: 3 seconds)

Download Application


[an error occurred while processing this directive]