这个异常是使用 spring-data-elasticsearch 并且集成 spring-data-jpa 时遇到的异常
Description:
The bean 'productBrandESRepository', defined in null, could not be registered. A bean with that name has already been defined in null and overriding is disabled.
Action:
Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true
百度了半天没有结果
在官方文档上看到了如下信息(已经过goole翻译)
这个就说的很明白了,你放一起之后没有指定包,es就不知道这个绑定的到底是jpa的库还是es得库
然后我添加了如下代码:
@Configuration
@EnableJpaRepositories(basePackages = "com.mall.market.repository.jpa")
@EnableElasticsearchRepositories(basePackages = "com.mall.market.repository.es")
public class ElasticSearchConfiguration {
}
这个报错问题就解决了。
但是还有别的坑啊:
- elasticsearch 在6.x 之后得版本中一个index下只能有一个type
- 我得主键名并不是id ,于是报了以下异常
No id property found for class com.mall.market.entity.ProductBrand!
我的ProductBrand 实体类是这样的
package com.mall.market.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.elasticsearch.annotations.Document;
import javax.persistence.*;
import java.io.Serializable;
/**
* @author lidai
* @date 2019/7/12 14:46
* <p>
* 品牌信息
*/
@Data
@Entity
@Table(name = "market_product_brand")
@NoArgsConstructor
@AllArgsConstructor
//声明存储于索引库为mall_demo下,类型为ProductBrand
@Document(indexName = "mall_product_brand", type = "ProductBrand")
public class ProductBrand implements Serializable {
private static final long serialVersionUID = 4066518364159966368L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "brand_id")
private Long brandId;
@Column(name = "brand_name")
private String brandName;
@Column(name = "telephone")
private String telephone;
@Column(name = "brand_web")
private String brandWeb;
@Column(name = "brand_desc")
private String brandDesc;
@Column(name = "brand_logo")
private String brandLogo;
@Column(name = "brand_status")
private String brandStatus;
@Column(name = "brand_order")
private Integer brandOrder;
}
当我把 brandId 直接改成了id 这个异常就消失了,我也不清楚是为什么。
还可以通过添加@Id注解解决
@org.springframework.data.annotation.Id//声明es主键