0Pricing
Spring Boot 4 Microservices & REST APIs · レッスン

Specificationによる動的クエリ

プログラムで柔軟なクエリを構築します

「Specificationによる動的クエリ」はCoddyKit上の無料Spring Boot 4 Microservices & REST APIsレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはSpring Boot 4 Microservices & REST APIs学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Spring Boot 4 Microservices & REST APIsコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

The Problem with Static Queries

When users can filter by many optional fields, writing one query method per combination explodes quickly.

Spring Data Specifications let you build queries dynamically at runtime based on which filters are present.

JpaSpecificationExecutor

Extend JpaSpecificationExecutor on your repository to enable Specification-based queries.

public interface UserRepository
    extends JpaRepository<User, Long>,
            JpaSpecificationExecutor<User> {
}

A Specification

A Specification<T> describes a single WHERE predicate using the JPA Criteria API.

Specification<User> hasName(String name) {
    return (root, query, cb) ->
        cb.equal(root.get("name"), name);
}

Executing a Specification

Pass the spec to findAll.

List<User> users =
    userRepository.findAll(hasName("Alice"));

Combining with and

Specifications compose with and, or, and not.

Specification<User> spec =
    hasName("Alice").and(isActive(true));
List<User> users = userRepository.findAll(spec);

Building Conditionally

The real power: add predicates only when the filter is supplied.

Specification<User> spec = Specification.where(null);
if (name != null) {
    spec = spec.and(hasName(name));
}
if (active != null) {
    spec = spec.and(isActive(active));
}
return userRepository.findAll(spec);

Like and Comparison Predicates

The Criteria builder offers many predicate types.

Specification<User> nameLike(String part) {
    return (root, query, cb) ->
        cb.like(root.get("name"), "%" + part + "%");
}

Specification<User> olderThan(int age) {
    return (root, query, cb) ->
        cb.greaterThan(root.get("age"), age);
}

Joining Related Entities

Specifications can navigate joins to filter on associated entities.

Specification<User> inCity(String city) {
    return (root, query, cb) ->
        cb.equal(root.join("address").get("city"), city);
}

Specifications with Paging

Combine a Specification with a Pageable for filtered, paginated results.

Page<User> page = userRepository.findAll(
    spec, PageRequest.of(0, 20));

Counting Matches

count accepts a Specification too.

long matches = userRepository.count(hasName("Alice"));

Reusing Predicate Factories

Group spec factories in a helper class so they can be reused and unit tested.

public class UserSpecs {
    public static Specification<User> active() {
        return (root, q, cb) ->
            cb.isTrue(root.get("active"));
    }
}

Quick Check

Test your understanding of Specifications.

Recap

You learned dynamic querying:

  • Extend JpaSpecificationExecutor
  • A Specification is one Criteria predicate
  • Compose with and/or, add conditionally
  • Combine with Pageable for filtered pages

よくある質問

「Specificationによる動的クエリ」レッスンは無料ですか?

はい。「Specificationによる動的クエリ」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Spring Boot 4 Microservices & REST APIsコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Spring Boot 4 Microservices & REST APIsコースには全4レッスンが含まれています。

「Specificationによる動的クエリ」で何を学びますか?

プログラムで柔軟なクエリを構築します ブラウザで直接実行するハンズオンコードでSpring Boot 4 Microservices & REST APIsを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Spring Boot 4 Microservices & REST APIsを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのSpring Boot 4 Microservices & REST APIsは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。

「Specificationによる動的クエリ」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このSpring Boot 4 Microservices & REST APIsレッスンでコードを書いて実行できますか?

はい。すべてのSpring Boot 4 Microservices & REST APIsレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. Specificationによる動的クエリ
  2. ProjectionとDTO
  3. @CreatedDateによる監査
  4. ページネーションとソート
← Spring Boot 4 Microservices & REST APIsに戻る