Custom method in spring data couchbase

1.1k views Asked by At

I need to write a custom method for spring data couchbase repository. Here is my code.

CBsampleRepositoryCustom.java

public interface CBsampleRepositoryCustom  {
public void addIndex() ;
}

CBsampleRepositoryImpl.java

public class CBsampleRepositoryImpl implements CBsampleRepositoryCustom {
@Override
public void addIndex() {
    System.out.println("CBsampleRepositoryCustomImpl createIndex");
}
}

CBsampleRepository.java

@Repository
public interface CBsampleRepository extends  CouchbaseRepository<Content,String> ,     CBsampleRepositoryCustom{
}

CouchBaseBeansConfiguration.java

@Configuration
public class CouchBaseBeansConfiguration {
@Bean
public CouchbaseClient couchbaseClient() throws IOException {

    return new CouchbaseClient(Arrays.asList(URI
            .create("http://localhost:8091/pools")), "test", "");
}
@Bean
public CouchbaseTemplate couchbaseTemplate() throws IOException {
    return new CouchbaseTemplate(couchbaseClient());
}
}

Main.java

public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(
        CouchBaseBeansConfiguration.class);
CouchbaseTemplate template = context.getBean("couchbaseTemplate",
        CouchbaseTemplate.class);
RepositoryFactorySupport factory = new CouchbaseRepositoryFactory(
        template);
CBsampleRepository repository = factory.getRepository(CBsampleRepository.class);
repository.addIndex();
}

But while running its showing an error.

Exception in thread "main" org.springframework.dao.InvalidDataAccessResourceUsageException: Could not load view "addIndex" for design doc "content"; nested exception is com.couchbase.client.protocol.views.InvalidViewException: Could not load view "addIndex" for design doc "content"

2

There are 2 answers

0
Arun M R Nair On

We need to pass implementation class object into getRepository function.Change main class as below.

Main.java

public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(
    CouchBaseBeansConfiguration.class);
CouchbaseTemplate template = context.getBean("couchbaseTemplate",
    CouchbaseTemplate.class);
RepositoryFactorySupport factory = new CouchbaseRepositoryFactory(
    template);
CBsampleRepository repository = factory.getRepository(CBsampleRepository.class,new CBsampleRepositoryImpl());
CBsampleRepository repository = factory.getRepository(CBsampleRepository.class,custom);
repository.addIndex();
}
0
Hazok On

The Spring Data Couchbase documentation covers this: Spring Data Reference Link

For a quick high-level overview, you create an interface for your implementation with the customer methods desired. Then create the implementation class for those methods with the same name as your CrudRepository interface, but suffixed with "Impl"; this suffix is important. Then when you create your interface that extends the Spring CrudRepository, you not only extend the CrudRepository, but also the interface you created for your custom methods. Then when you build as normal Spring should handle generating your CrudRepository with the custom methods you specified.

Here's a quick example from the Spring documentation referenced above. First the custom interface:

interface UserRepositoryCustom {
  public void someCustomMethod(User user);
}

Now the implementation of this interface:

class UserRepositoryImpl implements UserRepositoryCustom {

  public void someCustomMethod(User user) {
    // Your custom implementation
  }
}

Finally the typical interface for your CrudRepository, but also extend your custom interface:

interface UserRepository extends CrudRepository<User, Long>, UserRepositoryCustom {

  // Declare query methods here
}

For more details, please reference the Spring Data Couchbase documentation linked above.