处理couchbase中的非弹簧数据文档

问题描述 投票:0回答:1

是否有建议的方法来处理没有带有spring-data-couchbase的_class字段的文档(如果有的话)?尝试它只是按预期抛出异常。

编辑:道歉,如果这有点太模糊,让我添加更多的上下文。我想通过名字从沙发基地获取一些学生的数据,比方说。存储库看起来像 -

@Repository

public interface StudentRepository扩展了CouchbaseRepository {

Optional<StudentDocument> findByName(String name);

}

现在couchbase中的文档没有_class字段,或者说如果我们为_class字段输入不同的“key”和“value”,因为我们不想依赖它,所以这个方法失败了。我有点用这种方式破解了一个解决方法 -

`

@Override
public Student getStudent(String name) {
    N1qlQuery query = N1qlQuery.simple(String.format("select *, META().id AS _ID, META().cas AS _CAS" +
            " from student where name = \'%s\';", name));
    return Optional.ofNullable(studentRepository.getCouchbaseOperations()
            .findByN1QL(query, StudentWrapper.class)
            .get(0))
            .map(StudentWrapper::getStudent)
            .orElseGet(() -> {
                throw new HttpClientErrorException(HttpStatus.NOT_FOUND);
            });
}

`

我想知道是否有另一种方法来实现这一目标

spring-data couchbase spring-data-couchbase
1个回答
0
投票

使用Spring spEL时,Couchbase将自动包含_class(或您定义为类型的任何属性):

public interface AreaRepository extends CouchbaseRepository<Area, String> {
   //The _class/type is automatically included by Couchbase 
    List<Area> findByBusinessUnityIdAndRemoved(String businessId, boolean removed);
}

但是,如果要使用N1QL,则必须添加#{#n1ql.filter}:

public interface BusinessUnityRepository extends CouchbaseRepository<BusinessUnity, String>{

    @Query("#{#n1ql.selectEntity} where #{#n1ql.filter} and companyId = $2 and $1 within #{#n1ql.bucket}")
    BusinessUnity findByAreaRefId(String areaRefId, String companyId);

}

#{#n1ql.filter}将自动为您添加类型过滤器。

© www.soinside.com 2019 - 2024. All rights reserved.