Trying to use JodaTime's LocalDate
with Spring Data MongoDB and Spring 4 but I am getting the following exception:
SEVERE: Servlet.service() for servlet [dispatcher] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.data.mapping.model.MappingException: No property node found on entity class com.th.model.Price to bind constructor parameter to!] with root cause
org.springframework.data.mapping.model.MappingException: No property node found on entity class com.th.model.Price to bind constructor parameter to!
at org.springframework.data.mapping.model.PersistentEntityParameterValueProvider.getParameterValue(PersistentEntityParameterValueProvider.java:74)
at org.springframework.data.mapping.model.SpELExpressionParameterValueProvider.getParameterValue(SpELExpressionParameterValueProvider.java:63)
at org.springframework.data.convert.ReflectionEntityInstantiator.createInstance(ReflectionEntityInstantiator.java:71)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:249)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:230)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:190)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:186)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:77)
at org.springframework.data.mongodb.core.MongoTemplate$ReadDbObjectCallback.doWith(MongoTemplate.java:2121)
at org.springframework.data.mongodb.core.MongoTemplate.executeFindMultiInternal(MongoTemplate.java:1805)
at org.springframework.data.mongodb.core.MongoTemplate.doFind(MongoTemplate.java:1628)
at org.springframework.data.mongodb.core.MongoTemplate.doFind(MongoTemplate.java:1611)
at org.springframework.data.mongodb.core.MongoTemplate.find(MongoTemplate.java:535)
at org.springframework.data.mongodb.core.MongoTemplate.find(MongoTemplate.java:526)
at com.th.model.Company.getPrices(Company.java:255)
at com.th.controller.Home.getPrices(Home.java:32)
…
Here my configuration:
<mongo:db-factory id="mongoDbFactory" dbname="th" host="localhost"/>
<mongo:mapping-converter id="mappingConverter">
<mongo:custom-converters>
<mongo:converter>
<bean class="com.th.tool.MongoDBConverters.DateToLocalDateConverter"/>
</mongo:converter>
<mongo:converter>
<bean class="com.th.tool.MongoDBConverters.LocalDateToDateConverter"/>
</mongo:converter>
</mongo:custom-converters>
</mongo:mapping-converter>
<bean class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/>
<constructor-arg name="mongoConverter" ref="mappingConverter"/>
</bean>
public class MongoDBConverters {
public static class DateToLocalDateConverter implements Converter<Date, LocalDate> {
@Override
public LocalDate convert(Date source) {
return source == null ? null : LocalDate.fromDateFields(source);
}
}
public static class LocalDateToDateConverter implements Converter<LocalDate, Date> {
@Override
public Date convert(LocalDate source) {
return source == null ? null : source.toDate();
}
}
}
Apparently I can't have a constructor like this:
public Price(String companyId, JsonNode node) {
this.companyId = companyId;
if(node != null) {
this.date = Tools.getLocalDate(node.get("Date").getTextValue());
this.closePrice = Double.parseDouble(node.get("Close").getTextValue());
this.volume = Double.parseDouble(node.get("Volume").getTextValue());
}
}
A normal one with direct assignment work:
public Price(String companyId, LocalDate date, double closePrice, double volume) {
this.companyId = companyId;
this.date = date;
this.closePrice = closePrice;
this.volume = volume;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With