Habit #3: Repositories Don’t Leak (Domain Integrity) As engineers, we often trade long-term stability for short-term speed. One of the most common traps? Letting your Database Entities escape the Repository layer. The Problem: Passing Hibernate/JPA entities all the way to the Controller. It’s fast to code, but it couples your API to your DB schema. If the DB changes, the API breaks. Plus, the dreaded LazyInitializationException. The Habit: Keep your schema private. The Repository should map entities to clean Domain Models or DTOs before they reach the Service layer. The Benefits: ✅ Decoupling: Change your DB without affecting your API contract. ✅ Security: No accidental "Mass Assignment" of internal fields. ✅ Clarity: Your Service layer works with business logic, not DB rows. What’s your take: Do you map to DTOs every time, or is it "over-engineering" for you? Let's discuss! 👇 #Java #Backend #CleanCode #SoftwareArchitecture #BestPractices #LetsCodeJava
Repository Integrity: Decouple API from DB Schema
More Relevant Posts
-
Late last year, I faced a scenario I hadn't encountered before: the need to process a 600,000-row Excel file with 18 columns of data and complex dependencies. My first attempt resulted in an OutOfMemoryError. The sheer volume combined with the business logic just crushed the heap. Instead of simply asking for more server RAM, I decided to dig deeper and look for modern ways to utilize 100% of Java's capabilities. The solution involved moving away from framework defaults and optimizing three critical areas: 1. Reading: I switched from loading the whole DOM to a Streaming approach. Keeping only a sliding window of rows in memory drastically reduced the footprint. 2. Validation: I replaced thousands of database queries (the N+1 problem) with an in-memory entity cache (Maps), validating business rules locally. 3. E: I swapped JPA's saveAll for JDBC Batch. Inserting data in chunks without the overhead of Hibernate's dirty checking made the process fly. It was a great reminder that sometimes the solution isn't about infrastructure, but solid software engineering. #Java #Spring #Backend #Performance #SoftwareEngineering
To view or add a comment, sign in
-
-
𝗗𝗧𝗢 𝘃𝘀 𝗘𝗻𝘁𝗶𝘁𝘆 𝗶𝗻 𝗝𝗮𝘃𝗮, 𝗪𝗵𝘆 𝗜𝘁 𝗠𝗮𝘁𝘁𝗲𝗿𝘀 A common question in Java architecture: Should you expose your Entity directly, or use a DTO (Data Transfer Object)? Here’s the bottom line: 𝗘𝗻𝘁𝗶𝘁𝘆 Represents your database model. Great for persistence, but risky to expose externally. 𝗗𝗧𝗢 A lightweight object for API requests/responses. Enhances security, performance, and decoupling. 𝗪𝗵𝘆 𝗗𝗧𝗢𝘀 𝗺𝗮𝘁𝘁𝗲𝗿: • Keep sensitive fields hidden (e.g., password hashes, internal flags) • Transfer only the data you need for each API operation • Use tailored DTOs for different endpoints to keep things clean • Prevent breaking changes when the DB schema evolves (avoid coupling API to DB) • Apply validation rules easily with DTOs for incoming requests 𝗥𝘂𝗹𝗲 𝗼𝗳 𝗧𝗵𝘂𝗺𝗯: • Entities stay inside the data layer (e.g., JPA, Hibernate) • DTOs travel across the API and client layers (e.g., REST Controllers, Services) Do you always use DTOs in your Java projects, or do you sometimes expose Entities directly to simplify things? #Java #SpringBoot #DTO #Entity #BackendDevelopment #Microservices #API #CleanArchitecture #JavaBestPractices #DataTransferObject #JPA #Hibernate #Security #Performance #Decoupling #ScalableSystems
To view or add a comment, sign in
-
𝗗𝗧𝗢𝘀 𝗮𝗿𝗲 𝗻𝗼𝘁 𝗮𝗯𝗼𝘂𝘁 𝗺𝗮𝗽𝗽𝗶𝗻𝗴, 𝗶𝘁’𝘀 𝗮𝗯𝗼𝘂𝘁 𝗯𝗼𝘂𝗻𝗱𝗮𝗿𝗶𝗲𝘀 When I first started building APIs with Spring Boot, I saw this everywhere: • JPA Entities returned directly from controllers • lazy loading exceptions showing up randomly in production • internal fields accidentally exposed (audit flags, internal IDs, status codes) • small DB change breaking clients because response shape changed Did it work? Yes. Was it clean, scalable, and maintainable? No! That’s when I really understood the role of DTOs in Java. 𝗧𝗵𝗲 𝗸𝗲𝘆 𝗶𝗻𝘀𝗶𝗴𝗵𝘁: Entities exist for persistence. DTOs exist for communication. Your API is a contract. And your database model should not become that contract. Entities change when: • the schema evolves • relationships change • performance tuning happens • internal fields are added for business rules DTOs exist so your clients don’t feel that pain. 𝗧𝗵𝗲 𝗿𝗲𝘀𝘂𝗹𝘁: • cleaner APIs with predictable response shapes • sensitive fields stay private (passwordHash, internal flags, audit data) • less coupling between DB schema and public API • fewer production surprises (lazy-loading, infinite recursion, huge payloads) Instead of each controller deciding what to expose, the application clearly states: When data crosses the API boundary, it’s shaped intentionally. Always. 𝗟𝗲𝘀𝘀𝗼𝗻 𝗹𝗲𝗮𝗿𝗻𝗲𝗱 Architecture is not about writing more classes. It’s about protecting boundaries. #Java #SpringBoot #DTO #Entity #JPA #Hibernate #APIDesign #CleanArchitecture #BackendEngineering #Microservices #DomainModel #DataTransferObject #Encapsulation #Security #Performance #LazyLoading #JsonSerialization #SystemDesign #BestPractices #MaintainableCode
To view or add a comment, sign in
-
🚀 Built an Employee Management REST API using Spring Boot and MySQL. 📌Designed the application using clean layered architecture (Controller–Service–Repository) and implemented full CRUD operations following RESTful best practices. 📌Applied the DTO pattern for separation of concerns, added request validation with structured error handling, and implemented pagination & sorting using Spring Data JPA for efficient data management. ✨This project strengthened my understanding of backend architecture, validation lifecycle, exception propagation, and real-world API design. 🛠 Tech Stack: Java, Spring Boot, Spring Data JPA (Hibernate), MySQL, Maven, Postman. 💻 Source Code: https://lnkd.in/gawm5q4A #SpringBoot #Java #BackendDevelopment #RESTAPI #SoftwareDeveloper #FullStackDeveloper
To view or add a comment, sign in
-
🚀 Spring Boot Backend Journey – Day 5 Today I stepped into database integration using JPA & Hibernate. ✅ What I Learned: Creating @Entity classes Defining Primary Key with @Id Using JpaRepository Performing Basic CRUD Operations Connecting Spring Boot with Database 📌 Goal: Make the backend persistent and production-ready. Understanding how objects map to database tables made the entire flow clearer — from API request ➝ business logic ➝ database ➝ response. Now the application is no longer temporary in-memory — it stores real data 💾 Step by step building a real-world backend system. #SpringBoot #JPA #Hibernate #BackendDevelopment #JavaDeveloper #SoftwareEngineering #LearnInPublic
To view or add a comment, sign in
-
Small Change — Big Performance Impact This week I fixed an N+1 performance issue caused by calling the database inside a loop. The problem: For each item in a list, the code triggered a separate DB query. Under load → too many round trips → slower response time. The fix: Moved the database call outside the loop and fetched all required data in a single query. Result: ✔ Fewer DB calls ✔ Faster API response ✔ Better scalability Sometimes performance improvements are not complex — just smarter query design. #Java #SpringBoot #Backend #Performance #CleanCode
To view or add a comment, sign in
-
-
🧠 Java Systems from Production - Database Indexes Are Part of API Design Most API designs focus on: 1. Endpoints 2. Request/response payloads 3. Contracts But in production systems, an API is fundamentally an access pattern to data. Here’s the hard truth: If an access pattern is not indexed, the API is broken by design. 📌 Lessons from Java systems at scale: -->APIs define how data is accessed -->Indexes determine whether that access scales -->No index → full table scan -->Full table scan under load → high latency, timeouts, and failures 💡 A simple rule that holds in production: Every frequently invoked API must have a corresponding index that supports its query pattern. Why this matters: 1. Code optimizations come later 2. Infrastructure can be scaled horizontally 3. A missing index will bottleneck the system regardless of scaling Indexes are not just a database concern. They are a core part of the API contract. #Databases #SystemDesign #Java #BackendEngineering #Microservices #Performance #APIDesign
To view or add a comment, sign in
-
🚀 Published on Maven Central! 🎉 I’m excited to share that my open-source Java library — pg-query-builder — is now officially available on Maven Central! 📦 Maven Artifact: https://lnkd.in/g6SWKHDV pg-query-builder is a lightweight, PostgreSQL-focused dynamic SQL query builder tailored for reactive applications and flexible backend services. ✨ Key Features Build dynamic SQL queries with ease Designed for PostgreSQL database workflows Clean and intuitive API for filtering, sorting & pagination Ready to integrate in Spring WebFlux / R2DBC environments Whether you’re building REST APIs, microservices, or reactive data layers — this utility simplifies custom query construction and enhances code readability. 🔗 Check it out on Maven Central: https://lnkd.in/g6SWKHDV If you try it out, I’d love to hear your feedback! 🙌 #Java #OpenSource #MavenCentral #SpringWebFlux #PostgreSQL #R2DBC #SoftwareDevelopment
To view or add a comment, sign in
-
-
If you’re building Spring Boot applications and your project is getting messy… It’s not a framework problem. It’s a structure problem. This image perfectly explains how a clean Spring Boot architecture should look below👇 🟢 Controller – Entry Point Handles HTTP requests, validates DTOs, and delegates work to services. 👉 Rule: No business logic here. 🔵 Service – Business Logic This is the brain of your application. Contains domain rules, transactions, workflows, and policies. 👉 If it changes business behavior, it belongs here. 🟣 Repository – Persistence Layer Responsible for database communication using JPA, Hibernate, JDBC, or external APIs. 👉 Only data access. Nothing more. 🟢 Model / Entity – Domain Representation Represents your core business objects. Keep them simple, consistent, and valid. 🟠 DTO – API Contract Never expose entities directly. DTOs protect internal changes and maintain API stability. 🟢 Config – Configuration Layer Handles Security, Beans, Infrastructure setup. 🔴 Exception Handling – Global Errors Centralized error handling makes your application predictable and clean. ✅ Why This Works ✔ Clear separation of concerns ✔ Easier unit testing ✔ Faster debugging ✔ Safer refactoring ✔ Microservices-ready architecture A clean architecture today saves you from production headaches tomorrow. 💬 How do you structure your Spring Boot projects — layered or feature-based? #SpringBoot #Java #BackendDevelopment #Microservices #SoftwareArchitecture #CleanCode #FullStackDeveloper
To view or add a comment, sign in
-
-
If your backend can’t stay calm under load, it’s not “scaling”. It’s just surviving. Here’s a clean backend architecture I keep coming back to when building Java services. Not because it’s trendy. Because it works in production. Backend architecture (simple but strong): Client → Load Balancer → API Gateway (auth, rate limits, routing) From there it splits into two lanes: Lane 1: Sync APIs (fast user flows) API Gateway → Java microservices (domain based) → Cache (hot reads) → DB (writes + strong data) Lane 2: Async events (heavy work + spikes) Java microservices → Message broker (topics/queues) → Worker services (consume + process) → DB / search / notifications Why this setup holds up: You don’t block threads for long work. You don’t overload your database during traffic spikes. You don’t couple every service call to a single slow dependency. The gateway gives you one clean entry point. Caching protects your database. Events absorb bursts and keep the system steady. And when things go wrong (they will), you can fail small. #Java #BackendEngineering #Microservices #SystemDesign #DistributedSystems Raul Junco Neo Kim
To view or add a comment, sign in
-
Explore related topics
- Building Clean Code Habits for Developers
- Code Quality Best Practices for Software Engineers
- Keeping Code DRY: Don't Repeat Yourself
- Coding Best Practices to Reduce Developer Mistakes
- Clean Code Practices For Data Science Projects
- Best Practices for Writing Clean Code
- Writing Clean Code for API Development
- How to Stabilize Fragile Codebases
- How to Improve Code Maintainability and Avoid Spaghetti Code
- How to Assess High-Quality Code Repositories
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
I've just uploaded a quick deep dive on this habit to my YouTube channel! You can watch it here: https://www.youtube.com/watch?v=zVX-KSrhXNA