-
Notifications
You must be signed in to change notification settings - Fork 350
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enable converting enums to integer (built-in or custom converter) [DATAJDBC-408] #629
Comments
Jens Schauder commented Thanks for raising this. If we would handle |
I also had this problem. I'm Custom enumeration value ,eg: public enum StateEnum{
ACTIVATED(1),
DEACTIVATED(2);
StateEnum(Integer value) {
this.value = value;
}
private final Integer value;
public int getValue() {
return value;
}
} register converter to JdbcCustomConversions @Slf4j
@WritingConverter
public class StateToIntegerConverter implements Converter<StateEnum,Integer> {
@Override
public Integer convert(StateEnum source) {
return source.getValue();
}
} but always use org.springframework.core.convert.support.EnumToStringConverter Temporary plan @Slf4j
@WritingConverter
public class StateToIntegerConverter implements Converter<StateEnum,String> {
@Override
public String convert(StateEnum source) {
return String.valueOf(source.getValue());
}
} |
We also hit this issue. We have custom integer values associated with enums. We have custom WriteConverter classes. @WritingConverter
static class MyEnumWritingConverter implements Converter<MyEnum, Integer> {
@Override
public Integer convert(@NonNull MyEnum e) {
return e.getDbIndex();
}
} |
Proposed workaround in Spring-Boot 2.6.2: |
Outline that the usage of this annotation leads to calling Results.getRowsUpdated() to obtain the query method result value. Closes #629
Thanks for finding this. Yes I think this should be considered resolved. Leaving it open for a bit for the OP to veto a close. |
Closing this as solved as indicated by the comment above. |
@schauder |
We might have closed that prematurely. |
This problem exists in Spring Data JDBC and plain Spring JDBC 3.1.5. Workaround for Spring JDBC i.e. NOT using In the above mentioned method Unfortunately this does not work for Spring Data JDBC because by the time the above method is called Enum types have been cast to type String due to the proxy objects created by Spring Data for each Following the code through is can be seen that Enum.class is always mapped to String.class. Currently I don't know a work around for Spring Data JDBC. |
Another solution is to use JdbcValue as the target type. Then the writer will look something like this:
|
I tried to write a test verifying this problem, but found that there is already a set of test that tests reading, writing and querying enums with custom converters: Line 136 in fbc3bf0
So the issue does seem to be resolved as far as I can tell. If anyone still has an issue with this please create a new issue and provide a reproducer. |
Fábio Coutinho Valente opened DATAJDBC-408 and commented
I'm changing my application from Spring Data JPA to Spring Data JDBC, and I have one column on my table that saves an integer of the ordinal.
According to documentation Spring Data JDBC only allows converting enums to string out of the box. So I tried a workaround with custom converters.
I created a custom converter with
@ReadingConverter
and could read/convert properly to my enum value according to the integer on my table.I tried creating a custom converter with
@WritingConverter
annotation, but its not being executed instead the converter org.springframework.core.convert.support.EnumToStringConverter is being called.After some debugging I found the map javaToDbType in class BasicRelationalPersistentProperty to be the resposible for that due to this static line
If there is no built-in convertion enum to integer, could you allow custom converter to be used instead?
Is there any other way to convert enum values into integer (ordinal number)?
2 votes, 3 watchers
The text was updated successfully, but these errors were encountered: