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

zsohkypk  于 2022-11-14  发布在  其他
关注(0)|答案(4)|浏览(88)

我用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,以便更新文档并添加新属性。

1rhkuytd

1rhkuytd1#

1.每个字段使用import org.springframework.data.mongodb.core.mapping.Field;@Field注解
1.检查实体类的构造函数,确保参数名称正确

bpzcxfmw

bpzcxfmw2#

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

cyej8jka

cyej8jka3#

虽然不适用于本主题的主题,但在某些情况下,向文档实体添加任何args构造函数将解决此错误。

zbdgwd5y

zbdgwd5y4#

我的问题是,我通过以下方式在实体内设置了一个属性

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

当我更改为

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

啊,真灵

相关问题