Project Implementation of the List Mapping in JPA

Step 1: First, we will create a JPA project using Intellij Idea IDE. The project named as jpa-list-mapping-demo.

Step 2: Now, we will add the following dependencies into the created JPA project.

Dependencies:

<dependency>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.0.2.Final</version>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>

Now the project structure will look like below image:


Step 3: Now, open the persistance.xml file and write the below code for MYSQL database configuration of the database.

XML
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<persistence xmlns="https://jakarta.ee/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="https://jakarta.ee/xml/ns/persistence https://jakarta.ee/xml/ns/persistence/persistence_3_0.xsd"
             version="3.0">
    <persistence-unit name="PersistenceUnit">

        <properties>
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/example"/>
            <property name="javax.persistence.jdbc.user" value="root"/>
            <property name="javax.persistence.jdbc.password" value=""/>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
            <property name="hibernate.hbm2ddl.auto" value="update"/>
        </properties>

    </persistence-unit>
</persistence>


Step 4: After all the above steps, now create a package named model and create an entity class inside that package named as Student. Go to src > main > java > model > Student and write the below code file..

Java
package model;


import jakarta.persistence.*;

import java.util.ArrayList;
import java.util.List;

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

    @OneToMany(mappedBy = "student", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private List<Course> courses = new ArrayList<>();

    // Getters and setters

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Course> getCourses() {
        return courses;
    }

    public void setCourses(List<Course> courses) {
        this.courses = courses;
    }
}
  • This class represents the entity Student in the database.
  • The JPA annotation is added to define its mapping to the corresponding database table.
  • It contains fields for student ID, name, and List of courses, and getter and setter methods for these fields.


Step 5: Create the new Java package named as model. In that package, create a new entity class named Course. Go to src > main > java > model > Course and put the below code.

Java
package model;


import jakarta.persistence.*;

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

    @ManyToOne
    @JoinColumn(name = "student_id")
    private Student student;

    // Getters and setters

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Student getStudent() {
        return student;
    }

    public void setStudent(Student student) {
        this.student = student;
    }
}
  • This class represents the entity Course in the database.
  • The JPA annotation is added to define its mapping to the corresponding database table.
  • It contains fields for course ID, name, and many-to-one relationship of the Students, and getter and setter methods for these fields.


Step 6: Create a new Java package named util in that package, create the new Entity Java class for the creation of the entity and it named as JPAUtil. Go to src > main > java > util > JPAUtil and put the below code.

Java
package util;

import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityManagerFactory;
import jakarta.persistence.Persistence;

/**
 * Utility class for managing JPA (Jakarta Persistence API) resources.
 */
public class JPAUtil {

    private static final EntityManagerFactory entityManagerFactory;

    // Static block to initialize the EntityManagerFactory
    static {
        entityManagerFactory = Persistence.createEntityManagerFactory("PersistenceUnit");
    }

    /**
     * Retrieves an instance of EntityManager.
     */
    public static EntityManager getEntityManager() {
        return entityManagerFactory.createEntityManager();
    }

    /**
     * Closes the EntityManagerFactory.
     */
    public static void close() {
        entityManagerFactory.close();
    }
}


Step 7: Create a new Java class and named as the MainApplication. Go to src > main > java > MainApplication and put the below code.

Java
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityTransaction;
import model.Course;
import model.Student;
import util.JPAUtil;

import java.util.List;

public class MainApplication {
    public static void main(String[] args) {
        EntityManager em = JPAUtil.getEntityManager();
        EntityTransaction tx = em.getTransaction();

        try {
            tx.begin();

            // Create students
            Student student1 = new Student();
            student1.setName("Syam Sunder");

            Student student2 = new Student();
            student2.setName("Eswar");

            // Create courses
            Course course1 = new Course();
            course1.setName("Java");
            course1.setStudent(student1);

            Course course2 = new Course();
            course2.setName("HTML");
            course2.setStudent(student1);

            Course course3 = new Course();
            course3.setName("CSS");
            course3.setStudent(student2);

            em.persist(student1);
            em.persist(student2);
            em.persist(course1);
            em.persist(course2);
            em.persist(course3);

            tx.commit();

            // Retrieve students with their courses
            List<Student> students = em.createQuery("SELECT s FROM Student s", Student.class).getResultList();
            for (Student student : students) {
                System.out.println("Student: " + student.getName());
                System.out.println("Courses Enrolled:");
                for (Course course : student.getCourses()) {
                    System.out.println("- " + course.getName());
                }
            }
        } catch (Exception e) {
            if (tx != null && tx.isActive()) {
                tx.rollback();
            }
            e.printStackTrace();
        } finally {
            em.close();
            JPAUtil.close();
        }
    }
}
  • The MainApplication demonstrates basic CRUD operations using JPA (Jakarta Persistence API).
  • It creates and persists instances of students and courses, establishes relationships between them, and then retrieves and prints the students along with the courses they are enrolled in.
  • Finally, it handles transactions and closes the entity manager and connection resources properly.

pom.xml:

XML
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>jpa-list-mapping-demo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>jpa-list-mapping-demo</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.target>11</maven.compiler.target>
        <maven.compiler.source>11</maven.compiler.source>
        <junit.version>5.9.2</junit.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.hibernate.orm</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>6.0.2.Final</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.28</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jaxb</groupId>
            <artifactId>jaxb-runtime</artifactId>
            <version>3.0.2</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
        </plugins>
    </build>
</project>


Step 8: Once the project is done, run the application and we will get the output like the below image.

The above example demonstrates a simple Java application using JPA for the List mapping and it includes the entity classes for the Student and Course along with the JPA utility class for the managing the EntityManager instances.



JPA – List Mapping

JPA in Java is referred to as Java Persistence API(JPA). List mapping in JPA is a key feature for managing relationships between entities in an application. In this article, we will discuss the concept of List Mapping in JPA and its application. List mappings in JPA allow us to represent one-to-many relationships between entities using a Java collection, especially in the list of applications.

Similar Reads

Implementation Steps of JPA List Mapping

1. Define the Entity...

Project Implementation of the List Mapping in JPA

Step 1: First, we will create a JPA project using Intellij Idea IDE. The project named as jpa-list-mapping-demo....