JPA Database Connection Example

In this example, we will see the steps to connect to the database using JPA.

In JPA, a connection to a database is represented by an EntityManager instance, which also provides functionality for performing operations on a database. 

Steps to connect to the database

Step 1: Define persistence.xml file
Step 2: Obtaining an EntityManagerFactory
Step 3: Obtaining an EntityManager
Step 4: Using an EntityTransaction

Step 5: Closing EntityManagerFactory and EntityManager Resources

Step 1: Define persistence.xml file

In JPA, the persistence.xml file is the central piece of configuration. That makes it one of the most important files of your persistence layer.
<persistence
    xmlns="http://xmlns.jcp.org/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
             http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
 version="2.1">
    <persistence-unit name="PERSISTENCE">
        <description> Hibernate JPA Configuration Example</description>
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <class>net.javaguides.hibernate.entity.Student</class>
        <properties>
            <property name="javax.persistence.jdbc.driver"
    value="com.mysql.cj.jdbc.Driver" />
            <property name="javax.persistence.jdbc.url"
    value="jdbc:mysql://localhost:3306/hibernate_db" />
            <property name="javax.persistence.jdbc.user" value="root" />
            <property name="javax.persistence.jdbc.password"
    value="root" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.hbm2ddl.auto" value="create-drop" />
        </properties>
    </persistence-unit>
</persistence>
Step 2: Obtaining an EntityManagerFactory

Let's create an instance of EntityManagerFactory class using Persistence class.
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("PERSISTENCE");
Step 3: Obtaining an EntityManager

Once we have an EntityManagerFactory we can easily obtain an EntityManager instance:
EntityManager entityManager = entityManagerFactory.createEntityManager();
  • EntityManager - An EntityManager is an interface
  • createEntityManager() method - It creates new application-managed EntityManager
Step 4: Using an EntityTransaction

Given an EntityManager, entityManager, it is very easy to begin a transaction:
entityManager.getTransaction().begin();
  • getTransaction() method - This method returns the resource-level EntityTransaction object.
  • begin() method - This method is used to start the transaction.
Commit the transaction:
entityManager.getTransaction().commit();
Step 5: Closing EntityManagerFactory and EntityManager Resources


The EntityManagerFactory is also used to close the database once we are finished using it:
entityManagerFactory.close();
When the connection to the database is no longer needed the EntityManager can be closed:
entityManager.close();

Complete Example - Let's Put Together

private static void insertEntity() {
    EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("PERSISTENCE");
    EntityManager entityManager = entityManagerFactory.createEntityManager();
    entityManager.getTransaction().begin();

    Student student = new Student("Ramesh", "Fadatare", "rameshfadatare@javaguides.com");
    entityManager.persist(student);
    entityManager.getTransaction().commit();
    entityManager.close();
    entityManagerFactory.close();
}

References



Comments