Retrieving or Finding an Entity
- Once an entity in the database,
Student student = entintyManager.find(Student.class, 1L);
this line will retrieve student entity where identifier value is 1. This is equal to querySELECT * FROM STUDENT WHERE ID=1;
- what happens if the object has been removed or we supplied wrong id by accident? then the results will be simply
null
. - It’s just one line, but what we need to understand really is, Persistence transition or State of Student entity.
Note:
To understand JPA entity life cycle completely read JPA Entity lifecycle
Persistence transition or state change when EntityManager’s find() called
- On successful call
entintyManager.find(Student.class, 1L);
student entity state become persistent in the persistence context. - Persistent state instances are always associated with persistence-context and transnational.
- In this state if you change any value of student object using
student
reference, it will be reflected in persistence context.
Technologies Used in example :
- JPA 2.1
- Hibernate 5.2.6
- MySql 8.0
- Maven 3
- Spring Tool Suite (STS) 3.9.8
- Java 1.8
Mapping Studen.java class
@Entity(name="STUDENT") public class Student { @Id @GeneratedValue(strategy=GenerationType.AUTO, generator="native") @GenericGenerator(name = "native", strategy = "native") @Column(name = "ID") private Long studentId; @Column(name = "FNAME") private String firstName; @Column(name = "LNAME") private String lastName; @Column(name = "CONTACT_NO") private String contactNo; // setters and getters
Finding an Entity Code Example 1 :
public static void main( String[] args ) { EntityManagerFactory emf = null; EntityManager entityManager = null; EntityTransaction transaction = null; try{ emf = Persistence.createEntityManagerFactory("jbd-pu"); entityManager = emf.createEntityManager(); transaction = entityManager.getTransaction(); transaction.begin(); /* When ever EntityManager's find() calls the state of Student instace will be "Persitent" */ Student student = entityManager.find(Student.class, 2L); System.err.println("Student First Name: "+student.getFirstName()); transaction.commit(); }catch(Exception e){ transaction.rollback(); }finally{ entityManager.close(); emf.close(); } }
Output :
INFO - HHH000397: Using ASTQueryTranslatorFactory Hibernate: select student0_.ID as ID1_0_0_, student0_.CONTACT_NO as CONTACT_2_0_0_, student0_.FNAME as FNAME3_0_0_, student0_.LNAME as LNAME4_0_0_ from STUDENT student0_ where student0_.ID=? Student First Name: Arnold INFO - HHH000030: Cleaning up connection pool [jdbc:mysql://localhost:3306/jpa_JBD]
Finding an Entity Code Example 2 :
In the following example once you retrieve entity change the value of first name, you will notice that value will be reflected in persistence context.
public class App { public static void main( String[] args ) { EntityManagerFactory emf = null; EntityManager entityManager = null; EntityTransaction transaction = null; try{ emf = Persistence.createEntityManagerFactory("jbd-pu"); entityManager = emf.createEntityManager(); transaction = entityManager.getTransaction(); transaction.begin(); /* When ever EntityManager's find() calls the state of Student instace will be "Persitent" */ Student student = entityManager.find(Student.class, 2L); System.out.println("Student First Name: "+student.getFirstName()); student.setFirstName("Peter"); // value reflects in persistence context student = entityManager.find(Student.class, 2L); System.out.println("Student First Name: "+student.getFirstName()); transaction.commit(); }catch(Exception e){ transaction.rollback(); }finally{ entityManager.close(); emf.close(); } } }
Output :
You will see like following output in console.
Hibernate: select student0_.ID as ID1_0_0_, student0_.CONTACT_NO as CONTACT_2_0_0_, student0_.FNAME as FNAME3_0_0_, student0_.LNAME as LNAME4_0_0_ from STUDENT student0_ where student0_.ID=? Student First Name: Arnold Student First Name: Peter Hibernate: update STUDENT set CONTACT_NO=?, FNAME=?, LNAME=? where ID=?
Download Application – JPA-Entity-Retrieve.zip (10 KB)