我做了一个实验......两个 Spring 数据存储库的一个通用实体: - JPA - MongoDB
首先我使用以下库版本:
spring-data-jpa:1.7.0.RELEASE spring-data-mongodb : 1.6.0.RELEASE
我有一个实体:
@Entity
@Table(name = "ACCOUNTS")
public class Account {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ACCOUNT_ID")
private Long id;
@Column(name = "ACCOUNT_NUMBER")
private String number;
public Account() {
}
public Account(String number) {
this.number = number;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
}
JPA 存储库具有以下外观:
public interface Repository extends CrudRepository<Account, Long> {
public Account findByNumber(String number);
}
MongoDB 存储库具有以下外观:
包ua.home.springdata.investigation.repository.mongo;
public interface Repository extends CrudRepository<Account, Long> {
}
所以... JPA 有效 :) 没什么特别的 :) 但是 MongoDB 测试没有通过:( 我收到一个错误:
org.springframework.dao.InvalidDataAccessApiUsageException: Cannot autogenerate id of type java.lang.Long for entity of type ua.home.springdata.investigation.entity.Account! at org.springframework.data.mongodb.core.MongoTemplate.assertUpdateableIdIfNotSet(MongoTemplate.java:1149) at org.springframework.data.mongodb.core.MongoTemplate.doSave(MongoTemplate.java:878) at org.springframework.data.mongodb.core.MongoTemplate.save(MongoTemplate.java:833) at org.springframework.data.mongodb.repository.support.SimpleMongoRepository.save(SimpleMongoRepository.java:73) at org.springframework.data.mongodb.repository.support.SimpleMongoRepository.save(SimpleMongoRepository.java:88) at org.springframework.data.mongodb.repository.support.SimpleMongoRepository.save(SimpleMongoRepository.java:45) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:442) at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:427) at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:381) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207) at com.sun.proxy.$Proxy26.save(Unknown Source)
我认为这是一个非常普遍的情况。为什么 Spring 数据不能像 Long 一样生成实体 ID?太奇怪了。
最佳答案
Mongo ObjectIds 不映射到 java Long 类型。
我在 7.6.1 下的文档中看到了这一点:
http://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#mongo-template.id-handling
An id property or field declared as a String in the Java class will be converted to and stored as an ObjectId if possible using a Spring Converter<String, ObjectId>. Valid conversion rules are delegated to the MongoDB Java driver. If it cannot be converted to an ObjectId, then the value will be stored as a string in the database.
An id property or field declared as BigInteger in the Java class will be converted to and stored as an ObjectId using a Spring Converter<BigInteger, ObjectId>.
将 id 更改为 String 或 BigInteger 并删除 strategy 参数。
关于java - Spring 数据MongoDB。生成id的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26574409/