Как автоматически генерируются идентификаторы в базе данных spring?

Я хочу создать автогенерацию идентификаторов для моих документов в базе данных spring. В документации Spring есть шаги для этого: 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;
     ...
}

У меня есть репозиторий, созданный для этого:

public interface UserRepository extends CrudRepository<User, String> {

}

Теперь, когда я пытаюсь сохранить запись в корзину пользователя (userRepository.save(user)), я получаю следующее исключение:

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]

...

Пожалуйста помоги.


person User1230321    schedule 27.12.2017    source источник


Ответы (1)


Вы используете Spring Data Couchbase версии 2.2.9, которая не поддерживает автоматическое создание ключей.

Если вам нужны автоматически сгенерированные ключи, вам следует использовать более позднюю версию (например, 3.0.2).

Последние версии и инструкции см. здесь: http://projects.spring.io/spring-data-couchbase/

person Mico    schedule 29.12.2017
comment
Я не думаю, что это так. Если вы посмотрите документацию 2.2.9.RELEASE, в документах упоминается автоматическое создание ключей. Может не хватает автонастройки? docs.spring.io /spring-data/couchbase/docs/2.2.9.RELEASE/ - person tunix; 18.01.2018
comment
На самом деле кажется, что вы правы, скоро удалю этот ответ. :) - person Mico; 22.01.2018
comment
Кстати, я думаю, что в документации 2.2.9 есть проблема. Аннотации есть, но они не влияют на генерацию ключей. Когда я смотрю на PR, он помечен так, как будто он был объединен с ветвями 3.0.x. Итак, кто-то из команды Spring Data Couchbase должен вмешаться. :) Пожалуйста, не удаляйте свой комментарий, так как это один из очень немногих комментариев, которые привели меня на правильный путь. - person tunix; 23.01.2018