ProjectionとDTO
必要なデータだけを選択します
「ProjectionとDTO」はCoddyKit上の無料Spring Boot 4 Microservices & REST APIsレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはSpring Boot 4 Microservices & REST APIs学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Spring Boot 4 Microservices & REST APIsコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Why Projections
Fetching whole entities when you need only a few columns wastes memory and bandwidth.
Projections let you select a subset of fields, returning lightweight objects instead of full entities.
Interface Projections
Declare an interface with getters for the fields you want. Spring Data implements it as a proxy.
public interface UserSummary {
String getName();
String getEmail();
}Returning a Projection
Use the interface as the query method return type.
public interface UserRepository
extends JpaRepository<User, Long> {
List<UserSummary> findByActiveTrue();
}Closed vs Open Projections
A closed projection only exposes existing properties (efficient — selects only those columns). An open projection computes values with @Value and SpEL (loads the whole entity).
Open Projection Example
Combine fields with a SpEL expression.
public interface UserView {
@Value("#{target.firstName + ' ' + target.lastName}")
String getFullName();
}Class-Based Projections (DTOs)
A DTO class with a matching constructor is another projection style. Spring Data maps query columns to constructor parameters by name.
public class UserDto {
private final String name;
private final String email;
public UserDto(String name, String email) {
this.name = name;
this.email = email;
}
// getters
}Returning a DTO
Use the DTO class as the return type, just like interface projections.
public interface UserRepository
extends JpaRepository<User, Long> {
List<UserDto> findByActiveTrue();
}DTOs in JPQL Constructor Expressions
With custom JPQL, use a constructor expression to build DTOs directly.
@Query("SELECT new com.app.UserDto(u.name, u.email) "
+ "FROM User u WHERE u.active = true")
List<UserDto> findActiveSummaries();Dynamic Projections
Make the return type generic so one method serves multiple projections.
<T> List<T> findByActiveTrue(Class<T> type);
// usage
repo.findByActiveTrue(UserSummary.class);
repo.findByActiveTrue(UserDto.class);Projections with Native Queries
Interface projections also work with native SQL queries when column names match the getters.
@Query(value = "SELECT name, email FROM users",
nativeQuery = true)
List<UserSummary> findAllSummaries();Choosing an Approach
Prefer closed interface projections for read-only lists (most efficient). Use DTO classes when you need behavior or want them outside the persistence layer.
Quick Check
Test your understanding of projections.
Recap
You learned to return lightweight results:
- Interface projections — closed (efficient) or open (SpEL)
- Class-based DTO projections via constructor mapping
- JPQL constructor expressions for custom queries
- Dynamic projections with a generic type parameter
よくある質問
「ProjectionとDTO」レッスンは無料ですか?
はい。「ProjectionとDTO」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Spring Boot 4 Microservices & REST APIsコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Spring Boot 4 Microservices & REST APIsコースには全4レッスンが含まれています。
「ProjectionとDTO」で何を学びますか?
必要なデータだけを選択します ブラウザで直接実行するハンズオンコードでSpring Boot 4 Microservices & REST APIsを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Spring Boot 4 Microservices & REST APIsを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのSpring Boot 4 Microservices & REST APIsは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。
「ProjectionとDTO」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このSpring Boot 4 Microservices & REST APIsレッスンでコードを書いて実行できますか?
はい。すべてのSpring Boot 4 Microservices & REST APIsレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- Specificationによる動的クエリ
- ProjectionとDTO
- @CreatedDateによる監査
- ページネーションとソート