Basic Concepts
Before diving into greater details lets quickly walk through some basic classes and what they mean in JPA.
- EntityManager - A class that manages the persistent state(or lifecycle) of an entity.
- Persistence Unit - is a named configuration of entity classes.
- Persistence Context - is a managed set of entity instances. The entities classes are part of the Persistence Unit configurations.
- Managed Entities - an entity instance is managed if it is part of a persistence context and that Entity Manager can act upon it.
A Very Simple Example
Let us create a very simple Employee table.
To check, let us query the table using simple SQL language. Here's the result
Using JPA
First, create the persistence.xml file. This xml file contains information regarding the database server your JPA will connect to, including the user name and the password.
Important thing to notice here is the <persistence-unit> tag. A persistence.xml can have as many persistence unit depending on the number of database you are connecting to. (1) Note that after that, there are several properties with their corresponding names and values. The database driver and the URL is very much dependent on the vendor of the RDBMS you are using. And as the name implies, values for the user/password is the one you use for accessing your RDBMS.
- To get things going, first declare an object of EntityManagerFactory:
- Initialize the EntityManagerFactor object using Persistence object's static method createEntityManagerFactory(String persistenceUnit); emf = Persistence.createEntityManagerFactory("AllanJPA");
- Initialize the EntityManager object by calling createEntityManager() method of the EntityManagerFactory em = emf.createEntityManager();
private EntityManager em; private EntityManagerFactory emf;
There you go, that's the basic initialization for JPA. Now let us now try to see how to create Query objects and run a JQL using JPA. Note that for every transaction you need create a EntityTransaction object first. It is done in this manner:
em.getTransaction().begin();
Afterwards, you can now create your Query object using the following statements:
List
Full source code will look something like these:
The main() method here to run retrieve():
And here is the output based on the table we have queried earlier:
Let us try to create instances of Employee class, here's the class:
And the Employee class is mapped to a table in orm.xml
The objects declaration for the Employee class will look something like this:
Employee empRag = new Employee("Raggedy", "Anne", "Dressmaker", 14000.00, sdf.parse("22/06/2010"));
Afterwards, as mentioned earlier, we must have a transaction:
// Create Transaction
After that, we can call persist() method to persist (save) our data to the database:
em.persist(empRag);
The actual code snippet:
When you try to run the code, it would look something like:
Upon checking the database we have:
Persisting
Employee empAlbert = new Employee("Big", "Albert", "Musician", 122000.00, sdf.parse("17/06/2013"));
Employee empRasel = new Employee("Rasel", "Case", "Pilot", 140000.00, sdf.parse("14/04/2012"));
Employee empDavid = new Employee("David", "Levinson", "Technician", 256000.00, sdf.parse("25/12/2014"));
em.getTransaction().begin();
// Persist the objects
em.persist(empAlbert);
em.persist(empRasel);
em.persist(empDavid);