spring-projects / spring-projects/spring-data-commons

Introduce ProjectingConverter API

Open
#2,306 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

status: pending-design-work
Dominant language
Java
Stars
838
Forks
730
PR merge metrics
No merged PRs in 30d

Description

Spring Data projections can be either interface or DTO projections. In a lot of projects, we use an EntityConverter (EntityReader) to either materialize a backing entity or directly the DTO. The signature of R read(Class<R> targetType, S source) is limiting in the regard that the target type in case of a DTO cannot apply type hints and field name mappings of the underlying persistent entity.

In the case of interfaces, the backing entity must support a partial population which introduces nullability to the actual entity. Especially Kotlin users are required to apply workarounds as Kotlin defaults to non-null behavior.

Ideally, we could introduce a new interface for converters to implement that allows mapping a source object (row, document, …) into a target projection. The projection mapping needs to consider:

  • Target projection type (DTO, interface projection)
  • Underlying entity (to apply type/field name customizations)

Implementation-wise, interface projections could be backed by a Map instead of materializing a partially populated object. In that case, the converter needs to be aware of whether particular properties should be exposed as an actual entity or as nested projection to create the appropriate backing data structure.

Store overview:

  • Neo4j applies some of the field name customizations to their projections (thanks @michael-simons for the discussion)
  • JPA uses constructor/DTO creation and tuple queries to retrieve the correct representation from a query
  • MongoDB, Cassandra, and R2DBC materialize entities for interface projections and read DTO projects directly into the DTO without considering field/column names

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the EntityConverter and EntityReader APIs and reviewing how projections are handled across the Neo4j, JPA, MongoDB, Cassandra, and R2DBC implementations. Compare their handling of DTOs, interface projections, type hints, and field-name mappings. Done means agreeing on and implementing a shared ProjectingConverter API with compatible store-specific behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
backend-api-design
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.