Search code examples
javaspring-bootcouchbasespring-data-couchbasecouchbase-java-api

No converter found capable of converting from type [java.util.LinkedHashMap<?, ?>] to type


I have documents like this in a couchbase DB

{

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

and I have this query defined in a repository

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

but when I run it I got this error:

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

and the Hostel type:

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

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

I also tried with:

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

but then I got the error:

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

Solution

  • According to Spring Data: Modifying Query

    You can specify the following return types:

    • void

    • int (updated record count)

    • boolean(whether a record was updated)

    In your case you have return type Hostel what is not good if you are using SpringData. Spring can not convert result of query execution to Hostel type. Try to use one of type specified earlier