如何在Spring Data Couchbase中自动生成ID? [英] How are ids autogenerated in spring data couchbase?

查看:170
本文介绍了如何在Spring Data Couchbase中自动生成ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Spring Data Couchbase中为我的文档创建ID的自动生成. Spring文档为此提供了步骤: https://docs.spring.io/spring-data/couchbase/docs/current/reference/html/#couchbase.autokeygeneration

I want to create autogeneration of IDs to my documents in spring data couchbase. Spring docs has steps for this: https://docs.spring.io/spring-data/couchbase/docs/current/reference/html/#couchbase.autokeygeneration

这是我的代码:

@Document
public class User {
     @Id 
     @GeneratedValue(strategy = GenerationStrategy.USE_ATTRIBUTES, delimiter = ".")
     private String id;
     @IdPrefix(order=0)
     private String userPrefix;
     @IdSuffix(order=0)
     private String userSuffix;
     ...
}

我为此创建了一个存储库:

I've a repository created for this:

public interface UserRepository extends CrudRepository<User, String> {

}

现在,当我尝试将记录保存到用户存储桶(userRepository.save(user))时,出现以下异常:

Now, when I try to save a record to the user bucket(userRepository.save(user)), I get following exception:

java.lang.IllegalArgumentException: The Document ID must not be null or empty.
    at com.couchbase.client.java.document.AbstractDocument.<init>(AbstractDocument.java:53) ~[java-client-2.3.7.jar:na]
    at com.couchbase.client.java.document.RawJsonDocument.<init>(RawJsonDocument.java:149) ~[java-client-2.3.7.jar:na]
    at com.couchbase.client.java.document.RawJsonDocument.create(RawJsonDocument.java:80) ~[java-client-2.3.7.jar:na]
    at org.springframework.data.couchbase.core.CouchbaseTemplate.encodeAndWrap(CouchbaseTemplate.java:157) ~[spring-data-couchbase-2.2.9.RELEASE.jar:na]
    at org.springframework.data.couchbase.core.CouchbaseTemplate.access$400(CouchbaseTemplate.java:87) ~[spring-data-couchbase-2.2.9.RELEASE.jar:na]
    at org.springframework.data.couchbase.core.CouchbaseTemplate$11.doInBucket(CouchbaseTemplate.java:591) ~[spring-data-couchbase-2.2.9.RELEASE.jar:na]
    at org.springframework.data.couchbase.core.CouchbaseTemplate$11.doInBucket(CouchbaseTemplate.java:586) ~[spring-data-couchbase-2.2.9.RELEASE.jar:na]
    at org.springframework.data.couchbase.core.CouchbaseTemplate.execute(CouchbaseTemplate.java:536) ~[spring-data-couchbase-2.2.9.RELEASE.jar:na]
    at org.springframework.data.couchbase.core.CouchbaseTemplate.doPersist(CouchbaseTemplate.java:586) ~[spring-data-couchbase-2.2.9.RELEASE.jar:na]
    at org.springframework.data.couchbase.core.CouchbaseTemplate.save(CouchbaseTemplate.java:233) ~[spring-data-couchbase-2.2.9.RELEASE.jar:na]
    at org.springframework.data.couchbase.core.CouchbaseTemplate.save(CouchbaseTemplate.java:228) ~[spring-data-couchbase-2.2.9.RELEASE.jar:na]
    at org.springframework.data.couchbase.repository.support.SimpleCouchbaseRepository.save(SimpleCouchbaseRepository.java:86) ~[spring-data-couchbase-2.2.9.RELEASE.jar:na]

...

请帮助.

推荐答案

您正在使用Spring Data Couchbase版本2.2.9,该版本不支持自动生成密钥.

You are using Spring Data Couchbase version 2.2.9 which does not support auto generating keys.

如果要使用自动生成的密钥,则应使用更高版本(例如3.0.2)

If you want autogenerated keys you should use a later version (For example 3.0.2)

有关最新版本和说明,请参见此处: http://projects.spring.io/spring-data-couchbase/

See here for latest versions and instructions: http://projects.spring.io/spring-data-couchbase/

这篇关于如何在Spring Data Couchbase中自动生成ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆