generic enum converter dont work [DATAJDBC-566]
See original GitHub issueedwil13x 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:
- Created 3 years ago
- Comments:6
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Yes, I was running against H2 as well. As far as I can tell, your solution works.
@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.