spring-projects / spring-projects/spring-data-relational

generic enum converter dont work [DATAJDBC-566]

Open
#787 8 comments 0 reactions 1 assignee View on GitHub

@schauder is already working on this.

Since Dec 31, 2020.

type: bug
Dominant language
Java
Stars
827
Forks
394
PR merge metrics
No merged PRs in 30d

Description

edwil13x opened DATAJDBC-566 and commented

i want to use a genric converter for all the enums i used in my project. but i got an error.

Explanations :

Given this enum 

public enum Gender  {
    MALE("male"), FEMALE("female"), NON_BINARY("non-binary");

    private String gender;

    Gender(String gender) {
        this.gender = gender;
    }

    
    public String toString() {
        return gender;
    }
}

and this jdbcConfig

 


@Configuration
public class DataJdbcConfiguration extends AbstractJdbcConfiguration {

 
public <T extends Enum<T>> Converter<String, T> createEnumReadingConverter(Class<T> enumClass) { return new Converter<String, T>() { @Override public T convert(String value) { for(T constant : enumClass.getEnumConstants()) { if(constant.toString().equalsIgnoreCase(value)) return constant; } throw new IllegalArgumentException("Unable to find Enum type for : " + value); } } ; }

    @Override
    public JdbcCustomConversions jdbcCustomConversions() {
   
        Converter<String, Gender> genericGenderConverter =  createEnumReadingConverter(Gender.class);
        return new JdbcCustomConversions(Arrays.asList(genericGenderConverter));
    }

}

 

=> why my genericGenderConverter is not use at all, and spring data back to the default enum converter.

 

 

 


Affects: 2.0.1 (Neumann SR1)

1 votes, 2 watchers

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.