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?
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.
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.
Here is the correct answer.
@Document
public class User {
@Id @GeneratedValue(strategy = UNIQUE)
private String id;
...
}
as per this link
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With