0

I am trying to map the result of a couchbase query to a java reference type, so far I have found no way to do this. How can I capture the following as a java reference type:

N1qlQueryResult result = couchbaseBucket.query(
            N1qlQuery.simple("SELECT * FROM customers LIMIT 1"));

JsonObject cust = result.allRows().get(0).value();

How can I cast this 'cust' to a java object? What would be the best way of doing this, doesnt the couchbase SDK provide some solution to this?

Carlos Luis
  • 213
  • 2
  • 17

1 Answers1

0

There was a blog post published yesterday that shows you how to do this with couchbase spring-boot and spring data.

I'm not a Java expert at all, but it looks like you start by creating an entity class like this:

@Document
@Data
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode
public class Building {
    @NotNull
    @Id
    private String id;

    @NotNull
    @Field
    private String name;

    @NotNull
    @Field
    private String companyId;

    // ... etc ...
}

Then, create a repository class.

@N1qlPrimaryIndexed
@ViewIndexed(designDoc = "building")
public interface BuildingRepository extends CouchbasePagingAndSortingRepository<Building, String> {

    List<Building> findByCompanyId(String companyId);

    // ... etc ...
}

Finally, you can use @Autowired in a service class or wherever to instantiate a BuildingRepository and start calling the methods on it. The full documentation for Spring Data Couchbase is available on docs.spring.io

Matthew Groves
  • 25,181
  • 9
  • 71
  • 121