为什么我得到 "No property node found on entity class … to bind constructor parameter to!" 和 Spring 数据 MongoDB

Why do I get a "No property node found on entity class … to bind constructor parameter to!" with Spring Data MongoDB

尝试将 JodaTime 的 LocalDate 与 Spring 数据 MongoDB 和 Spring 4 一起使用,但出现以下异常:

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)
    …

这是我的配置:

<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();
        }
    }
}

你必须让你的内部 classes static 在他们需要一个封闭的 class MongoDBConverters 的实例之前可用之前建。制作这个 class static 不需要那个。

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();
        }
    }
}

有关更多信息,请查看 Java inner class and static nested class

显然我不能有这样的构造函数:

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());
        }
}

直接分配工作的普通人:

public Price(String companyId, LocalDate date, double closePrice, double volume) {
    this.companyId = companyId;
    this.date = date;
    this.closePrice = closePrice;
    this.volume = volume;
}