问题
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