I spent weeks writing SQL by hand. Then I met Hibernate. Then Spring Boot. Now I can’t go back. Here’s what changed 👇 Raw JDBC → Hibernate → Spring Boot Data JPA It’s not just “less code.” It’s a completely different mental model. What Hibernate gave me: No more raw SQL for basic operations. You write Java. Hibernate translates. session.persist(empleado); // INSERT session.merge(empleado); // UPDATE session.remove(empleado); // DELETE Zero SQL written by hand. ✅ HQL lets you query objects, not tables. It felt like a superpower. Then Spring Boot Data JPA walked in. empleadoRepository.save(empleado); empleadoRepository.findById(id); empleadoRepository.deleteById(id); That’s it. No SessionFactory. No transaction boilerplate. No session.close(). Spring manages it all. So when does each one make sense? 🔹 Raw JDBC — when you need full control, complex custom queries, or you’re working in a legacy codebase. Not glamorous, but powerful. 🔹 Hibernate (standalone) — when you want ORM without a full framework. Great for learning the fundamentals before Spring. 🔹 Spring Boot + JPA — for real-world apps where speed and maintainability matter. This is what most companies actually use. ⚠️ The traps nobody warns you about: • Spring Boot hides so much — if you skip Hibernate basics, you won’t understand why things break • N+1 query problem: fetching a list of entities that each trigger extra queries = silent performance killer • Lazy loading outside a transaction = LazyInitializationException (classic first-week Spring error) • @Transactional is not optional — forget it and your data won’t save when you think it will The magic is real. But the magic has rules. Learn Hibernate first. Then let Spring Boot automate it. Building this in IntelliJ with Java — and every topic feels like unlocking a new level 🎮 hashtag #Java hashtag #Hibernate hashtag #SpringBoot hashtag #JPA hashtag #DesarrolloBackend hashtag #DAM
From Raw JDBC to Spring Boot: Simplifying Java Development
More Relevant Posts
-
I spent weeks writing SQL by hand. Then I met Hibernate. Then Spring Boot. Now I can’t go back. Here’s what changed 👇 Raw JDBC → Hibernate → Spring Boot Data JPA It’s not just “less code.” It’s a completely different mental model. What Hibernate gave me: No more raw SQL for basic operations. You write Java. Hibernate translates. session.persist(empleado); // INSERT session.merge(empleado); // UPDATE session.remove(empleado); // DELETE Zero SQL written by hand. ✅ HQL lets you query objects, not tables. It felt like a superpower. Then Spring Boot Data JPA walked in. empleadoRepository.save(empleado); empleadoRepository.findById(id); empleadoRepository.deleteById(id); That’s it. No SessionFactory. No transaction boilerplate. No session.close(). Spring manages it all. So when does each one make sense? 🔹 Raw JDBC — when you need full control, complex custom queries, or you’re working in a legacy codebase. Not glamorous, but powerful. 🔹 Hibernate (standalone) — when you want ORM without a full framework. Great for learning the fundamentals before Spring. 🔹 Spring Boot + JPA — for real-world apps where speed and maintainability matter. This is what most companies actually use. ⚠️ The traps nobody warns you about: • Spring Boot hides so much — if you skip Hibernate basics, you won’t understand why things break • N+1 query problem: fetching a list of entities that each trigger extra queries = silent performance killer • Lazy loading outside a transaction = LazyInitializationException (classic first-week Spring error) • @Transactional is not optional — forget it and your data won’t save when you think it will The magic is real. But the magic has rules. Learn Hibernate first. Then let Spring Boot automate it. Building this in IntelliJ with Java — and every topic feels like unlocking a new level 🎮 #Java #Hibernate #SpringBoot #JPA #DesarrolloBackend #DAM
To view or add a comment, sign in
-
-
🔥 N+1 Query Problem in Hibernate (Most Common Performance Killer) What is it? When fetching a list of parent entities, Hibernate fires 1 query for the parent + N extra queries for each child → leading to N+1 queries. Example 🚨 List<User> users = userRepository.findAll(); for (User user : users) { System.out.println(user.getOrders().size()); } 👉 Hibernate does: 1 query → fetch all users N queries → fetch orders for each user Why this is bad? 🐌 Slow performance (DB round trips) 📈 Poor scalability ⚠️ Hidden issue (works fine locally, fails at scale) Solutions ✅ 1. Use JOIN FETCH @Query("SELECT u FROM User u JOIN FETCH u.orders") List<User> findAllUsersWithOrders(); 2. Use EntityGraph @EntityGraph(attributePaths = {"orders"}) List<User> findAll(); 3. Use Batch Fetching spring.jpa.properties.hibernate.default_batch_fetch_size=10 4. Use DTO Projection (🔥 Best for APIs) @Query(""" SELECT new com.app.dto.UserOrderDTO(u.name, o.id) FROM User u JOIN u.orders o """) List<UserOrderDTO> fetchUserOrders(); 👉 Fetch only required fields → avoids N+1 + reduces data load Flow ⚙️ 1️⃣ Fetch users 2️⃣ Access lazy collection 3️⃣ Hibernate triggers extra queries → N+1 problem Result 🎯 Reduced queries → Faster API → Better scalability Rule of Thumb 💡 👉 Always check generated SQL logs when using LAZY relationships 👉 If you are preparing for Java backend interviews, connect & follow - I share short, practical backend concepts regularly. #Java #Hibernate #SpringBoot #BackendDevelopment #Performance #JPA #SoftwareEngineering
To view or add a comment, sign in
-
-
Request life cycle (focused on Transactional, Hibernate, JPA & HikariCP) 1. Client sends HTTP request (POST /api/v1/orders) 2. OS receives TCP bytes → places in socket buffer 3. Tomcat picks up bytes → parses HTTP headers & body 4. Tomcat assigns request to a thread 5. Jackson deserialises JSON → Java object 6. Request routed to matching Controller via @RequestMapping 7. Controller validates input → calls Service 8. @Transactional proxy intercepts → opens DB connection → BEGIN 9. Service applies business logic → calls Repository 10. Spring Data JPA reads method name → generates JPQL 11. JPQL handed to EntityManager → Hibernate translates to SQL 12. Hibernate checks L1 cache → cache miss → sends SQL to MySQL 13. MySQL parses SQL → optimiser picks execution plan 14. InnoDB reads rows from disk/buffer → sends result back 15. Hibernate maps columns → @Entity Java object → stored in L1 cache 16. Service persists new data → Hibernate generates INSERT/UPDATE 17. MySQL executes write → acquires row locks → acknowledges 18. Method returns → @Transactional proxy intercepts → COMMIT 19. MySQL flushes to redo log → releases locks 20. Connection returned to HikariCP pool → L1 cache discarded 21. Controller wraps result → Jackson serialises to JSON 22. Tomcat writes HTTP response → OS sends TCP bytes back 23. Client receives response ✓ I love correlating all applications with kernel, so included sockets. If you want to know in depth correlation then I can mention page cache, fsync, thread life cycle, connection life cycle with port & overall kernel interaction at surface level in above lifecycle sequentially from next time :)
To view or add a comment, sign in
-
🚀43/100 - N+1 Problem in Hibernate If you’re working with JPA/Hibernate, you are likely facing this without realizing it. What is N+1 Problem 🔸1 query to fetch parent data (Department) 🔸N queries to fetch child data (Employees) 🔸Total → N + 1 queries 🔸Fetching 5 departments → triggers 6 queries Why does this happen 🔸Root cause = Lazy Loading 🔸Hibernate does NOT fetch related data initially 🔸It loads a proxy (placeholder) 🔸When accessed → fires a query 🔸Access inside loop → multiple queries → N+1 Where it usually happens 🔹OneToMany relationships 🔹Looping over entities 🔹Returning entities directly in APIs 🔹Nested object access Why this is a problem 🔸Multiple DB hits → performance degradation 🔸Increased latency 🔸Heavy load on database 🔸Not scalable for large datasets How to solve this Spring Boot Approach (Recommended) 🔸@EntityGraph(attributePaths = "employees") 🔸Forces single query fetch Hibernate / JPQL Approach 🔸JOIN FETCH Example: SELECT d FROM Department d JOIN FETCH d.employees Other Approaches 🔸DTO Projection → fetch only req data 🔸Batch Fetching → reduces queries 🔸Avoid blind EAGER loading Key takeaway 🔹Lazy loading is not bad 🔹Lazy loading + loop = N+1 problem 🔹Always control how data is fetched I’ve created a complete backend developer guide covering: 🔸What is N+1 problem (with examples) 🔸Why it happens (deep dive - lazy loading) 🔸Real code (Entity, Repo,Service,Controller) 🔸With vs Without N+1 execution 🔸EntityGraph vs Fetch Join 🔸Multiple optimization approaches 🔸Diagrams for clear understanding Worth going through if you're preparing for backend interviews or building scalable APIs. Save & Repost🔁 if this helps someone preparing seriously Follow Surya Mahesh Kolisetty for more backend and Java deep dives #Java #SpringBoot #Hibernate #BackendDevelopment #JPA #SystemDesign #Performance #InterviewPreparation #SoftwareEngineering #CodingInterview #Developers #TechLearning #CFBR #SystemDesign #MicroServices
To view or add a comment, sign in
-
🚨 Struggling with Performance Issues in Hibernate / Spring Data JPA? You might be facing the N+1 Problem! As Java developers, we often focus on writing clean business logic—but performance bottlenecks silently creep in through ORM behavior. One of the most common (and dangerous) issues? 👉 N+1 Query Problem --- 🔍 What is it? When your application executes: ✔️ 1 query to fetch parent entities ❌ + N additional queries to fetch child entities 👉 Result: N+1 total queries → serious performance degradation --- ⚠️ Why should you care? - Massive DB calls - Slow response time - High latency in microservices - Poor scalability under load --- 💡 Root Cause Default LAZY loading in ORM frameworks like Hibernate ORM triggers multiple queries when accessing relationships inside loops. --- 🔥 How to Solve It (Production-Ready Approaches) ✅ JOIN FETCH (Most Effective) → Fetch everything in a single query ✅ EntityGraph → Clean and flexible fetching strategy ✅ Batch Fetching → Reduces queries using IN clause batching ✅ DTO Projection → Fetch only required data (best for APIs) --- 📊 Real Impact Example Without optimization: 👉 100 records = 101 queries With optimization: 👉 100 records = 1 query --- 🧠 Pro Tip Always monitor SQL logs and understand how your ORM behaves internally. --- 🎯 Rule of Thumb “Fetch only what you need, when you need it — but do it efficiently.” --- I’ve created a detailed cheat sheet covering: ✔️ Problem breakdown ✔️ Internal working ✔️ Best solutions ✔️ Interview explanation --- #Java #SpringBoot #Hibernate #BackendDevelopment #Microservices #PerformanceOptimization #TechInterview #SoftwareEngine
To view or add a comment, sign in
-
-
🔥 How Spring Data JPA Works Internally (Explained with Architecture) If you’re a Java developer working with Spring Boot, understanding what happens behind JpaRepository is a game changer. Here’s a simplified breakdown of the internal flow 👇 🔹 When you define a repository interface, Spring DOES NOT require implementation ➡️ It generates a dynamic proxy at runtime using reflection 🔹 That proxy delegates calls to SimpleJpaRepository ➡️ The actual class handling CRUD operations 🔹 Then comes the heart of JPA → EntityManager ➡️ Acts as a bridge between Spring and Hibernate 🔹 Inside JPA lies the Persistence Context ➡️ First-level cache ➡️ Tracks entity changes (Dirty Checking) ➡️ Ensures consistency 🔹 Hibernate (JPA Implementation) does the heavy lifting: ✔ Converts Objects → SQL ✔ Manages Session & SessionFactory ✔ Handles caching & transactions 🔹 Query Execution Flow: Repository → EntityManager → Hibernate Session → JDBC → Database 🔹 Transactions? Handled seamlessly using @Transactional ➡️ Begin → Execute → Commit/Rollback 💡 Key Takeaways: ✔ Spring Data JPA = Abstraction ✔ Hibernate = Implementation ✔ EntityManager = Core engine ✔ Persistence Context = Hidden superpower Mastering this flow helps you: ✅ Debug production issues faster ✅ Optimize performance (N+1, Lazy Loading) ✅ Crack system design interviews 📌 If you’re preparing for senior Java interviews (7–10 yrs), this is MUST-KNOW. #Java #SpringBoot #Hibernate #SpringDataJPA #BackendDevelopment #SystemDesign #TechInterview
To view or add a comment, sign in
-
-
🚀 Day18 – Hibernate Today’s learning focused on stepping beyond JDBC and exploring Hibernate, a powerful ORM (Object Relational Mapping) framework that simplifies database interactions in Java 💻 🔹 What is Hibernate? ✔ Hibernate is an ORM framework that maps Java objects ↔ Database tables ✔ Eliminates the need for writing complex SQL queries manually ✔ Works on top of JDBC but provides a higher-level abstraction 💡 Instead of writing SQL, we work with objects → cleaner & more maintainable code 🔹 Core Concepts Learned: ✔ Configuration File (hibernate.cfg.xml) Contains database details, dialect, driver, and mapping configurations ✔ SessionFactory Heavy object, created once Responsible for creating sessions ✔ Session Used to perform CRUD operations Acts as a bridge between application & database ✔ Transaction Ensures data consistency (commit & rollback) 🔹 Hibernate Workflow: ➡️ Configure Hibernate (XML / properties) ➡️ Create SessionFactory ➡️ Open Session ➡️ Begin Transaction ➡️ Perform operation (save/update/delete) ➡️ Commit Transaction ➡️ Close Session 🔹 Basic Example (Saving Data): Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); session.save(employee); tx.commit(); session.close(); 💡 Minimal code compared to JDBC → more readable & efficient 🔹 Advantages of Hibernate: ✔ No need to write repetitive SQL queries ✔ Database-independent (supports multiple DBs) ✔ Automatic table mapping ✔ Reduces boilerplate code ✔ Improves development speed 🔹 Key Learnings: ✨ Understood ORM concept (Object ↔ Table mapping) ✨ Learned Hibernate architecture & workflow ✨ Compared Hibernate with JDBC ✨ Realized how frameworks simplify backend development 🔹 What’s Next? 🚀 Performing full CRUD operations using Hibernate 🚀 Exploring annotations (@Entity, @Id, etc.) 🚀 Integrating Hibernate with real-time projects #Java #Hibernate #ORM #AdvancedJava #BackendDevelopment #FullStackDevelopment Guided by, Anand Kumar Buddarapu sir, Saketh Kallepu sir, Uppugundla Sairam sir.
To view or add a comment, sign in
-
-
🚀 Understanding @Entity vs @Table in JPA (Spring Boot Developers, this one's for you!) If you're working with JPA or Hibernate, you've definitely come across @Entity and @Table. While they often appear together, they serve different purposes. Let’s break it down 👇 🔹 @Entity – Marks a Class as a Database Entity The @Entity annotation tells JPA: 👉 "This Java class should be mapped to a database table." ✔️ It is mandatory for persistence ✔️ Must be placed at the class level ✔️ Every entity must have a primary key (@Id) @Entity public class User { @Id private Long id; private String name; private String email; } 🔹 @Table – Customizes Table Mapping The @Table annotation lets you control how your entity maps to the database table. 👉 Without it, JPA uses the class name as the table name by default ✔️ It is optional ✔️ Used for customization like table name, schema, indexes, etc. @Entity @Table(name = "users", schema = "public") public class User { @Id private Long id; private String name; private String email; } 🔥 Key Difference @Entity → Declares the class as a JPA entity @Table → Customizes the table details 💡 Pro Tip: Use @Table when working with existing databases or when you need specific naming conventions. 📌 Mastering these small annotations can make your ORM layer much cleaner and more powerful. #Java #SpringBoot #JPA #Hibernate #BackendDevelopment #SoftwareEngineering #CodingTips
To view or add a comment, sign in
-
Yesterday we talked about how Lombok can crash your app through Hibernate relationships. Today, let’s talk about the other silent killer in those exact same entities: The N+1 Query Problem. If you work with Spring Data JPA long enough, you will eventually write a piece of code that looks completely fine, passes all unit tests, and then completely chokes your database in production. Usually, the culprit is the N+1 problem. The Trap: Let’s say you have a User entity with a @OneToMany relationship to Order. You want to fetch 100 users and print their order totals. You write a simple repository call: userRepository.findAll() (This is 1 query).Then, you loop through those 100 users and call user.getOrders(). What you think happens: Hibernate fetches the users and their orders efficiently. What actually happens: Because relationships are (and should be!) FetchType.LAZY by default, Hibernate executes 1 query to get the 100 users, and then 100 additional queries—one for each user—to fetch their orders. You just hit your database 101 times for a single API request. Multiply that by 1,000 concurrent users, and your DBA is calling you in a panic. 😅 The Senior Fixes: 1. The Quick Fix: JOIN FETCH Instead of using standard findAll(), write a custom JPQL query: SELECT u FROM User u JOIN FETCH u.orders This forces Hibernate to grab everything in a single, efficient JOIN query. 2. The Elegant Fix: @EntityGraph If you don't want to write custom queries, Spring Boot lets you use Entity Graphs to dynamically define which lazy associations should be fetched eagerly for a specific method call. 3. The Ultimate Fix: DTO Projections Stop fetching full Managed Entities if you just need to read data! Write a query that maps directly to a Record or DTO. It bypasses the Hibernate proxy lifecycle entirely and is blazing fast. JPA makes the easy things trivial, but it makes the hard things incredibly dangerous if you don't look at the generated SQL. What is your team's standard way of catching N+1 queries before they hit production? Do you use a specific tool, or rely on code reviews? 👇 #Java #SpringBoot #Hibernate #BackendDevelopment #Microservices #SoftwareEngineering #PerformanceTuning #CleanCode
To view or add a comment, sign in
-
✨ #Day13 – Hibernate: Two Tables & Sequence Generator 🔥 Today I moved one step deeper into Hibernate (ORM) by working with multiple tables and sequence-based ID generation 🚀 🔹 Working with Two Tables 🧩 In real projects, data is connected across tables. Hibernate makes this easy using relationships 👇 👉 Example: Student ↔ Course ✔ One student can have many courses → One-to-Many ✔ Tables are linked using foreign keys 🔹 Entity Example 🧑💻 @Entity class Student { @Id @GeneratedValue(strategy = GenerationType.SEQUENCE) int id; String name; @OneToMany List<Course> courses; } @Entity class Course { @Id int id; String courseName; } 👉 Hibernate automatically manages relationships between these two tables 🔗 🔹 Hibernate Sequence Generator 🔢 Instead of manually giving IDs, Hibernate can generate them using database sequences ✔ Ensures unique IDs ✔ Improves performance in large applications ✔ Works well with databases like Oracle, PostgreSQL 🔹 Example of Sequence Generator 🧑💻 @Entity class Student { @Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "student_seq") @SequenceGenerator(name = "student_seq", sequenceName = "student_sequence", allocationSize = 1) int id; String name; } 👉 Here, IDs are generated automatically using a sequence in DB 🔹 Key Takeaways 🎯 ✔ Hibernate simplifies table relationships ✔ No need to manage foreign keys manually ✔ Sequence generator helps in auto ID creation ✔ Clean, scalable & production-ready approach 🔥 What’s Next? ➡️ Mapping types (OneToOne, ManyToMany) ➡️ Fetch types (Eager vs Lazy) ➡️ Real-time mini project using Hibernate #Java #Hibernate #ORM #FullStackDeveloper #BackendDevelopment #LearningJourney 💻 Anand Kumar Buddarapu,Saketh Kallepu,Uppugundla Sairam
To view or add a comment, sign in
-
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development