1

I have a projection interface used in a native query in Spring JPA, one of the values ​​mapped as String is an enum inside Java.

I would like to get the enum description field based on the string returned from the database in my projection interface, I thought of a custom annotation for that, but I didn't come up with a construction idea.

Can anyone share a solution to this scenario.

Ex My Enum

@AllArgsConstructor
@Getter
public enum PersonType {

    PHYSICAL_PERSON("F","Physical Person"),
    LEGAL_PERSON("J","Legal Person");
    
    private String code;
    private String description;

}

My inteface projection

public interface PersonProjection {

    Long getId();
    String getName();
    String getObs();
    String getPersonType();
}

My repository

@Query(value = "select p.id as id, p.description as name, p.obs as obs, p.type as personType from public.person p where p.code = :code", nativeQuery = true)
List<PersonProjection> findByCode(@Param("code") Long code);

My interface is being returned from the database like this:

id=1,name="Mike",obs="Tests",personType="F";

Is there any way in this projection interface I can use an annotation where I can get the description of the enum by the value of the returned string

Ex:

public interface PersonProjection {

    Long getId();
    String getName();
    String getObs();
    @DescriptionEnum(PersonType.class)
    String getPersonType();
}

In the domain class with the JPA annotations I can, but since I'm out, returning the data in an interface like a DTO, I can't get the same resource.

Ger
  • 583
  • 2
  • 13
  • 35
  • possible duplicate https://stackoverflow.com/questions/58417190/spring-data-jpa-custom-type-conversion-in-interface-based-projection – J Asgarov Jan 05 '22 at 07:42

0 Answers0