java - 如何在不可变类中使用带有参数的构造函数 NO_CONSTRUCTOR 来修复 "Failed to instantiate ' className'

标签 java spring mongodb

我在 Spring Boot 中使用 MongoDBRepository,当我在数据库中保存一些对象时,一切正常。但是当我通过 id 找到对象时,spring 不允许这样做。

我尝试将VehicleRoutingProblemSolution类型更改为Object类型,但是VehicleRoutingProblemSolution有其他对象字段PickupService并且它没有默认构造函数。是的,这个类是不可变的...我无法创建默认构造函数,我该怎么办?

import com.fasterxml.jackson.annotation.JsonProperty;
import com.graphhopper.jsprit.core.problem.solution.VehicleRoutingProblemSolution;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

@Document(collection = "vrp_solutions")
public class VrpSolutionHolder {
    // Specifies the solution id
    @Id
    @JsonProperty("id")
    private String id;

    // Specifies the solution id
    @JsonProperty("solution")
    private VehicleRoutingProblemSolution vehicleRoutingProblemSolution;

    // Created at timestamp in millis
    @JsonProperty("created_at")
    private Long created_at = System.currentTimeMillis();


    public VrpSolutionHolder(String id, VehicleRoutingProblemSolution vehicleRoutingProblemSolution) {
        this.id = id;
        this.vehicleRoutingProblemSolution = vehicleRoutingProblemSolution;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public VehicleRoutingProblemSolution getVehicleRoutingProblemSolution() {
        return vehicleRoutingProblemSolution;
    }

    public void setVehicleRoutingProblemSolution(VehicleRoutingProblemSolution vehicleRoutingProblemSolution) {
        this.vehicleRoutingProblemSolution = vehicleRoutingProblemSolution;
    }

    public Long getCreated_at() {
        return created_at;
    }

    public void setCreated_at(Long created_at) {
        this.created_at = created_at;
    }
}

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.data.mapping.model.MappingInstantiationException: Failed to instantiate com.graphhopper.jsprit.core.problem.solution.VehicleRoutingProblemSolution using constructor NO_CONSTRUCTOR with arguments

最佳答案

我遇到了完全相同的问题。包含其他类实例的持久不可变类,通过此存储库方法检索时抛出上述异常:

public interface ProjectCodeCacheRepository extends MongoRepository<CachedCode, String> {
    public CachedCode findByCode(String code);  
    public List<CachedCode> findByClientId(UUID clientId);
}
...
List<CachedCode> cachedForClient = this.codeCacheRepo.`**findByClientId**`(clientId);
...

根据 Erwin Smouts 的提示,通过给它一个特殊的构造函数注释 org.springframework.data.annotation.PersistenceConstructor 可以很好地解决这个问题,如下所示:

@Document(collection="cachedcodes")
public class CachedCode {
    
    @PersistenceConstructor
    public CachedCode(String code, UUID clientId, LocalDateTime expiration) {
        this.code = code;
        this.clientId = clientId;
        this.expiration = expiration;
    }
    
    public CachedCode(String code, UUID clientId, long secondsExpiring) {
        this.code = code;
        this.clientId = clientId;
        this.expiration = LocalDateTime.now().plusSeconds(secondsExpiring);
    }
    
    
    public UUID getClientId( ) {
        return this.clientId;
    }
            
    public String getCode() {
        return this.code;
    }
        
    public boolean hasExpired(LocalDateTime now) {
        return (expiration.isBefore(now));
    }
    
    ...
    @Id
    private final String code;
    private final UUID clientId;
    private final LocalDateTime expiration;
}

因此,您应该检查您的 VehicleRoutingProblemSolution 是否具有 a) 一个与数据库字段匹配的构造函数(在 mongo 客户端中检查)和 b) 被注释为驱动程序使用的构造函数(或引擎盖下的任何 Spring 魔法) )。

关于java - 如何在不可变类中使用带有参数的构造函数 NO_CONSTRUCTOR 来修复 "Failed to instantiate ' className',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55827640/

相关文章:

java - IAIK PKCS11 Wrapper 中的私钥和证书对如何匹配?

java - Kotlin 初始化一个对象

java - Java 类加载的解析阶段实际上从哪里开始?

java - 带有 Freemarker 的 Netbeans 模板 : built-in with argument fails

java - 是否可以让 ObjectProvider 提供来自其他包的对象?

hibernate - 带有 HSQLDB 的 TDD -- 删除外键

spring - 为可与@PropertySource 一起使用的 AbstractAnnotationConfigDispatcherServletInitializer 设置事件配置文件?

mongodb - Ubuntu 时区已更改,但 mongodb 仍根据以前的时区打印日期时间

Node.js:无法返回 Mongoose 查找结果

mongodb - 如何在 MongoDB 中执行条件更新插入