The Ultimate JPA Interview Cheat Sheet | Top Scenario-Based Questions & Answers | Code Decode

The Ultimate JPA Interview Cheat Sheet | Top Scenario-Based Questions & Answers | Code Decode

6.971 Lượt nghe
The Ultimate JPA Interview Cheat Sheet | Top Scenario-Based Questions & Answers | Code Decode
we have covered top Hibernate JPA scenario based interview questions and answers for experienced developer Udemy Course Discounted Link https://www.udemy.com/course/master-spring-boot-microservice-angular-with-k8s-cicd-aws/?couponCode=BLACKFRIDA### 7. Improper Spring Transaction Configuration Y2024 1) When using `@Transactional`, you notice that the changes are not reflecting in the database. What could be the reason? ### 1. Missing `@Transactional` Proxy (Self-Invocation Issue) ### 2. Incorrect Transaction Propagation ### 3. No Exception Thrown (Transaction Not Rolled Back) ### Scenario: File or I/O Error in a Transaction ### 4. Database Flush Not Triggered ### 5. Incorrect Isolation Level ### 6. Read-Only Transaction ### 7. Improper Spring Transaction Configuration If changes are not reflecting in the database despite using `@Transactional`, check the following: ✅ Are you calling the transactional method from another bean? (Proxy issue) ✅ Is the correct transaction propagation used? (`Propagation.REQUIRED`) ✅ Are checked exceptions preventing rollback? (`rollbackFor = Exception.class`) ✅ Is Hibernate flushing the session properly? (`flush()`) ✅ Is the transaction isolation level appropriate? (`Isolation.READ_COMMITTED`) ✅ Is `@Transactional(readOnly = true)` mistakenly set? ✅ Is Spring transaction management enabled? (`@EnableTransactionManagement`) 2) You have an entity with a `@GeneratedValue(strategy = GenerationType.AUTO)`, but while inserting records, IDs are not generating sequentially. Why? ### N+1 Query Problem - Scenario-Based Interview Answers #### Q1: What is the N+1 Query Problem? 👉 Answer: The N+1 Query Problem happens when Hibernate or JPA executes one query to fetch the parent entities (N) and then executes N separate queries to fetch the associated child entities. ### Key Takeaways ✅ N+1 Query Problem happens when multiple queries fetch related entities lazily. ✅ Best Fix: Use `JOIN FETCH` or Entity Graphs to load all data in one query. ✅ Alternative Fixes: Use `@BatchSize` or `@Fetch(FetchMode.SUBSELECT)` if you can't modify queries. #### Q3: How would using `@BatchSize` or `@Fetch(FetchMode.SUBSELECT)` improve performance? 👉 Answer: If you cannot modify queries directly (e.g., using `JOIN FETCH`), Hibernate provides two batch fetching strategies: ## Soft Delete Implementation in JPA/Hibernate ### 📌 What is Soft Delete? Soft delete means marking a record as inactive instead of physically deleting it from the database. This is useful when you need to retain historical data or recover deleted records. ## 📌 Key Takeaways ✅ Soft Delete is implemented using a boolean flag (`isDeleted`) instead of removing records. ✅ Use `@Where(clause = "is_deleted = false")` to automatically exclude deleted entities. ✅ Use `@SQLDelete` to override the delete query and mark records as deleted instead of removing them. ✅ Be aware of caching issues, cascading deletes, and `JOIN` queries that may still fetch deleted data. Mock Interview Playlist: https://www.youtube.com/playlist?list=PLyHJZXNdCXsdKpIT19C1nlZH_HROXyROF Most Asked Core Java Interview Questions and Answers: https://youtube.com/playlist?list=PLyHJZXNdCXscoyL5XEZoHHZ86_6h3GWE1 Advance Java Interview Questions and Answers: https://youtube.com/playlist?list=PLyHJZXNdCXsexOO1VQ4vs-BM2-8CKrixd Java 8 Interview Questions and Answers: https://youtube.com/playlist?list=PLyHJZXNdCXsdeusn4OM33415DCMQ6sUKy Hibernate Interview Questions and Answers: https://youtube.com/playlist?list=PLyHJZXNdCXsdC-p2186C6NO4FpadnCC_q Spring Boot Interview Questions and Answers: https://youtube.com/playlist?list=PLyHJZXNdCXsexOO1VQ4vs-BM2-8CKrixd Angular Playlist: https://www.youtube.com/watch?v=CAl7RQSdq2Q&list=PLyHJZXNdCXsfxRtDwtGkDD_lLfTWc1g0i SQL Playlist: https://www.youtube.com/playlist?list=PLyHJZXNdCXse86eLuwy5uZohd_bddE9Ni GIT: https://youtube.com/playlist?list=PLyHJZXNdCXscpl6pxOnL2lRWJlzvzjyZE Subscriber and Follow Code Decode Subscriber Code Decode: https://www.youtube.com/c/CodeDecode?sub_confirmation=1 LinkedIn : https://www.linkedin.com/in/codedecodeyoutube/ Instagram: https://www.instagram.com/codedecode25/ #jpa #codedecode #interviewquestions