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

时间:2014-04-04 09:09:29

标签: java spring-data couchbase spring-data-couchbase

我尝试使用spring-data-couchbase版本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;

   }

当我尝试将对象保存到couchbase数据库时,我收到了MappingException。下面的完整堆栈。 AFAIK entiry似乎很好。 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)

4 个答案:

答案 0 :(得分:1)

正如作者所回答的那样,您需要一个非空的ID,因为Couchbase本身通过其唯一ID识别每个Document。您应该在ID之上添加验证,以确保它始终不为空且不为空。

答案 1 :(得分:1)

我认为问题如下:

1:你的类上有@Entity注释,这是一个JPA注释。请改用org.springframework.data.couchbase.core.mapping.Document注释

2:CouchBase存储名为_id的id,因此您应该将类​​中的该字段重命名为_id(根据http://developer.couchbase.com/documentation/mobile/current/develop/guides/couchbase-lite/native-api/document/index.html#special-properties

答案 2 :(得分:0)

您可以使用以下

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

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

答案 3 :(得分:0)

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;

}

如果您未明确设置ID,这应该可以工作。另外,使用@Document代替@Entity