Data access without leaking JDBC upstairs
Repositories speak persistence; services speak use-cases
Spring Data JPA can derive queries from method names, and @Query handles the rest. The rule of thumb: repositories return domain objects or values, never ResultSets, never JPA-specific types leaking into controllers. Services orchestrate multi-step operations inside a @Transactional boundary.
interface InvoiceRepository extends JpaRepository<Invoice, Long> {
List<Invoice> findByCustomerIdAndStatus(long customerId, InvoiceStatus status);
@Query("select i from Invoice i where i.dueDate < :before and i.status = 'OPEN'")
List<Invoice> findOverdue(@Param("before") LocalDate before);
}Takeaways
- Repositories return domain; services orchestrate.
- Transactions bracket business operations, not single SQL statements.
- Watch for N+1 queries — they hide well until production traffic shows up.
Enjoying This Lesson?
Your support helps create more comprehensive courses and lessons like this one. Help me build better learning experiences for everyone.
Support Awashyak