简体   繁体   中英

Spring data couchbase documents java classes inheritance

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM