Spring Data JPA和Spring Data Elasticsearch;类型中未找到属性索引?

6

我不确定为什么会发生这种情况!我有一个类被Spring Data Elasticsearch和Spring Data JPA使用,但是当我尝试运行我的应用程序时,出现了错误。

Error creating bean with name 'articleSearch': 
Invocation of init method failed; nested exception is 
org.springframework.data.mapping.PropertyReferenceException: 
No property index found for type Article!

Caused by: org.springframework.data.mapping.PropertyReferenceException: 
No property index found for type Article!
    at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:77) ~[spring-data-commons-1.11.4.RELEASE.jar:na]

I've got the following application class:

@SpringBootApplication
@EnableAsync
@ComponentScan(basePackages = {"com.article.models", "com.user"})
public class ArticleApplication {

以下是elasticsearch的配置:

@Configuration
@EnableElasticsearchRepositories(basePackages = "com.article.search")
public class ElasticSearchConfiguration {
    @Resource
    private Environment environment;

    @Bean
    public Client client() {
        TransportClient client = new TransportClient();
        TransportAddress address = new InetSocketTransportAddress(environment.getProperty("elasticsearch.host"), Integer.parseInt(environment.getProperty("elasticsearch.port")));
        client.addTransportAddress(address);
        return client;
    }

    @Bean
    public ElasticsearchOperations elasticsearchTemplate() {
        return new ElasticsearchTemplate(client());
    }
}

这是我设置模型类的方式:

@Entity
@Table(name="article")
@Document(indexName="article", type="articles")

public class Article implements Serializable {

我有一个扩展了elasticsearchrepository的包search,如下所示:

public interface ArticleSearch extends ElasticsearchRepository<Article, String> {

我正在尝试在另一个服务中自动装配文章搜索类,这导致错误发生:

@Autowired
ArticleSearch articleSearch;

我在这里漏掉了什么?!当尝试使用data-jpa + data-elasticsearch时,可能会更加复杂。


能给我显示 Article 类的全部代码吗? - xierui
这是一个带有@column注释的标准Hibernate类。我没有添加任何Elasticsearch注释@xierui。 - James111
我以前见过这个错误。我认为您应该先检查Repository中的方法或查询。一些方法例如“findByIndex”会导致错误。 - xierui
根据错误日志显示的“articleSearch”,我认为该方法应该在“articleSearch”中。最好将所有代码都显示出来,这样我就可以看到问题出在哪里了。 - xierui
仍然不知道 @xierui 发生了什么。 - James111
1个回答

4
我找出了发生这种情况的原因。我不确定为什么,但是Spring似乎没有识别我的ElasticSearchConfiguration配置类!因此,我只需将该类中的所有内容移动到我的主应用程序类(我的其他配置都在那里)中即可。
我还删除了组件扫描并在我的主类中添加了enablejparepository + enableelasticsearchrepository注释。现在它看起来像这样:
@SpringBootApplication
@EnableAsync
@EnableElasticsearchRepositories(basePackages = "com.article.search")
@EnableJpaRepositories(basePackages = {"com.article.dao", "com.user.dao"})
public class ArticleApplication {

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接