Posts

Spring Data JPA Java Record DTO Projection

Image
1. Introduction With Java 14 (and finalized in Java 16), Records provide an immutable and compact way to represent data. Using records in Spring Data JPA allows better performance and cleaner code when fetching only required fields instead of full entity objects. Benefits of Using Java Records in DTO Projections: Immutable by Default : Ensures data integrity. Concise Syntax : Reduces boilerplate code. Optimized Performance : Fetches only necessary fields from the database. 2. Setting Up the Spring Boot Project Dependencies Ensure you have the necessary dependencies in your pom.xml for a Spring Boot application with Spring Data JPA. < dependencies > <!-- Spring Boot Starter JPA --> < dependency > < groupId > org.springframework.boot </ groupId > < artifactId > spring-boot-starter-data-jpa </ artifactId > </ dependency > <!-- H2 Database (for testing) --> < dependency > <...

Guide to Dynamic Queries in Spring Boot: Using JpaSpecificationExecutor, Specification & Criteria API for Advanced Filtering

Image
Spring Data JPA provides a powerful way to build dynamic queries using the JpaSpecificationExecutor<T> interface along with the Specification API and Criteria API . This guide will walk you through how to use these features from scratch. 1. What is JpaSpecificationExecutor<T> ? JpaSpecificationExecutor<T> is an interface provided by Spring Data JPA that allows us to write dynamic and complex queries without manually crafting JPQL or native SQL. It works alongside the Specification API , which enables filtering data based on multiple criteria dynamically. Key Benefits : Dynamic Filtering : Build queries at runtime based on user input. Flexible Querying : Combine multiple conditions using AND / OR logic. Better Performance : Avoid fetching unnecessary data. Easier Maintenance : No need to write complex JPQL or native SQL. 2. Setting Up Spring Boot with Spring Data JPA Step 1: Add Dependencies Make sure you have the required dependencies in your pom.xml : < depe...

Securing a Python Flask REST API with Okta: A Complete Guide

Image
In today's API-driven world, securing backend services is non-negotiable . Whether you're building a microservices architecture or exposing critical business logic via APIs, robust authentication and authorization mechanisms are essential. Okta , a leading identity provider, offers seamless integration with OAuth 2.0 and OpenID Connect (OIDC) , allowing developers to enforce secure access control with minimal effort. Step 1: Set Up Okta for Authentication 1.1 Create an Okta Developer Account Go to Okta Developer Console and sign up for a free account. After signing in, navigate to Admin Dashboard . 1.2 Create an Okta Application From the Okta Admin Dashboard , go to Applications → Create App Integration . Select OAuth 2.0/OpenID Connect (OIDC) . Choose Web as the application type. Set Sign-in redirect URIs to: http : //localhost:5000/login/callback Set Sign-out redirect URIs to: http : //localhost:5000/logout/callback Assign users/groups to the application. 1.3 Get Client ...

Spring Boot JPA @EntityGraph, @NamedEntityGraph, and JOIN FETCH – Complete Guide with Examples

Image
When working with Spring Boot JPA, the N+1 query problem can impact performance due to lazy loading. To optimize queries and fetch related entities efficiently, JPA provides @EntityGraph, @NamedEntityGraph, and JOIN FETCH . This guide walks you through their usage with practical examples, helping you write efficient, high-performance database queries in Spring Boot. 1. Introduction When working with JPA and Hibernate, N+1 query issues often arise due to lazy loading. @NamedEntityGraph and @EntityGraph provide a way to fetch related entities efficiently. JOIN FETCH is a JPQL technique that helps load associated entities in a single query. 2. Create a Spring Boot Project You can use Spring Initializr with: Spring Boot 3+ Dependencies: Spring Web, Spring Data JPA, Lombok, H2 Database 3. Define JPA Entities Department Entity (OneToMany with Employee) import jakarta .persistence .*; import lombok .Data ; import java .util .List ; @Data @Entity @Table (name = "departments...

Spring Boot JPA Composite Key Example | @EmbeddedId vs @IdClass

Image
In JPA, a composite key is a primary key that consists of multiple columns. There are two ways to define composite keys: Using @Embeddable and @EmbeddedId (Recommended) Using @IdClass (Alternative Approach) We will cover both approaches with examples in a Spring Boot application using Spring Data JPA. Approach 1: Using @Embeddable and @EmbeddedId (Recommended) This is the preferred method because it allows better encapsulation of key fields. Step 1: Create a Spring Boot Project Use Spring Initializr to create a new project with the following dependencies: ✅ Spring Web ✅ Spring Data JPA ✅ H2 Database (for testing) Step 2: Define the Composite Key Class This class represents the primary key. package com.example.demo.model; import jakarta.persistence.Embeddable; import java.io.Serializable; import java.util.Objects; @Embeddable public class OrderKey implements Serializable { private Long orderId; private Long productId; public OrderKey() {} pub...