如何修复不可变类中的“无法使用带参数的构造函数 NO_CONSTRUCTOR 实例化‘className’”

tam*_*low 3 java spring mongodb

我在春季启动时使用 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;
    }
}

Run Code Online (Sandbox Code Playgroud)

org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常是 org.springframework.data.mapping.model.MappingInstantiationException:无法使用带参数的构造函数 NO_CONSTRUCTOR 实例化 com.graphhopper.jsprit.core.problem.solution.VehicleRoutingProblemSolution

ono*_*ouv 11

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

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;
Run Code Online (Sandbox Code Playgroud)

}`

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