Spring Boot 3迁移后Spring Data Couchbase更新查询失效问题
从错误信息Keyspace not found in CB datastore: default:priceDetails.priceDetails.priceDetails可以看出,系统在拼接Couchbase的keyspace时重复叠加了bucket名称,这是Spring Data Couchbase 5.x版本(适配Spring Boot 3.x)的命名空间解析逻辑变化导致的冲突问题。以下是具体解决步骤:
1. 清理实体类的冗余配置
检查你的实体类上的@Document注解,不要同时指定bucket属性,尤其是已经通过全局配置或Repository注解指定bucket的情况下。比如删除实体类中多余的bucket配置:
// 错误写法:重复指定bucket // @Document(bucket = "priceDetails") @Document(collection = "priceDetails") // 仅保留collection(如果需要) public class PriceDetail { // 实体字段 }
2. 用全局配置统一指定bucket和scope
在application.properties或application.yml中配置全局的bucket和scope,替代分散的注解配置,避免重复解析:
# application.properties spring.data.couchbase.bucket-name=priceDetails spring.data.couchbase.scope-name=default # 使用默认scope时可配置,也可省略
配置完成后,移除Repository上的@DefaultScope注解,让全局配置生效。
3. 修正Repository的注解配置
如果必须在Repository级别指定scope,确保@DefaultScope的value是scope名称而非bucket名称,且不要重复配置:
// 正确示例:指定自定义scope(如果不是default的话) @DefaultScope("custom_scope") public interface PriceDetailRepository extends CouchbaseRepository<PriceDetail, String> { // 自定义查询方法 }
4. 手动指定查询的keyspace(可选)
如果自动生成的N1QL仍然存在问题,直接在自定义查询上用@Query注解指定正确的keyspace:
@Query("UPDATE `priceDetails`.`default`.`priceDetails` SET price = $1 WHERE id = $2") void updatePrice(Double price, String id);
原因说明
Spring Data Couchbase 5.x版本对命名空间的解析逻辑进行了调整,当实体、Repository、全局配置多个位置重复指定bucket/scope时,会导致系统重复拼接名称,最终生成bucket.bucket.bucket这种无效的keyspace,触发12003错误。
内容的提问来源于stack exchange,提问作者Anurag Jain

