Retrieving or finding an Entity using JPA

/, JPA/Retrieving or finding an Entity using JPA
Note:
Maven dependencies used all over JPA tutorials same. Please follow "Setting up Project" section to understand about project configuration.
persistence.xml and other code files reused from How to Persist an Entity in data base using JPA?
Each Sample application from download includes SQL file also.

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 SELECT * 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 see this tutorial JPA Entity lifecycle

Persistence transition or state change when EntityManager’s find() called :

Finding an Entity Code :

Console Output :

Download Application – JPA-Entity-Retrieve.zip (10 KB)
2017-08-31T12:42:45+00:00Categories: Hibernate, JPA|Tags: , , , |2 Comments

2 Comments

  1. Adrian Vrabie November 10, 2016 at 3:13 am - Reply

    good read! thank you!

Leave A Comment