Understanding of the Object Relational Mapping in JPA

JPA ORM allows developers to work with Java objects to represent entities in the application and map them to the corresponding tables in a relational database.

Below are the processes to Implement.

1. Define the Entity Classes

An Entity class represents the table in the database. These classes are annotated with the @Entity to mark them as JPA entities of the JPA application.

@Entity
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private double price;

// Constructors, getters, setters
}

2. Define the Relationships

We can use the annotation such as the @OneToOne, @OneToMany, @ManyToOne and @ManyToMany to define the relationships between the entities of the application.

3. Configure the Persistence Unit

Define the persistece.xml file to the configure the persistence unit and it can specify the database connection details and the entity classes of the JPA application.

// Configuration in persistence.xml
<persistence-unit name="myPU" transaction-type="RESOURCE_LOCAL">
<class>com.example.Product</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/mydb"/>
<property name="javax.persistence.jdbc.user" value="username"/>
<property name="javax.persistence.jdbc.password" value="password"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
</properties>
</persistence-unit>

4. Perform the CRUD operations

We can use the EntityManager interface provided by the JPA to perform the CRUD (Create, Read, Update, Delete) operations on the entities.

// CRUD Operations
EntityManagerFactory emf = Persistence.createEntityManagerFactory("myPU");
EntityManager em = emf.createEntityManager();
// Create
em.getTransaction().begin();
Product product = new Product();
product.setName("Laptop");
product.setPrice(1000.0);
em.persist(product);
em.getTransaction().commit();
// Read
Product retrievedProduct = em.find(Product.class, 1L);
System.out.println(retrievedProduct.getName()); // Output: Laptop
// Update
em.getTransaction().begin();
retrievedProduct.setPrice(1200.0);
em.getTransaction().commit();
// Delete
em.getTransaction().begin();
em.remove(retrievedProduct);
em.getTransaction().commit();

5. Transaction Management

We can use the transactions to the ensure the data consistency and integrity and it can annotations such as the @Transcational can be used for the declarative transactions management of the JPA application.

JPA – Object Relational Mapping

JPA in Java is defined as the Java Persistence API and consists of Java specification for accessing, persisting, and maintaining data between Java objects and related databases One of the main features of JPA is Object Relational Mapping (ORM). This is the bridge between, or we can say the difference between an object-oriented domain model and a relational database model.

Similar Reads

Understanding of the Object Relational Mapping in JPA

JPA ORM allows developers to work with Java objects to represent entities in the application and map them to the corresponding tables in a relational database....

Implementing JPA ORM in a Java application

We will develop a simple JPA application that can demonstrate the Object relational Mapping of the application....