question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

generic enum converter dont work [DATAJDBC-566]

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
pwincklescommented, Jan 14, 2022

Yes, I was running against H2 as well. As far as I can tell, your solution works.

0reactions
Ikki-Daicommented, Jan 14, 2022

@pwinckles I was tried on H2db with unit test. it seems convert between enum with jdbcValue directly. I can’t remember more details, but you can tried again with h2db.

Read more comments on GitHub >

github_iconTop Results From Across the Web

generic enum converter dont work [DATAJDBC-566] #787
i want to use a genric converter for all the enums i used in my project. but i got an error. ... =>...
Read more >
Is it possible to write a generic enum converter for JPA?
Based on @scottb solution I made this, tested against hibernate 4.3: (no hibernate classes, should run on JPA just fine). Interface enum ......
Read more >
Converting Strings to Enums in Java - Baeldung
A quick and practical guide to converting strings to enums in Java.
Read more >
Dissecting new generic constraints in C# 7.3
The Enum constraint helps to work-around existing issues with utility methods from System.Enum type, or for creating application-specific “enum ...
Read more >
Room: TypeConverter for Enums [73132006] - Issue Tracker
It would be great if you could add a default type converter for enums. ... automatic conversion to work but that also break...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found