Skip to content

Introduce ProjectingConverter API #2306

Open
@mp911de

Description

@mp911de

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions