JakartaJEEWebDevelopment

Jakarta JEE8 Enterprise Web Development

View on GitHub

JPA Essentials for the Movie Database

1. Introduction

Why Use JPA for Database Operations?

How JPA Simplifies Our Movie DB Project


2. Maven Dependencies

Key Points:

Hands-On: Adding Dependencies

Update your pom.xml file with the following:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
    <groupId>org.mariadb.jdbc</groupId>
    <artifactId>mariadb-java-client</artifactId>
    <scope>runtime</scope>
</dependency>

3. Marking an Entity and Mapping It to a Table

Concepts:

Example: Mapping the Studio Class

import javax.persistence.*;

@Entity
@Table(name = "Studio")
public class Studio {
    // Fields and methods here...
}

4. How JPA Uses Entity Fields

Key Points:

Practical Example: Map the studioName Field

@Column(name = "StudioName", nullable = false, length = 100)
private String studioName;

5. Mapping Surrogate Fields

Explanation:

Demo: Adding a Surrogate Key

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

6. Creating Simple IDs

Teachings:

Example: Setting Up IDs for the Actor Entity

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long actorId;

7. Creating Composite IDs

Concept:

Example: Composite Key for the Movie_Actor Table

@Embeddable
public class MovieActorId implements Serializable {
    @Column(name = "MovieId")
    private Long movieId;
    @Column(name = "ActorId")
    private Long actorId;

    // equals, hashCode, getters, and setters...
}

8. Using Basic Data Types

Discussion:

Illustration: Examples in Movie Entity

@Column(name = "MovieReleaseDate")
private LocalDate releaseDate;

@Column(name = "MovieRating", precision = 3, scale = 1)
private BigDecimal rating;

9. Specifying Column Names and Details

Key Points:

Example: Customizing the actorName Field

@Column(name = "ActorName", nullable = false, length = 100, unique = true)
private String actorName;

Engagement Strategies

1. Scenario-Based Learning

2. Interactive Quizzes

3. Live Coding Sessions

4. Pair Programming