Mongo的自动增量功能无法将Long设置为_id?

Posted

技术标签:

【中文标题】Mongo的自动增量功能无法将Long设置为_id?【英文标题】:Unable to set Long as _id for the auto increment feature of Mongo? 【发布时间】:2019-11-01 08:55:33 【问题描述】:

我浏览了 URL:https://www.baeldung.com/spring-boot-mongodb-auto-generated-field 和 How can we create Auto generated field for mongodb using spring boot 代码和建议效果很好。

但是当我改变了

@Id
private long id;

@Id
private Long id;

然后代码不起作用并给出以下错误。

java.lang.IllegalStateException: Failed to execute CommandLineRunner
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:816) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
    at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:797) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:324) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
    at org.javers.organization.structure.SeqMongoApplication.main(SeqMongoApplication.java:18) [classes/:na]
Caused by: java.lang.NullPointerException: null
    at org.javers.organization.structure.listeners.UserModelListener.onBeforeConvert(UserModelListener.java:22) ~[classes/:na]
    at org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener.onApplicationEvent(AbstractMongoEventListener.java:87) ~[spring-data-mongodb-2.1.6.RELEASE.jar:2.1.6.RELEASE]
    at org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener.onApplicationEvent(AbstractMongoEventListener.java:31) ~[spring-data-mongodb-2.1.6.RELEASE.jar:2.1.6.RELEASE]
    at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172) ~[spring-context-5.1.6.RELEASE.jar:5.1.6.RELEASE]
    at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165) ~[spring-context-5.1.6.RELEASE.jar:5.1.6.RELEASE]
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) ~[spring-context-5.1.6.RELEASE.jar:5.1.6.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:402) ~[spring-context-5.1.6.RELEASE.jar:5.1.6.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:359) ~[spring-context-5.1.6.RELEASE.jar:5.1.6.RELEASE]
    at org.springframework.data.mongodb.core.MongoTemplate.maybeEmitEvent(MongoTemplate.java:2276) ~[spring-data-mongodb-2.1.6.RELEASE.jar:2.1.6.RELEASE]
    at org.springframework.data.mongodb.core.MongoTemplate.doInsertBatch(MongoTemplate.java:1322) ~[spring-data-mongodb-2.1.6.RELEASE.jar:2.1.6.RELEASE]
    at org.springframework.data.mongodb.core.MongoTemplate.insert(MongoTemplate.java:1269) ~[spring-data-mongodb-2.1.6.RELEASE.jar:2.1.6.RELEASE]
    at org.springframework.data.mongodb.repository.support.SimpleMongoRepository.saveAll(SimpleMongoRepository.java:102) ~[spring-data-mongodb-2.1.6.RELEASE.jar:2.1.6.RELEASE]
    at org.springframework.data.mongodb.repository.support.SimpleMongoRepository.saveAll(SimpleMongoRepository.java:51) ~[spring-data-mongodb-2.1.6.RELEASE.jar:2.1.6.RELEASE]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_171]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_171]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_171]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_171]
    at org.springframework.data.repository.core.support.RepositoryComposition$RepositoryFragments.invoke(RepositoryComposition.java:359) ~[spring-data-commons-2.1.6.RELEASE.jar:2.1.6.RELEASE]
    at org.springframework.data.repository.core.support.RepositoryComposition.invoke(RepositoryComposition.java:200) ~[spring-data-commons-2.1.6.RELEASE.jar:2.1.6.RELEASE]
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$ImplementationMethodExecutionInterceptor.invoke(RepositoryFactorySupport.java:644) ~[spring-data-commons-2.1.6.RELEASE.jar:2.1.6.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.6.RELEASE.jar:5.1.6.RELEASE]
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:608) ~[spring-data-commons-2.1.6.RELEASE.jar:2.1.6.RELEASE]
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.lambda$invoke$3(RepositoryFactorySupport.java:595) ~[spring-data-commons-2.1.6.RELEASE.jar:2.1.6.RELEASE]
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:595) ~[spring-data-commons-2.1.6.RELEASE.jar:2.1.6.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.6.RELEASE.jar:5.1.6.RELEASE]
    at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:59) ~[spring-data-commons-2.1.6.RELEASE.jar:2.1.6.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.6.RELEASE.jar:5.1.6.RELEASE]
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93) ~[spring-aop-5.1.6.RELEASE.jar:5.1.6.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.6.RELEASE.jar:5.1.6.RELEASE]
    at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:61) ~[spring-data-commons-2.1.6.RELEASE.jar:2.1.6.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.6.RELEASE.jar:5.1.6.RELEASE]
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212) ~[spring-aop-5.1.6.RELEASE.jar:5.1.6.RELEASE]
    at com.sun.proxy.$Proxy48.saveAll(Unknown Source) ~[na:na]
    at org.javers.organization.structure.SeqMongoApplication.run(SeqMongoApplication.java:32) [classes/:na]
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:813) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
    ... 5 common frames omitted

UserModelListener

@Component
public class UserModelListener extends AbstractMongoEventListener<User> 

    private SequenceGeneratorUtils sequenceGenerator;

    @Autowired
    public UserModelListener(SequenceGeneratorUtils sequenceGenerator) 
        this.sequenceGenerator = sequenceGenerator;
    

    @Override
    public void onBeforeConvert(BeforeConvertEvent<User> event) 
        if (event.getSource().getId() < 1) 
            event.getSource().setId(sequenceGenerator.generateSequence(User.SEQUENCE_NAME));
        
    

SequenceGeneratorUtils.java

@Service
public class SequenceGeneratorUtils 

    private MongoOperations mongoOperations;

    @Autowired
    public SequenceGeneratorUtils(MongoOperations mongoOperations) 
        this.mongoOperations = mongoOperations;
    

    public long generateSequence(String seqName) 
        DatabaseSequence counter = mongoOperations.findAndModify(query(where("_id").is(seqName)),
                new Update().inc("seq", 1), 
                options().returnNew(true).upsert(true), 
                DatabaseSequence.class);

        return !Objects.isNull(counter) ? counter.getSeq() : 1;

    

【问题讨论】:

@All - 有人知道解决方案吗? 【参考方案1】:

您忘记在 MongoOperations 上使用 @Autowired

@Autowired 
private MongoOperations mongoOperations;

【讨论】:

以上是关于Mongo的自动增量功能无法将Long设置为_id?的主要内容,如果未能解决你的问题,请参考以下文章

Mongo:将返回的记录限制为切片的增量计数

自动增量 pymongo

如何使用 Petapoco 将自动增量 ID 设置为另一列

如何将我的表的自动增量 id 设置为 700,即使同一张表上存在 1000 条记录?

将 _id 从 ObjectId 更改为正常的唯一自动增量整数

是否可以指定使用 Hibernate 将 id 自动增量非空主键设置为 @ManyToMany 关系映射?