繁体   English   中英

如何使用Elasticsearch重命名实体中的ID字段?

[英]How to rename id field in an entity using with elasticsearch?

嗨,朋友们,我在Spring Boot项目中使用弹性搜索,我的主要数据库是mysql。 我的项目在默认配置下运行良好。 假设我的实体是CancelReason.java

@Entity
@Table(name = "cancel_reason")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Document(indexName = "cancelreason")
public class CancelReason implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;


    @NotNull
    @Size(max = 10)        
    @Column(name = "reason_code", length = 10, nullable = false)
    private String reasonCode;
    .......
}

现在使用此配置,我的项目正在运行,弹性搜索工作正常,但是我的要求是使reasonCode主键代替id所以我想删除

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

此代码。 当我删除id字段并设置原因代码主键时,当我运行我的项目时,它给了我这个错误:

[WARN] org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext - Exception encountered during context initialization - cancelling refresh attempt
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cancelReasonManager': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private in.megacabs.repository.search.CancelReasonSearchRepository in.megacabs.manager.CancelReasonManager.cancelReasonSearchRepository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cancelReasonSearchRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: No id property found for class in.megacabs.domain.CancelReason!
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1210) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757) ~[spring-context-4.1.7.RELEASE.jar:4.1.7.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480) ~[spring-context-4.1.7.RELEASE.jar:4.1.7.RELEASE]
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) [spring-boot-1.2.5.RELEASE.jar:1.2.5.RELEASE]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686) [spring-boot-1.2.5.RELEASE.jar:1.2.5.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:320) [spring-boot-1.2.5.RELEASE.jar:1.2.5.RELEASE]
        at in.megacabs.Application.main(Application.java:77) [classes/:na]
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private in.megacabs.repository.search.CancelReasonSearchRepository in.megacabs.manager.CancelReasonManager.cancelReasonSearchRepository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cancelReasonSearchRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: No id property found for class in.megacabs.domain.CancelReason!
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]

虽然mysql工作正常,但实体是在mysql数据库中创建的,而不是在elasticsearch中创建的。 请帮忙!

尝试在主键字段中添加以下注释:

@Id
@org.springframework.data.annotation.Id

问候。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM