Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QueryDslMongoRepository for different collections

I am trying to implement a QueryDslMongoRepository for a model "Document"

@QueryEntity
@Document(collection="currentDocuments")
public class DocumentImpl extends TranslatableObjectImpl implements Document

In our current implementation a to be deleted document moves von "currentDocuments" into "deletedDocuments" collection.

I cant find a solution to create a repository like this

public interface DocumentRepository extends MongoRepository<DocumentImpl, String> ,QueryDslPredicateExecutor<DocumentImpl> {}

with a dynamic collection name.

My goal is to have the advantages of queryDsl in one Repository for different collections and to be able to move models from one collection into another like

public move(DocumentImpl entity, String sourceCollection, String targetCollection){
    repository.delete(entity,sourceCollection);
    repository.save(entity,targetCollection);
}

or something like

public List<Document> findAllDocumentsWithAttachments(String collectionName){
    return repository.findAll(QDocumentImpl.documentImpl.attachments.isNotEmpty(), collectionName);
}

Any suggestions?

like image 655
DCO Avatar asked Apr 17 '26 02:04

DCO


1 Answers

I implemented this feature by creating an own FactoryBean extending MongoRepositoryFactoryBean.

like image 179
DCO Avatar answered Apr 19 '26 15:04

DCO