gpt4 book ai didi

java - 是否可以将 Spring Data Couchbase 与外部文档(来自依赖项)映射?

转载 作者:行者123 更新时间:2023-12-02 11:50:51 25 4
gpt4 key购买 nike

我需要从旧版 Couchbase 存储桶中检索没有特定架构的数据。它可以映射为com.couchbase.client.java.document.StringDocument来自 Couchbase Java 客户端。我可以直接使用 Java 客户端来执行此操作:

bucket.get(key, StringDocument.class)

但是我怎样才能映射这个StringDocument使用org.springframework.data.repository.CrudRepository

我无法创建这样的界面interface UserRepository extends CrudRepository<StringDocument, String>因为 Spring Data Couchbase 需要 @Document 和 @Id 注释。

All entities should be annotated with the @Document annotation. Also, every field in the entity should be annotated with the @Field annotation. While this is - strictly speaking - optional, it helps to reduce edge cases and clearly shows the intent and design of the entity.

There is also a special @Id annotation which needs to be always in place. Best practice is to also name the property id.

我应该直接使用Bucket,创建自己的类似实体,还是有其他解决方案?

我使用以下版本的 spring-boot-starter-data-couchbase:1.5.9.RELEASE .

最佳答案

如果它只是一个StringDocument(并且它甚至没有被spring-data-couchbase保存......它会注入(inject)_class 属性用于反序列化目的),那么我只需使用存储桶来检索它。

请记住,如果您的存储库(我指的是您定义的映射到 StringDocument 所在存储桶的任何其他存储库)定义为您将能够访问的 CouchbaseRepository来自存储库方法的存储桶本身,例如 repository.getCouchbaseOperations().getCouchbaseBucket()。或者您可以尝试以下repository.getCouchbaseOperations().findById("id", StringDocument.class)

关于java - 是否可以将 Spring Data Couchbase 与外部文档(来自依赖项)映射?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47882866/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com