Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Data Couchbase: Automatic ID generation

I'm setting up a Document with the @Id annotation and in my tests I get a MappingException because the Id is not set when creating a new document. Is spring-data + couchbase unable to automatically assign an ID for new documents?

like image 982
user2722238 Avatar asked May 01 '16 06:05

user2722238


3 Answers

There is no auto-generation of IDs in Couchbase, so you need to set one.

Keep in mind that Couchbase can store heterogeneous data in the same Bucket, so by default if you have several types of entities, they'll end up in the same storage unit. Therefore if you have eg. User and Product entities, creating and saving a User which @Id is "foo" then a Product also id-ed "foo" will end up overwriting the User with the Product.

What I mean is, you have to provide the @Id and make sure no ID collide, even across entity classes.

like image 191
Simon Baslé Avatar answered Nov 19 '22 05:11

Simon Baslé


As of commit 069ceea spring-data-couchbase seems to include support for autogenerating keys using generated keys by properties or unique UUIDs. See HERE for documentation on how to use it.

like image 33
Mico Avatar answered Nov 19 '22 05:11

Mico


Here is the correct answer.

@Document
public class User {
     @Id @GeneratedValue(strategy = UNIQUE)
     private String id;
     ...
}

as per this link

like image 1
Ronny Shibley Avatar answered Nov 19 '22 05:11

Ronny Shibley