java
31 lines · 8 steps
Deriving JPA queries from method names in Spring
Spring Data JPA generates real SQL from repository method names, so a naming convention replaces hand-written queries.
Explained by
highlit
1package com.example.shop.repository;
2
3import com.example.shop.domain.Order;
4import com.example.shop.domain.OrderStatus;
5import org.springframework.data.domain.Page;
6import org.springframework.data.domain.Pageable;
7import org.springframework.data.jpa.repository.JpaRepository;
8import org.springframework.stereotype.Repository;
9
10import java.math.BigDecimal;
11import java.time.Instant;
12import java.util.List;
13import java.util.Optional;
14
15@Repository
16public interface OrderRepository extends JpaRepository<Order, Long> {
17
18 List<Order> findByCustomerIdOrderByCreatedAtDesc(Long customerId);
19
20 Page<Order> findByStatusAndTotalGreaterThanEqual(OrderStatus status, BigDecimal minTotal, Pageable pageable);
21
22 Optional<Order> findFirstByCustomerIdAndStatusOrderByCreatedAtDesc(Long customerId, OrderStatus status);
23
24 List<Order> findByStatusInAndCreatedAtBetween(List<OrderStatus> statuses, Instant from, Instant to);
25
26 boolean existsByReferenceIgnoreCase(String reference);
27
28 long countByCustomerIdAndStatus(Long customerId, OrderStatus status);
29
30 List<Order> findTop10ByCustomerCountryAndStatusNotOrderByTotalDesc(String country, OrderStatus excludedStatus);
31}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Method names in a Spring Data repository are parsed into queries, so structure beats boilerplate.
- 2Return types shape behavior: Optional guards absence, Page adds paging metadata, List returns everything.
- 3Keywords like In, Between, GreaterThanEqual, and Top10 express filtering, ranges, and limits without any SQL.
Related explainers
java
import java.util.List; import java.util.IntSummaryStatistics; public class OrderReport {
Aggregating stats with IntSummaryStatistics
streams
aggregation
records
Intermediate
5 steps
java
public class OrderRepository { private static final int BATCH_SIZE = 500; private static final String INSERT_SQL =
Batched JDBC inserts with transaction safety
jdbc
batching
transactions
Intermediate
8 steps
java
@RestControllerAdvice public class GlobalExceptionHandler { private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
Centralized error handling in Spring
exception-handling
problemdetail
rest-api
Intermediate
7 steps
javascript
const express = require('express'); const { z } = require('zod'); const router = express.Router();
Validating query params with Zod in Express
validation
schema-parsing
query-building
Intermediate
9 steps
java
public List<String> collectTagsFromArticles(List<Article> articles) { return articles.stream() .flatMap(article -> article.getTags().stream()) .map(String::trim)
Flattening nested data with Java streams
streams
flatmap
collectors
Intermediate
7 steps
python
from datetime import datetime, date from decimal import Decimal from flask import Flask, jsonify from flask.json.provider import DefaultJSONProvider
Serializing SQLAlchemy models in Flask JSON
serialization
orm
json
Intermediate
6 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/deriving-jpa-queries-from-method-names-in-spring-explained-java-cd35/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.