2
votes

My problem is, that spring data couchbase doesn't search for subclasses of searched class. For example:

Model:

@Document 
class A { 
   @Id
   String id 
}

@Document
class B extends A {}

And repository:

public interface ARepository extends PagingAndSortingRepository<A, String>{
     Page<A> findAll(Pageable pageable);
}

Spring data couchbase generate query, that has in where condition

_class="com.example.model.A"

But I want in this query search B documents too. Is some way, how can I do this? When I write own query, I must defining order, limit and offset in query and Pageable is not used. But I want use Pageable.

2
Hi Tomas. Did you find a way to do this with Spring Data Couchbase? - Nickolas
Does my answer there help? stackoverflow.com/questions/42636774/… - Tom

2 Answers

1
votes

Consider generic interface based on inheritance.

Firstly create super class:

@Inheritance
public abstract class SuperClass{ 

  @Id
  private int id;
}

Then create your subclasses:

public class A extends SuperClass { /* ... */ }
public class B extends SuperClass { /* ... */ }

Create base repository:

@NoRepositoryBean
public interface SuperClassBaseRepository<T extends SuperClass> 
extends PagingAndSortingRepository<T, Integer> { 
     public T findAll();

}

And then create SuperClass repository basing on base repo:

@Transactional
public interface SuperClassRepository extends SuperClassBaseRepository<SuperClass> { /* ... */ }

@Transactional
public interface ARepository extends SuperClassBaseRepository<A> { /* ... */ }

@Transactional
public interface BRepository extends SuperClassBaseRepository<B> { /* ... */ }

SuperClassRepository findAll() will search all A and B classes

0
votes

We managed to make this work on Spring Data Couchbase 3.2.12. Here's what we did:

We figured out that mappers for each type were only being created if a repository existed for that type, so, besides our superclass repository...

public interface ARepository extends PagingAndSortingRepository<A, String> {
     Page<A> findAll(Pageable pageable);
}

We created an empty repository for each of the subtypes such as:

public interface BRepository extends PagingAndSortingRepository<B, String>{
     // No methods
}

The presence of this second repo warranted the existence of an appropriate mapper for B, so when findAll (or other methods) are invoked in ARepository, the mapper for each subclass is present. Having done this, we were able to get a list of A that were actually B instances.

Hope this helps and nobody has to lose any more time on this. :)