【问题标题】:No converter found capable of converting from type [java.util.LinkedHashMap<?, ?>] to type没有找到能够从类型 [java.util.LinkedHashMap<?, ?>] 转换为类型的转换器
【发布时间】:2020-01-17 16:09:17
【问题描述】:

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

{

  "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

【问题讨论】:

  • 你用过@Modifying(clearAutomatically = true)
  • 在 org.springframework.data.couchbase.core.query 包中不可用。
  • 问题出在返回值Hostel,因为从Spring的反序列化器的角度来看,像"name: {"key": value}"这样的结构是Map。所以首先检查你的实体类Hostel 是否有正确注释的类和属性。作为一个案例,您可以创建自己的反序列化器
  • @Sandro Rey 请将@Field 注释添加到hostel 的属性以及Bucket 实体中的hostel 属性。 Example

标签: java spring-boot couchbase spring-data-couchbase couchbase-java-api


【解决方案1】:

根据Spring Data: Modifying Query

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

  • 无效

  • int(更新的记录数)

  • boolean(记录是否更新)

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-17
    • 2018-02-15
    • 2020-11-19
    • 2019-10-20
    • 2019-02-10
    • 2021-09-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多