Начинающий с Java здесь,
Я пытаюсь определить мою модель для доступа к данным из моей базы данных Mongodb с помощью Spring.
Я бы хотел определить Gender как Enum, где в базе данных значения хранятся в виде строк: male
или female
Но я получаю следующую ошибку:
No enum constant com.nemeantalestudios.mythology.models.Gender.male
Вот моя фигура POJO:
@Getter
@Setter
@Document(collection = "figures")
public class Figure {
@Id
public String id;
public String name;
public String greekName;
public String romanName;
public String description;
public String category;
public String immortal;
public Gender gender;
}
А вот мое перечисление для пола:
public enum Gender {
MALE("male"),
FEMALE("female");
private final String text;
Gender(final String text) {
this.text = text;
}
@Override
public String toString() {
return text;
}
}
Вот как я пытаюсь получить к нему доступ:
@ Override
public void run (String ... args) создает исключение {
Необязательный chronos = figureRepository.findById ("5893a17a88dcfdf6dfa73429");
Список figureSet = figureRepository.findAll ();
System.out.println (figureSet.size ());
chronos.ifPresent(figure -> {
System.out.println(figure.name);
System.out.println(figure.category);
System.out.println(figure.description);
System.out.println(figure.immortal);
System.out.println(figure.romanName);
System.out.println(figure.greekName);
System.out.println(figure.gender.toString());
});
}
В случае вот трассировки стека:
Caused by: java.lang.IllegalArgumentException: No enum constant com.nemeantalestudios.mythology.models.Gender.male
at java.lang.Enum.valueOf(Enum.java:238) ~[na:1.8.0_171]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.getPotentiallyConvertedSimpleRead(MappingMongoConverter.java:884) ~[spring-data-mongodb-2.0.8.RELEASE.jar:2.0.8.RELEASE]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.readValue(MappingMongoConverter.java:1392) ~[spring-data-mongodb-2.0.8.RELEASE.jar:2.0.8.RELEASE]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter$MongoDbPropertyValueProvider.getPropertyValue(MappingMongoConverter.java:1335) ~[spring-data-mongodb-2.0.8.RELEASE.jar:2.0.8.RELEASE]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.readProperties(MappingMongoConverter.java:335) ~[spring-data-mongodb-2.0.8.RELEASE.jar:2.0.8.RELEASE]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:297) ~[spring-data-mongodb-2.0.8.RELEASE.jar:2.0.8.RELEASE]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:253) ~[spring-data-mongodb-2.0.8.RELEASE.jar:2.0.8.RELEASE]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:202) ~[spring-data-mongodb-2.0.8.RELEASE.jar:2.0.8.RELEASE]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:198) ~[spring-data-mongodb-2.0.8.RELEASE.jar:2.0.8.RELEASE]
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:86) ~[spring-data-mongodb-2.0.8.RELEASE.jar:2.0.8.RELEASE]
at org.springframework.data.mongodb.core.MongoTemplate$ReadDocumentCallback.doWith(MongoTemplate.java:2785) ~[spring-data-mongodb-2.0.8.RELEASE.jar:2.0.8.RELEASE]
at org.springframework.data.mongodb.core.MongoTemplate.executeFindOneInternal(MongoTemplate.java:2401) ~[spring-data-mongodb-2.0.8.RELEASE.jar:2.0.8.RELEASE]
at org.springframework.data.mongodb.core.MongoTemplate.doFindOne(MongoTemplate.java:2193) ~[spring-data-mongodb-2.0.8.RELEASE.jar:2.0.8.RELEASE]
at org.springframework.data.mongodb.core.MongoTemplate.findById(MongoTemplate.java:797) ~[spring-data-mongodb-2.0.8.RELEASE.jar:2.0.8.RELEASE]
at org.springframework.data.mongodb.repository.support.SimpleMongoRepository.findById(SimpleMongoRepository.java:121) ~[spring-data-mongodb-2.0.8.RELEASE.jar:2.0.8.RELEASE]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_171]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_171]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_171]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_171]
at org.springframework.data.repository.core.support.RepositoryComposition$RepositoryFragments.invoke(RepositoryComposition.java:377) ~[spring-data-commons-2.0.8.RELEASE.jar:2.0.8.RELEASE]
at org.springframework.data.repository.core.support.RepositoryComposition.invoke(RepositoryComposition.java:200) ~[spring-data-commons-2.0.8.RELEASE.jar:2.0.8.RELEASE]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$ImplementationMethodExecutionInterceptor.invoke(RepositoryFactorySupport.java:629) ~[spring-data-commons-2.0.8.RELEASE.jar:2.0.8.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) ~[spring-aop-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:593) ~[spring-data-commons-2.0.8.RELEASE.jar:2.0.8.RELEASE]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:578) ~[spring-data-commons-2.0.8.RELEASE.jar:2.0.8.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) ~[spring-aop-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:59) ~[spring-data-commons-2.0.8.RELEASE.jar:2.0.8.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) ~[spring-aop-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) ~[spring-aop-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) ~[spring-aop-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:61) ~[spring-data-commons-2.0.8.RELEASE.jar:2.0.8.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) ~[spring-aop-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212) ~[spring-aop-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at com.sun.proxy.$Proxy50.findById(Unknown Source) ~[na:na]
at com.nemeantalestudios.mythology.MythologyApplication.run(MythologyApplication.java:31) [classes/:na]
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:797) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
Что я делаю не так?