We already discussed the fundamentals of JPA earlier. We also discussed the select query in JPA which returns object(s) which satisfies the mentioned condition in an earlier chapter. In this chapter we are looking into the find method in JPA which can be used to find a particular object based on a primary key value specified.
The find method in jpa example
Here Open JPA is using as implementation.The steps in creating an Open JPA project is already discussed in the earlier chapter.We need the same set up here also.The same entity class and persistence.xml is using here also.In this chapter we are searching for a particular Student object using the find() method EntityManager .We are trying to acess an object which is already existing in the table.
Student.java
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity(name = "Student")
@Table(name = "student")
public class Student implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name;
private String level;
public Student() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
@Column(name = "name")
public void setName(String name) {
this.name = name;
}
public String getLevel() {
return level;
}
@Column(name = "level")
public void setLevel(String level) {
this.level = level;
}
public String toString() {
return "ID = " + getId() + " ; Name : = " + getName() + " ; Level : = "
+ getLevel();
}
}
persistence.xml
We already inserted the data in the previous example. We are retrieving a Student object. In this case the primary key is 1. Since Strategy.AUTO is used in the entity , we cannot guarantee the existence of an object with primary key 1. We need to verify the primary key values of existing objects in table and we need to give the appropriate primary key to get a valid object as output.
TransactionSample.java
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import com.jpa.entity.Student;
public class TransactionSample {
public void retrieveObjects() {
EntityManagerFactory entityManagerFactory = Persistence
.createEntityManagerFactory("OpenJPASample");
EntityManager entitymanager = entityManagerFactory
.createEntityManager();
if (null != entitymanager) {
Student student = new Student();
student.setId(1);
Student student1 = entitymanager.find(Student.class, 1);
if (null != student1) {
System.out.println("Retrived object .. " + student1);
} else {
System.out.println("No object found...");
}
}
}
public static void main(String[] args) {
TransactionSample sample = new TransactionSample();
sample.retrieveObjects();
}
}
Output
Run the TransactionSample.java. Based on the primary key value given , the object will be retrieved.
See Related Posts
JPQL
Caching
Locking in JPA
Relational Mappings in JPA