JakartaJEEWebDevelopment

Jakarta JEE8 Enterprise Web Development

View on GitHub

Mapping Complex Data Types

Advanced JDBC and JPA Mapping

Objective

Gain advanced skills in mapping complex data types, enums, dates/times, and large properties using JPA while working with the Movie DB. By the end of this lesson, you will confidently implement advanced mappings and understand their use cases in real-world applications.


1. Mapping Complex Data Types

Introduction

Key Concepts

Example: Mapping an Embedded Address

  1. Create an Address class for use in multiple entities:
    @Embeddable
    public class Address {
        private String street;
        private String city;
        private String zipCode;
    
        // Getters and Setters
    }
    
  2. Embed the Address in the Studio entity:
    @Entity
    public class Studio {
        @Id
        private Long id;
    
        private String name;
    
        @Embedded
        private Address address;
    
        // Getters and Setters
    }
    

Activity


2. Using Enums as Entity Types

Introduction

Key Concepts

Example: Mapping Movie Ratings

  1. Define an enum for Rating:
    public enum Rating {
        G, PG, PG_13, R, NC_17
    }
    
  2. Add the Rating enum to the Movie entity:
    @Entity
    public class Movie {
        @Id
        private Long id;
    
        private String title;
    
        @Enumerated(EnumType.STRING)
        private Rating rating;
    
        // Getters and Setters
    }
    

Hands-On Task

Discussion


3. Understanding How JPA Handles Dates and Times

Introduction

Key Concepts

Examples

  1. Using Legacy Date Types:
    @Entity
    public class Movie {
        @Id
        private Long id;
    
        @Temporal(TemporalType.DATE)
        private Date releaseDate;
    
        // Getters and Setters
    }
    
  2. Using LocalDate:
    @Entity
    public class Movie {
        @Id
        private Long id;
    
        private LocalDate releaseDate;
    
        // Getters and Setters
    }
    

Practical Exercise

Quiz


4. Mapping Large Properties to CLOBS and BLOBS

Introduction

Key Concepts

Examples

  1. Mapping a Movie Description (CLOB):
    @Entity
    public class Movie {
        @Id
        private Long id;
    
        private String title;
    
        @Lob
        private String description;
    
        // Getters and Setters
    }
    
  2. Mapping Movie Posters (BLOB):
    @Entity
    public class Movie {
        @Id
        private Long id;
    
        @Lob
        private byte[] poster;
    
        // Getters and Setters
    }
    

Hands-On Task

Engagement Strategies

  1. Code Review:
    • Pair up with another student and review each other’s CLOB and BLOB implementations.
  2. Performance Discussion:
    • Discuss the performance implications of storing large objects in the database vs. using external storage (e.g., AWS S3).

Summary

This lesson explored advanced JPA techniques, including mapping complex types, enums, dates/times, and large objects. By applying these concepts, you can now: