Skip to content
Advertisement

Simple Spring Data Couchbase Find Query Not Working

I’m new to Couchbase and Spring Data Couchbase and am having issues getting a simple find query to work. I am running the community edition of couchbase (version 6.6.0), have the latest version of Spring Data Couchbase, and have created a small spring boot app to query from a new bucket I created. I am able to connect to the database ok, but none of the find queries I am using return any data. I have gone into the bucket and added a few indexes, but that didn’t seem to help either. I am hoping it is just something small that I am missing.

Here is my code…

Config

@Configuration
@EnableCouchbaseRepositories
public class Config extends AbstractCouchbaseConfiguration {

    @Override
    public String getConnectionString() {
        return "couchbase://127.0.0.1";
    }

    @Override
    public String getUserName() {
        return "Administrator";
    }

    @Override
    public String getPassword() {
        return "admin1";
    }

    @Override
    public String getBucketName() {
        return "testing";
    }

    @Override
    protected boolean autoIndexCreation() {
        return true;
    }
}

Model

@Document
public class UserInfo {
    @Id
    private String id;
    @QueryIndexed
    private String name;

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }
}

Repository

@Repository
public interface UserRepository extends CrudRepository<UserInfo, String> {
    List<UserInfo> findByName(String name);
}

Main code that runs on app startup. I never seem to be getting data back, even though I have a few documents in my bucket.

@Configuration
public class StartupConfig {
    @Autowired
    public TravelRepository travelRepository;

    @Autowired
    public UserRepository userRepository;

    @PostConstruct
    public void postConstruct() {
        System.out.println("Processing data...");
        List<UserInfo> userInfo = userRepository.findByName("Steve");
        //List<NameInfo> nameInfo = nameRepository.findAll();
        if (userInfo.size() == 0) {
            System.out.println("No data found");
        } else {
            System.out.println("Data found: " + userInfo.get(0).getName());
        }
    }
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>couchbase</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>couchbase</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-couchbase</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Advertisement

Answer

OK, I figured out the issue. I was manually inserting data via the Admin console, and I didn’t realize that you have to add an _class attribute to your documents. When Spring Data Couchbase generates its queries, they will look something like this:

Select * from testing where name = "Steve" and _class= "com.example.couchbase.model.UserInfo";

And it won’t find any data if the _class attribute is missing (unless there is some property setting or something that lets you get around this).

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement