Srikanth Hugar
Srikanth Hugar

Reputation: 385

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

I'm trying to persist the object with spring-data-couchbase version 1.0.0.RELEASE:

package com.spring.data.couchbase.user;

import javax.persistence.Entity;

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

@Entity
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;

   }

I get a MappingException when I try to save the object to couchbase database. Full stack below. AFAIK entiry seems to be fine. Anything extra expected by spring-data-couchbase ?

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)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:358)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:343)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.data.couchbase.repository.support.ViewPostProcessor$ViewInterceptor.invoke(ViewPostProcessor.java:80)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:91)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
at $Proxy18.save(Unknown Source)
at com.spring.data.couchbase.user.MyService.doWork(MyService.java:27)
at com.spring.data.couchbase.user.Main.main(Main.java:17)

Upvotes: 4

Views: 5295

Answers (4)

Rohit Naik
Rohit Naik

Reputation: 539

import com.couchbase.client.java.repository.annotation.Field;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Version;
import org.springframework.data.couchbase.core.mapping.Document;
import org.springframework.data.couchbase.core.mapping.id.GeneratedValue;
import org.springframework.data.couchbase.core.mapping.id.GenerationStrategy;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

@Document
@Data
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode
@Builder
public class User {

  @Id
  @GeneratedValue(strategy = GenerationStrategy.UNIQUE)
  private String id;

  @Field("lastName")
  private String lastName;

  @Field("firstName")
  private String firstName;



  @Version
  private long version;

}

This should work if you are not setting the id explicitly. Also, use @Document instead of @Entity

Upvotes: 0

Ronny Shibley
Ronny Shibley

Reputation: 2155

You can use the following

import com.couchbase.client.java.repository.annotation.Field;
import com.couchbase.client.java.repository.annotation.Id;

@Id
@GeneratedValue(strategy = GenerationStrategy.UNIQUE)
public String id;

Upvotes: 0

aBnormaLz
aBnormaLz

Reputation: 847

I think the problems are the following:

1: you have @Entity annotation on your class, which is a JPA annotation. Use org.springframework.data.couchbase.core.mapping.Document annotation instead

2: CouchBase stores the ids named _id, so you should rename that field in your class to _id (according to http://developer.couchbase.com/documentation/mobile/current/develop/guides/couchbase-lite/native-api/document/index.html#special-properties)

Upvotes: 1

daschl
daschl

Reputation: 1124

As answered by the author, you need to have an ID which is not null, because Couchbase itself identifies each Document by its unique ID. You should add validations maybe on top of your ID to make sure it is always not null and not empty.

Upvotes: 1

Related Questions