简体   繁体   English

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

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

Hi friends i am using elastic search with spring boot project and my primary database is mysql. 嗨,朋友们,我在Spring Boot项目中使用弹性搜索,我的主要数据库是mysql。 My project is running fine with default configuration . 我的项目在默认配置下运行良好。 Suppose my entity is CancelReason.java 假设我的实体是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;
    .......
}

Now with this configuration my project is running and elastic search is working fine but my requirment is to make reasonCode primary key instead of id so i want to remove 现在使用此配置,我的项目正在运行,弹性搜索工作正常,但是我的要求是使reasonCode主键代替id所以我想删除

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

this code. 此代码。 as i remove id field and make reasonCode primarykey and when i run my project it gives me this error: 当我删除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]

Though mysql is working fine entity is beign created in mysql database but not in elasticsearch. 虽然mysql工作正常,但实体是在mysql数据库中创建的,而不是在elasticsearch中创建的。 Please help! 请帮忙!

Try adding this annotations in the primary key field: 尝试在主键字段中添加以下注释:

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

Regards. 问候。

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

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