簡體   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