How to resolve getCouchbaseOperations() related dependencies using Spring Data Couchbase (version 4.4.5)

Spring Data Couchbase documentation (version 4.4.5) specifies that the method getCouchbaseOperations() has also been removed. It also states that you still can access all methods from the native Java SDK via the class CouchbaseTemplate or Cluster. But I couldn’t resolve this. (Spring Data Couchbase - Reference Documentation)

The code below is from Couchbase SDK 2 implementation. I opted to migrate the deprecated DSL packages into my code as specified in below mentioned thread. (What are the solutions for com.couchbase.client.java.query.dsl.Sort issue?)

I am currently facing the issue of resolving getCouchbaseOperations() related issues. Could someone please specify how I may handle this as a scenario?

package com.acme.acmepay.config.dao;

import com.acme.acmepay.config.document.paymentoption.PaymentGatewayConfiguration;
import com.acme.acmepay.config.logic.paymenttemplatereportlogic.data.PaymentTemplateDataTableSearchLogicData;
import com.acme.acmepay.config.repository.PaymentGatewayConfigurationRepository;
import com.couchbase.client.java.Bucket;
import com.couchbase.client.java.query.N1qlQuery;
import com.couchbase.client.java.query.Statement;
import com.couchbase.client.java.query.dsl.Expression;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.couchbase.repository.query.support.N1qlUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class PaymentTemplateConfigDAOImpl implements PaymentTemplateConfigDAO {

    @Autowired
    PaymentGatewayConfigurationRepository paymentGatewayConfigurationRepository;

    Bucket couchbaseBucket;

    public List<PaymentGatewayConfiguration> getPaymentTemplateSearchReportList(
            PaymentTemplateDataTableSearchLogicData paymentTemplateDataTableSearchLogicData) {


        Statement statement = getQueryStatement(paymentTemplateDataTableSearchLogicData);
        log.info("N1QL Statement" + statement);

      //Error happen here
        N1qlQuery query = N1qlQuery.simple(statement);
        
        return paymentGatewayConfigurationRepository.getCouchbaseOperations().findByN1QL(query,
                PaymentGatewayConfiguration.class);


    }
    
    private Statement getQueryStatement(
            PaymentTemplateDataTableSearchLogicData paymentTemplateDataTableSearchLogicData) {

        //Error happen here
        return N1qlUtils
                .createSelectClauseForEntity(
                        paymentGatewayConfigurationRepository.getCouchbaseOperations().getCouchbaseBucket().name())
                .from(Expression
                        .i(paymentGatewayConfigurationRepository.getCouchbaseOperations().getCouchbaseBucket().name()))
                .where(paymentTemplateDataTableSearchLogicData.getSearchQuery())
                .orderBy(paymentTemplateDataTableSearchLogicData.getSort())
                .limit(paymentTemplateDataTableSearchLogicData.getLimit())
                .offset(paymentTemplateDataTableSearchLogicData.getOffset());

    }
}

Know someone who can answer?

I just tried using @Autowired CouchbaseTemplate in an @Service and it does work. Note that the service object itself must be created from spring - either by @Autowired or from the spring application context. If you created them by just calling their constructor, Spring has no opportunity to process the annotations.

Also note that the @Autowired variables are not initialized until AFTER the constructor is executed.

@Autowired AirlineGatesService airlineGatesService;

@Service
public class AirlineGatesService {
	@Autowired Cluster cluster;
	@Autowired CouchbaseTemplate couchbaseTemplate;
	private void traceAirlineGatesService(){
		System.err.println("cluster is : "+cluster);
		System.err.println("template is : "+couchbaseTemplate);
	}
	@Transactional
	public void transferGates(String fromId, String toId, int gatesToTransfer, RuntimeException exceptionToThrow) {
		traceAirlineGatesService();
cluster is : com.couchbase.client.java.Cluster@6ffa3bf1
template is : org.springframework.data.couchbase.core.CouchbaseTemplate@81dde1

An @Service can also get the couchbaseTemplate bean by dependency injection (by having a CouchbaseTemplate argument in the constructor.

private final CouchbaseTemplate template;
public void PaymentTemplateConfigDAOImpl(CouchbaseTemplate template){
this.template = template;
}

public void doStuff(){
template.getCouchbaseClientFactory().getCluster()…
}