spring-data-couchbase - org.springframework.data.mapping.model.MappingException

Hi,

I am trying to execute the spring data couchbase example provided at http://projects.spring.io/spring-data-couchbase/#quick-start . But fails with below exception.
Exception in thread "main" org.springframework.data.mapping.model.MappingException: An ID property is needed, but not found on this entity. at org.springframework.data.couchbase.core.convert.MappingCouchbaseConverter.write(MappingCouchbaseConverter.java:316) at org.springframework.data.couchbase.core.convert.MappingCouchbaseConverter.write(MappingCouchbaseConverter.java:50) at org.springframework.data.couchbase.core.CouchbaseTemplate.save(CouchbaseTemplate.java:298) at org.springframework.data.couchbase.core.CouchbaseTemplate.save(CouchbaseTemplate.java:149) at org.springframework.data.couchbase.repository.support.SimpleCouchbaseRepository.save(SimpleCouchbaseRepository.java:82) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

Model for the same is

package com.spring.data.couchbase.user;

import org.springframework.data.annotation.Id;
import org.springframework.data.couchbase.core.mapping.Field;

public class User {

@Id
private String id;

public String getId() {
	return id;
}

public void setId(String id) {
	this.id = id;
}

@Field("lastName")
private String lastName;

public String getLastName() {
	return lastName;
}

public void setLastname(String lastName) {
	this.lastName = lastName;
}

public String getFirstName() {
	return firstName;
}

public void setFirstName(String firstName) {
	this.firstName = firstName;
}

@Field("firstName")
private String firstName;

@Override
public String toString() {
	return "User{" + "id='" + id + '\'' + ", firstName='" + firstName
			+ '\'' + ", lastName='" + lastName + '}';
}

}

I think Id is not getting set or assigned automatically. Any idea how to generate automatically?
or we have to create explicitly?

You need to set the ID of the document always and explicitly.

1 Like

Thank you. It helped.
I was using “javax.persistence.Id” and facing some issue (An ID property is needed).