JOptimize’s Post

Your DTO might be triggering database queries This looks like a clean DTO. But it can silently hit your database. public record User(String name, List<Order> orders) {} 🧠 Somewhere in your code user.orders().size(); 🚨 What actually happens 👉 Depending on your JPA setup: triggers a lazy query or loads the full collection or causes N+1 issues 👉 all from a simple .size() 💥 Real impact hidden database calls unpredictable performance harder to debug in production ✅ Better approach public record UserDto(String name, int orderCount) {} 👉 and compute it at the query level: SELECT u.name, COUNT(o) FROM User u LEFT JOIN u.orders o GROUP BY u.name 🧠 Takeaway A DTO should not control when your database is queried. 🔍 Bonus I built a tool that detects issues like: N+1 queries lazy loading traps inefficient data access 👉 https://joptimize.io How many hidden queries are behind your DTOs? #JavaDev #SpringBoot #Hibernate #JavaPerformance #Backend

To view or add a comment, sign in

Explore content categories