How are ids autogenerated in spring data couchbase?

冷暖自知 提交于 2019-12-11 06:58:38

问题


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

Here is my code:

@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> {

}

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]

...

Please help.


回答1:


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

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

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



来源:https://stackoverflow.com/questions/47998493/how-are-ids-autogenerated-in-spring-data-couchbase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!