MappingException: "No property found on entity to bind constructor parameter to"?

Viewed 11001

I developed my project with spring data mongodb, and used to have this document:

@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....

Now I need to add some property to my document as bellow:

....

private Long from;

private Long to;

private Long OpenHourfrom;

private Long OpenHourTo;

private Boolean isActive;

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

so I have this new constructor:

    @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;
}

but when I run one of repo methods this exception had thrown:

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 ....

Note that I use spring-confix.xml with bellow settings:

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

I wonder how can I set auto update property of hibernate spring to true, so that I could update my document and add new properties.

4 Answers

The MongoRepository defined custom query methods use the constructor parameter names for locate the property to use in search, so this parameters names must be have the same same as the Document entity properties.

Although not applicable to the subject of this topic, in some cases, adding no args constructor to the document entity will solve this error.

my problem was, that I set a property inside the entity by

ticker.setLastPrice(new LastPriceDbo() {{ setPrice(...) }}

when I changed to

var lastPrice = new LastPriceDbo();
lastPrice.setPrice(...)
ticker.setLastPrice(lastPrice);

it worked

Related