Database Record findById

This section, you will see how to develop a jpa application to find records according to its id( primary key).

Database Record findById

Database Record findById

     

This section, you will see how to develop a jpa application to find records according to its id( primary key). EntityManager interface gives a method "find(Class class_name.class, Object primaryKey). This method finds data by primary key.

You need the following artifacts:

  1. Database table: student
  2. Model Class: Student.java
  3. Main Class: JPAFindById.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, 
PRIMARY KEY (`id`) 

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
*/
@Entity
@Table(name="student")

@NamedQueries({
@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 {

@Id
@GeneratedValue
private int id;

public int getId() {
return id;
}

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

@Column(name="sname",length=40,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=true)
private String scourse;

public String getScourse() {
return scourse;
}

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

}

Main Class: JPAFindById.java

find(Class class_name.class, Object primaryKey): Here we put the class name "Student.class" and primary key "1". The find method to find data by primary key "1". If primary key is found then record will be displayed otherwise not.

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

package jpacrud;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

/**
*
* @author Administrator
*/
public class JPAFindById {
public static void main(String arg[]){
EntityManagerFactory emf=Persistence.createEntityManagerFactory("netjpa");
EntityManager em=emf.createEntityManager();
try{
EntityTransaction entr=em.getTransaction();
entr.begin();
Student stu=em.find(Student.class, 1);
System.out.print("Name:"+stu.getSname());
System.out.print(" Roll:"+stu.getSroll());
System.out.println(" Course:"+stu.getScourse());
entr.commit();
}
catch(Exception e){
System.out.println(e.getMessage());
}
finally{
em.close();
}

}

}

Output:

init:
deps-jar:
Compiling 1 source file to C:\NetBeansProjects\JPACRUD\build\classes
compile-single:
run-single:
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_0_,
student0_.scourse as scourse0_0_, student0_.sname as sname0_0_,
student0_.sroll as sroll0_0_ from student student0_ where student0_.id=?
Name:Vinod Roll:101 Course:MCA
BUILD SUCCESSFUL (total time: 4 seconds)

Download Application