Hi
I'm currently using Spring data for mongodb version 1.1.1.RELEASE. We currently have a document that has an enum as a property, however the value of the enums are not the same as the enum name, i.e. ACTIVE("active"). Therefore when storing this information in mongo the lower case value is stored which meant that when reading the object from mongodb the Enum.valueOf("active") would fail as the enums are all upper case. Therefore I decided that I would need a converter to achieve this. I created a read converter: -
Where "Status" is the enum. I've added the custom converter to the mongoTemplate using the usual
Now, I can see the mapping converters are there, however for some when querying the documents using a different field its trying to invoke the generic StringToEnum converted on a completely different field resulting in the following exception: -
where PTM is the value of a regular String field. I'm not sure why its trying to convert this string field to an enum value when the property isn't defined as being of type Status.....Its very confusing
I hope this is clear
Thanks
Darrell
I'm currently using Spring data for mongodb version 1.1.1.RELEASE. We currently have a document that has an enum as a property, however the value of the enums are not the same as the enum name, i.e. ACTIVE("active"). Therefore when storing this information in mongo the lower case value is stored which meant that when reading the object from mongodb the Enum.valueOf("active") would fail as the enums are all upper case. Therefore I decided that I would need a converter to achieve this. I created a read converter: -
Code:
public class StatusReadConverter implements Converter<String, Status> {
@Override
public Status convert(String source) {
Status status = Status.fromString(source);
return status;
}
}
Code:
<mongo:mapping-converter>
<mongo:custom-converters>
<mongo:converter>
<bean class="com.sap.ca.cps.usermanagement.db.converters.StatusReadConverter"/>
</mongo:converter>
</mongo:custom-converters>
</mongo:mapping-converter>
Code:
Failed to convert from type java.lang.String to type com.sap.ca.cps.usermanagement.User$Status for value 'PTM'; nested exception is java.lang.IllegalArgumentException: No enum const class com.sap.ca.cps.usermanagement.User$Status.PTM
at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:41)
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:171)
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:154)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.convertToMongoType(MappingMongoConverter.java:825)
at org.springframework.data.mongodb.repository.query.ConvertingParameterAccessor.getConvertedValue(ConvertingParameterAccessor.java:118)
at org.springframework.data.mongodb.repository.query.ConvertingParameterAccessor.getBindableValue(ConvertingParameterAccessor.java:93)
at org.springframework.data.mongodb.repository.query.StringBasedMongoQuery.getParameterWithIndex(StringBasedMongoQuery.java:100)
at org.springframework.data.mongodb.repository.query.StringBasedMongoQuery.replacePlaceholders(StringBasedMongoQuery.java:93)
at org.springframework.data.mongodb.repository.query.StringBasedMongoQuery.createQuery(StringBasedMongoQuery.java:65)
at org.springframework.data.mongodb.repository.query.AbstractMongoQuery.execute(AbstractMongoQuery.java:75)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:313)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at $Proxy55.findByIdentifier(Unknown Source)
at com.sap.ca.cps.usermanagement.db.MongoUserDao.findByIdentifier(MongoUserDao.java:236)
I hope this is clear
Thanks
Darrell