首页 > 解决方案 > 找不到能够从类型 [java.util.LinkedHashMap ]转换为类型的转换器

问题描述

我在沙发数据库中有这样的文件

{

  "hostel": {
    "city": {}
    ...
  }
}

我在存储库中定义了这个查询

@Query("UPDATE #{#n1ql.bucket} SET hostel.state = $2 where hostel.id=$1 RETURNING #{#n1ql.bucket}.hostel    ")
Hostel update(String entityId, String newState);

但是当我运行它时,我得到了这个错误:

org.springframework.core.convert.ConverterNotFoundException: 
No converter found capable of converting from type [java.util.LinkedHashMap<?, ?>] to type [com.model.Hostel]

和旅馆类型:

@Data
@Builder
@AllArgsConstructor
@JsonInclude(NON_NULL)
@EqualsAndHashCode
public class Hostel {

    private @NotNull String id;
    private @NotNull String city;
    private JsonObject commodities;
}

我也尝试过:

@Query("UPDATE #{#n1ql.bucket} SET hostel.state = $2 where hostel.id=$1 RETURNING #{#n1ql.bucket}.hostel    ")
void update(String entityId, String newState);

但后来我得到了错误:

org.springframework.data.couchbase.core.CouchbaseQueryExecutionException: Query returning a primitive type are expected to return exactly 1 result, got 0

标签: javaspring-bootcouchbasespring-data-couchbasecouchbase-java-api

解决方案


根据Spring Data:修改查询

您可以指定以下返回类型:

  • 空白

  • int(更新的记录数)

  • 布尔值(记录是否更新)

在您的情况下,您有返回类型Hostel,如果您使用SpringData. Spring 无法将查询执行的结果转换为Hostel类型。尝试使用前面指定的类型之一


推荐阅读