Welcome to our comprehensive guide on Java Persistence API (JPA)! In this lesson, we'll explore the world of Java persistence, diving deep into JPA and its practical applications. Let's get started!
Java Persistence API (JPA) is a Java specification for accessing, persisting, and managing data between Java objects (Java Persistent objects) and a relational database. It provides an easy-to-use, database-agnostic, and standardized way to interact with databases.
JPA simplifies Java persistence by:
To use JPA, you'll need the following:
To add JPA and Hibernate to your Maven project, include the following dependencies in your pom.xml:
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>8.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.6.3.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.2.Final</version>
</dependency>
</dependencies>An entity class represents a database table. Let's create a simple entity:
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class User {
@Id
private Long id;
private String name;
private String email;
// Getters and Setters
}š Note: Annotations like @Entity and @Id are used to define JPA entities and their primary keys, respectively.
A persistence unit defines the connection to the database. To create a persistence unit, add a persistence.xml file in the src/main/resources directory:
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
<persistence-unit name="myPersistenceUnit">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.example.User</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/your_database_name"/>
<property name="hibernate.connection.driver_class" value="com.mysql.cj.jdbc.Driver"/>
<property name="hibernate.connection.username" value="your_database_username"/>
<property name="hibernate.connection.password" value="your_database_password"/>
</properties>
</persistence-unit>
</persistence>To save an entity, use the EntityManager:
EntityManagerFactory factory = Persistence.createEntityManagerFactory("myPersistenceUnit");
EntityManager entityManager = factory.createEntityManager();
User user = new User();
user.setName("John Doe");
user.setEmail("johndoe@example.com");
EntityTransaction transaction = entityManager.getTransaction();
transaction.begin();
entityManager.persist(user);
transaction.commit();
entityManager.close();
factory.close();š Note: EntityManager is responsible for managing entities and their state.
To retrieve an entity, use the following code:
EntityManager entityManager = factory.createEntityManager();
entityManager.getTransaction().begin();
User user = entityManager.find(User.class, 1L);
entityManager.getTransaction().commit();
entityManager.close();
factory.close();What is the role of the `@Entity` annotation in JPA?
That's it for our Java JPA Introduction! In the next lesson, we'll dive deeper into JPA by exploring queries, relationships, and more. Stay tuned! šÆ