Class DTOToEntityPathMapper

java.lang.Object
in.co.akshitbansal.springwebquery.pathmapper.DTOToEntityPathMapper
Direct Known Subclasses:
CachedDTOToEntityPathMapper

public class DTOToEntityPathMapper extends Object
Translates DTO-visible selector paths into entity paths used for query construction.

This mapper exists so that filtering and sorting can be expressed against a DTO contract while the underlying query still targets entity attributes. A caller provides a selector path in DTO terms, and the mapper returns the corresponding entity path together with the terminal DTO field that was reached while resolving the selector.

Mapping happens in three stages:

  • the incoming DTO path is resolved reflectively against the DTO class
  • each resolved DTO field contributes one entity path segment, either from its own field name or from MapsTo
  • the assembled entity path is resolved reflectively against the entity class to verify that the DTO mapping configuration is valid

If a DTO field does not declare MapsTo, its own field name is reused as the entity-side segment. If MapsTo is present, its MapsTo.value() is appended instead. When MapsTo.absolute() is true, any previously accumulated parent segments are discarded before the mapped segment is added, allowing a nested DTO field to point to a path rooted elsewhere in the entity graph.

The mapper distinguishes between caller mistakes and developer misconfiguration:

  • if the DTO path itself cannot be resolved, the mapper throws QueryFieldValidationException
  • if the DTO path resolves successfully but the derived entity path does not, the mapper throws QueryConfigurationException because the DTO-to-entity mapping metadata is inconsistent with the entity model
  • Field Details

    • entityClass

      protected final Class<?> entityClass
      Entity type that receives translated selector paths.
    • dtoClass

      protected final Class<?> dtoClass
      DTO type exposed to callers for filtering and sorting.
  • Method Details

    • map

      public DTOToEntityPathMapper.MappingResult map(@NonNull @NonNull String dtoPath)
      Maps a DTO-visible selector path to the corresponding entity path.

      The incoming path is first resolved against the DTO class using ReflectiveFieldResolver. The resolved DTO fields are then processed from left to right to assemble the entity path:

      • without MapsTo, the DTO field name is reused
      • with MapsTo, the annotation value is used instead
      • with MapsTo.absolute() set to true, any previously collected parent segments are cleared before the current mapped segment is appended

      After the entity path string has been assembled, it is resolved against the entity class to ensure the mapping metadata actually points to a valid entity-side path.

      The returned DTOToEntityPathMapper.MappingResult contains both the final entity path and the terminal DTO field. The terminal DTO field is preserved because later validation steps, such as checking filterability or sortability, operate on the DTO contract rather than on the entity field.

      Parameters:
      dtoPath - selector path expressed against the DTO contract
      Returns:
      mapped entity path together with the terminal DTO field
      Throws:
      QueryFieldValidationException - if the DTO path cannot be resolved and is therefore invalid from the caller's perspective
      QueryConfigurationException - if the DTO path resolves but the derived entity path is invalid for the configured entity type