0
votes

I am getting this exception every time i try to query a view on Couchbase DB from my spring boot application. Unsupported parameter type for key: class com.couchbase.client.protocol.views.Query.

I was setting a string on setKey() method of Query class, got an exception. But then I checked the API and provided a json to setKey, still not working. Have searched a lot but could not get this to work.

I am sharing the code snippet in this post as well.

Application.properties

spring.couchbase.bucket.password=
spring.couchbase.bucket.name=default
spring.couchbase.bootstrap-hosts=127.0.0.1
spring.data.couchbase.repositories.enabled=true

PlayerRepository

public interface PlayerRepository extends CouchbaseRepository<Player, Integer>
{

    @View(designDocument = "player", viewName = "all")
    public List<Player> findAll();
    @View(designDocument = "player", viewName = "by_Name")
    public Player findByName(Query query);
    @View(designDocument = "player", viewName = "by_TeamId")
    public List<Player> findByTeamId(Query query);

}

Player.java

@Document
public class Player
{
    @Id
    int playerId;
    @Field
    String name;
    @Field
    String type;
    @Field
    String country;
    @Field
    String playingHand;
    @Field
    String era;
    @Field
    int teamId;
    @Field
    int odiCenturies;
    @Field
    int testCenturies;

    public Player(){}


    public Player(int playerId, String name, String type, String country, String playingHand, String era, int teamId,
            int odiCenturies, int testCenturies) {
        super();
        this.playerId = playerId;
        this.name = name;
        this.type = type;
        this.country = country;
        this.playingHand = playingHand;
        this.era = era;
        this.teamId = teamId;
        this.odiCenturies = odiCenturies;
        this.testCenturies = testCenturies;
    }

SpringBootApplication class

@SpringBootApplication public class CricketTeamSelectionMain {

/**
 * @param args
 */
public static void main(String[] args) 
{
    SpringApplication.run(CricketTeamSelectionMain.class, args);

}

@Configuration
@EnableCouchbaseRepositories
public static class DBConfig extends AbstractCouchbaseConfiguration
{

    @Value("${spring.couchbase.bucket.name}")
    private String bucketName;

    @Value("${spring.couchbase.bucket.password}")
    private String password;

    @Value("${spring.couchbase.bootstrap-hosts}")
    private String ip;

    @Override
    public String getBucketName() {
        return this.bucketName;
    }

    @Override
    public String getBucketPassword() {
        return this.password;
    }

    @Override
    public List<String> getBootstrapHosts() {
        return Arrays.asList(this.ip);
    }

}

}

PlayerService class

package org.ups.fantasyCricket.service;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.ups.fantasyCricket.CricketTeamSelectionMain.DBConfig;
import org.ups.fantasyCricket.Model.Player;
import org.ups.fantasyCricket.Repository.PlayerRepository;

import com.couchbase.client.CouchbaseClient;
import com.couchbase.client.protocol.views.Query;
import com.couchbase.client.protocol.views.View;
import com.couchbase.client.protocol.views.ViewResponse;

@Service
public class PlayerService 
{

    @Autowired
    PlayerRepository playerRepo;

    private CouchbaseClient client;


    public List<Player> getAllPlayers() 
    {
        List<Player> allPlayerLists = new ArrayList<Player>();
        /*allPlayerLists.addAll((Collection<? extends Player>) playerRepo.findAll());
        return allPlayerLists;*/
        playerRepo.findAll().forEach(allPlayerLists::add);
        return allPlayerLists;
    }

    public Player getPlayerByName(String name) 
    {
        DBConfig dbCon = new DBConfig();

        try
        {
            Query query = new Query();
            query.setIncludeDocs(true);
            query.setKey(name);
            Player player = playerRepo.findByName(query);
            return player;
        }
        catch(Exception e)
        {
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
        return null;

    }

    public String addPlayer(Player player) 
    {
        playerRepo.save(player);
        return "Success";
    }

    public String updatePlayer(Player player, int id)
    {
        playerRepo.save(player);
        return "Success";
    }

    public List<Player> getPlayersByTeamId(int teamId) 
    {
        List<Player> allPlayerLists = new ArrayList<Player>();
        Query query = new Query();
        query.setKey(String.valueOf(teamId));
        playerRepo.findByTeamId(query).forEach(allPlayerLists::add);
        return allPlayerLists;

    }

    public String addPlayers(List<Player> players)
    {
        playerRepo.save(players);
        return "Success";
    }
}

View by_Name on CouchBase DB

function (doc) {
emit(doc.name, doc);
}
1
which version of spring-data-couchbase are you using? - Simon Baslé

1 Answers

0
votes

Which version of spring-data-couchbase are you using? Starting with 2.x, the @Query annotation uses query derivation and you cannot use a ViewQuery as a parameter anymore... Have a look at the docs, on query derivation with a view.

You could probably use the CouchbaseTemplate to perform a manual query though.