gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 03:23:19 24 4
gpt4 key购买 nike

我正在尝试使用 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)

最佳答案

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

关于java - spring-data-couchbase - org.springframework.data.mapping.model.MappingException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22858339/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com