Category: Java Persistence API (JPA)

Posted on: July 2, 2025 Posted by: rahulgite Comments: 0

JPA Scenario for dynamic DB

Question:How can I dynamically fetch data from different country-specific databases (like india_db, italy_db) using Spring Boot, such that I don’t need to add new configurations or modify any table when a new country is added? Answer:To achieve this in Spring Boot, where each country has its own database (following a pattern like <country>_db), but all databases share the same schema and credentials, you can implement a dynamic repository factory pattern.…

Posted on: July 2, 2025 Posted by: rahulgite Comments: 0

Caching in Spring Data JPA

Caching is a performance optimization technique that stores frequently accessed data in memory to avoid repetitive and expensive computations or database calls. 1. Why Use Cache? 2. Types of Caches 1. First-Level Cache (Persistence Context Cache) 2. Second-Level Cache 3. Application-Level Cache (Spring Cache Abstraction) Spring Cache abstracts the caching mechanism and lets you plug in your own provider: 2.1 Steps to Add Each Cache Provider ✅ Overview of All…

Posted on: July 2, 2025 Posted by: rahulgite Comments: 0

Spring Data JPA: Complete Guide with Examples and Diagrams

This guide provides an end-to-end explanation of Spring Data JPA including project setup, entity creation, repository interfaces, query methods, JPA architecture, persistence lifecycle, and JPQL examples. It also includes diagrams and best practices. 1. Project Setup Start with Spring Initializr (start.spring.io) and add the following dependencies: Project Structure: 2. JPA Architecture Overview Key Components: Flow Diagram: 3. Persistence Lifecycle States States of a JPA Entity: State Description New Not yet…

Posted on: June 24, 2025 Posted by: rahulgite Comments: 0

JPA, Hibernate, and Transaction Scenarios

1. @Transactional Annotation Issues Problem: Changes are not reflected in the database despite using @Transactional. Reason 1: Calling from the Same Class Reason 2: Wrong Propagation Type Reason 3: No Exception Thrown (or Checked Exception) Reason 4: No Explicit Flush (rare in Spring Boot) Reason 5: Incorrect Isolation Levels Reason 6: Read-Only Transaction Reason 7: @EnableTransactionManagement Not Enabled 2. ID Generation Gaps Problem: Gaps in ID generation like 1, 10,…

Posted on: April 21, 2025 Posted by: rahulgite Comments: 0

JPA Scenario Questions 3

Question 1: Before: Why is JPA being called every time even though the cache is not expired and criteria haven’t changed? Issue: Even though the employee data is the same, JPA repository is still being called on every request. The caching layer (like Redis or Ehcache) is not being used properly, and there’s no database query optimization in place. The data is not being retrieved from the cache as expected.…

Posted on: April 21, 2025 Posted by: rahulgite Comments: 0

JPA Interview Question 2

How can we implement a REST endpoint using a GET request to filter Employee entities based on any of their fields, without adding all 100+ fields as request parameters, and further support field-level data sources dynamically without changing application code or application.yml? Answer: To achieve zero-code and zero-config dynamic filtering, we design a system where each field of an Employee can pull its data from different data sources or queries…

Posted on: April 21, 2025 Posted by: rahulgite Comments: 0

JPA Scenario Questions 1

We have an Employee class with the following structure: When a manager is terminated, we want to terminate all employees reporting to them. However, since reportingEmps is marked as LAZY fetch, we are encountering an exception when trying to access the reporting employees. What is the exception, and how can it be resolved? Answer: The exception being encountered is: This happens because the reportingEmps collection is lazily loaded, and you…

Posted on: March 8, 2025 Posted by: rahulgite Comments: 0

Java Persistence API (JPA)

What is JPA? Java Persistence API (JPA) is a specification for managing relational data in Java applications. It provides an abstraction layer for object-relational mapping (ORM) and allows developers to interact with databases using Java objects rather than writing SQL queries manually. 1. Key Features of JPA 2. JPA Components a. Entity Entities are Java objects representing database tables. b. EntityManager The EntityManager is used to interact with the database.…

Posted on: January 19, 2025 Posted by: rahulgite Comments: 0

Hibernate:Basic Concepts and Features

Hibernate is a powerful, high-performance object-relational mapping (ORM) framework for Java applications. It simplifies database interactions by mapping Java objects to database tables and vice versa. 1. What is Hibernate and Why Use It? What is Hibernate? Hibernate is an open-source ORM tool that abstracts database interactions, enabling developers to write database queries using Java objects rather than SQL. Why Use Hibernate? 2. ORM and JPA ORM (Object-Relational Mapping) ORM…