MappingException:“在实体上找不到绑定构造函数参数的属性”?

时间:2017-11-20 07:41:47

标签: java spring mongodb hibernate spring-data-mongodb

我使用spring data mongodb开发了我的项目,并且曾经有过这个文档:

@Document(collection="Instrument")
public class Instrument {
@Id
private Integer id;

private String name;

private String internalCode;

private String fosMarketId;

private String localCode;

//setters...getters... and constructurs....

现在我需要在文档中添加一些属性,如下所示:

....

private Long from;

private Long to;

private Long OpenHourfrom;

private Long OpenHourTo;

private Boolean isActive;

//setters...getters... and constructurs....

所以我有了这个新的构造函数:

    @PersistenceConstructor
public Instrument(Integer id, String name, String internalCode, String fosMarketId, String localCode, Long from,
        Long to, Long openHourfrom, Long openHourTo, Boolean isActive) {
    super();
    this.id = id;
    this.name = name;
    this.internalCode = internalCode;
    this.fosMarketId = fosMarketId;
    this.localCode = localCode;
    this.from = from;
    this.to = to;
    this.OpenHourfrom = openHourfrom;
    this.OpenHourTo = openHourTo;
    this.isActive = isActive;
}

但是当我运行一个repo方法时抛出了这个异常:

org.springframework.data.mapping.model.MappingException: No property openHourfrom found on entity class com.tosan.entity.Instrument to bind constructor parameter to!
at org.springframework.data.mapping.model.PersistentEntityParameterValueProvider.getParameterValue(PersistentEntityParameterValueProvider.java:74)
at ....

请注意,我使用带有以下设置的spring-confix.xml:

    <mongo:mongo-client 
    host="IP" port="Port"  >
<mongo:client-options write-concern="NORMAL" 
            connections-per-host="1000"
                threads-allowed-to-block-for-connection-multiplier="600"
                connect-timeout="10000"
                max-wait-time="15000"                   
                socket-keep-alive="true"
                socket-timeout="15000"
    />
</mongo:mongo-client>

我想知道如何将hibernate spring的自动更新属性设置为true,以便我可以更新我的文档并添加新属性。

2 个答案:

答案 0 :(得分:0)

1使用import org.springframework.data.mongodb.core.mapping.Field; @Filed anotaion for each filed

2检查实体类的构造函数并确保参数名称为corrcect

答案 1 :(得分:0)

MongoRepository 定义的自定义查询方法使用构造函数参数名称来定位要在搜索中使用的属性,因此该参数名称必须与 Document 实体属性相同。