Skip to content

Adding custom converter to Projection #2335

Open
@martashorak

Description

@martashorak

Hi,
in previous versions it was possible to register custom converter for proxy projections via shared instance:
https://github.com/spring-projects/spring-data-commons/blob/2.3.x/src/main/java/org/springframework/data/projection/ProxyProjectionFactory.java#L64

However in most recent version (2.4.x) it is not possible anymore as new instance is used instead of shared one. What is the correct way to register custom converter?

final static GenericConversionService CONVERSION_SERVICE = new DefaultConversionService();

Thank you
Martin

Activity

mp911de

mp911de commented on Mar 22, 2021

@mp911de
Member

We never intended to allow converter registration in the first place. They kind-of worked by accident.

Can you help us understand your use-case, why you want to register a converter and what conversion you try to make work?

martashorak

martashorak commented on Mar 22, 2021

@martashorak
Author

We never intended to allow converter registration in the first place. They kind-of worked by accident.

Can you help us understand your use-case, why you want to register a converter and what conversion you try to make work?

I see, i was following example from stack overflow and it is connected to native query which is also my case:
https://stackoverflow.com/questions/58417190/spring-data-jpa-custom-type-conversion-in-interface-based-projection

Here you can try simple failing test for it:
https://github.com/martashorak/spring-data-projection-test/blob/master/src/test/java/com/example/demoprojection/ProjectionConverterTest.java
If you change version of parent pom to 2.3.9.RELEASE it is passing

joshiste

joshiste commented on Apr 8, 2021

@joshiste

@mp911de We have projections with custom value types for Ids (mostly wrapping String or UUID) this has stopped working:

 interface ObjectPagePosition {
        Integer getRowNumber();

        Boolean getDraft();

        TeamId getTeamId(); // Custom Value Type
    }
markoknez

markoknez commented on Nov 25, 2022

@markoknez

Is there any news on this topic? We are facing the same issue

asiaon123

asiaon123 commented on May 29, 2024

@asiaon123

Also facing this issue, in my case, i want to convert string like 2024-05 to YearMonth

interface:

YearMonth getDate();

repo:

@Query("SELECT DATE_FORMAT(createdAt, '%Y-%m') AS date, COUNT(1) AS count FROM ArticleView GROUP BY date")
List<ArticleViewCountMonth> countByMonth();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @mp911de@asiaon123@joshiste@markoknez@spring-projects-issues

        Issue actions

          Adding custom converter to Projection · Issue #2335 · spring-projects/spring-data-commons