1Lan chọn PAID, tổng từ 300 nghìn và giao Hà Nội để gom đơn cho chuyến sáng; khoảng ngày bỏ trống. Controller gọi OrderSearchService.search(filter, page).Lan selects PAID, a minimum total of 300,000 VND and Hanoi delivery for the morning run; the date range is blank. The controller calls OrderSearchService.search(filter, page).
2Service chỉ thêm spec cho bộ lọc đã điền: hasStatus(PAID), totalAtLeast(300000), shippedToCity("Hà Nội"). placedBetween bị bỏ vì from và to đều null. Chọn spec chưa chạy truy vấn nào.The service adds specs only for filled filters: hasStatus(PAID), totalAtLeast(300000), shippedToCity("Hà Nội"). placedBetween is skipped because from and to are null. Choosing specs runs no query yet.
3Mỗi spec là một lambda (root, query, cb) cài đặt Specification<Order>: hasStatus dùng cb.equal, totalAtLeast dùng cb.greaterThanOrEqualTo, shippedToCity join shippingAddress và so sánh lower(city).Each spec is a (root, query, cb) lambda implementing Specification<Order>: hasStatus uses cb.equal, totalAtLeast uses cb.greaterThanOrEqualTo, shippedToCity joins shippingAddress and compares lower(city).
4Service ghép bằng Specification.allOf(specs) (AND) rồi gọi orders.findAll(combined, page). OrderRepository nhận được spec vì kế thừa JpaSpecificationExecutor; không cần method riêng cho tổ hợp này.The service combines them with Specification.allOf(specs) (AND) and calls orders.findAll(combined, page). OrderRepository accepts specs because it extends JpaSpecificationExecutor; no dedicated method is needed for this combination.
5JPA provider sinh SQL từ các Predicate và gửi tới PostgreSQL; giá trị đi qua tham số nên đầu vào không thành cú pháp SQL. Đơn PAID 420 nghìn giao Hà Nội được chọn, đơn 250 nghìn bị loại. Trong SpecificationDemo không có database: InMemoryOrderRepository đưa spec cho InMemoryCriteria, nó gọi toPredicate với một CriteriaBuilder giả và tính Predicate thẳng trên danh sách Order.The JPA provider generates SQL from the predicates and sends it to PostgreSQL; values travel as parameters, so input cannot become SQL syntax. A PAID 420,000 VND Hanoi order matches, a 250,000 VND one does not. SpecificationDemo has no database: InMemoryOrderRepository hands the spec to InMemoryCriteria, which calls toPredicate with a fake CriteriaBuilder and evaluates the Predicate directly on a list of Orders.
6Quy tắc nghiệp vụ được đặt tên một lần trong OrderSpecs.refundable(since): hasStatus(PAID).and(placedBetween(since, now)).and(not(hasStatus(SHIPPED))). Kết quả vẫn là một Specification<Order>, dùng được với findAll như mọi spec khác.A business rule is named once in OrderSpecs.refundable(since): hasStatus(PAID).and(placedBetween(since, now)).and(not(hasStatus(SHIPPED))). The result is still a Specification<Order>, usable with findAll like any other spec.
7Bản Go SQL: SpecFromQuery chỉ thêm search.HasStatus và search.TotalAtLeast cho tham số có mặt, rồi search.And ghép thành Spec{SQL, Args}. OrderRepo.Find nối thêm LIMIT ?, clone Args, gọi Rebind đổi ? thành $1, $2, $3 rồi pool.Query.Go SQL: SpecFromQuery adds search.HasStatus and search.TotalAtLeast only for present parameters, then search.And builds Spec{SQL, Args}. OrderRepo.Find appends LIMIT ?, clones Args, calls Rebind to turn ? into $1, $2, $3, then pool.Query.
8Bản Go trong bộ nhớ: Rule[T] là func(T) bool có And / Or. FreeShipping = HasStatus("PAID").And(TotalAtLeast(300_000)).Or(IsVIP), và Filter(orders, FreeShipping) giữ đơn thoả quy tắc mà không cần database.Go in memory: Rule[T] is a func(T) bool with And / Or. FreeShipping = HasStatus("PAID").And(TotalAtLeast(300_000)).Or(IsVIP), and Filter(orders, FreeShipping) keeps the orders that satisfy it without a database.