[英]“MappingException: No property null found on entity class java.time.OffsetDateTime to bind constructor parameter to!” with Spring MongoDB
[英]MappingException: "No property found on entity to bind constructor parameter to"?
我用spring数据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;
}
但是当我运行其中一个回购方法时,这个异常被抛出:
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,以便我可以更新我的文档并添加新属性。
使用import org.springframework.data.mongodb.core.mapping.Field;
每个@Field
@Field 注释
检查实体类的构造函数并确保参数名称正确
MongoRepository 定义的自定义查询方法使用构造函数参数名称来定位要在搜索中使用的属性,因此此参数名称必须与 Document 实体属性相同。
虽然不适用于本主题的主题,但在某些情况下,在文档实体中不添加 args 构造函数将解决此错误。
我的问题是,我在实体内部设置了一个属性
ticker.setLastPrice(new LastPriceDbo() {{ setPrice(...) }}
当我改为
var lastPrice = new LastPriceDbo();
lastPrice.setPrice(...)
ticker.setLastPrice(lastPrice);
有效
对于未来的某些人(2022 年 12 月)。
正如之前有人提到的:,,MongoRepository 定义的自定义查询方法使用构造函数参数名称来定位要在搜索中使用的属性,因此此参数名称必须与文档实体属性相同。”
例子:
@Data //Lombok creates constructor and Getter/Setter methods
@Document() //User is Collection class
public class User {
@Id
private String id;
private Gender Gender;
public User(Gender Gender) {
this.Gender = Gender;
}}
通常 IntelliJ 使用小写参数创建构造函数,例如
User(Gender gender){
Gender = gender;
}
改成这样,因为在这种情况下,User class 中的实体名称“Gender”是大写的,所以出于某种原因,Parameter 也应该是大写的。
User(Gender Gender){
this.Gender = Gender;
}
public enum Gender {
MALE, FEMALE
}
只有当我的服务使用 MongoRepository 时,这个问题才在我创建用户时出现:
@AllArgsConstructor
@Service
public class UserService {
private final UserRepository userRepository;
public List<User> getAllUser() {
return userRepository.findAll();
}
}
public interface UserRepository extends MongoRepository<User, String> {
//Optional<User> findUserByEmail(String email);
}
这是我的情况,也许你还有其他问题。 继续前进哥们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.