在弹簧数据弹性搜索中保存ENUM

时间:2017-08-03 20:36:06

标签: java spring spring-boot enums spring-data-elasticsearch

我正在尝试使用spring数据elasticsearch将我的实体保存在elasticsearch中,所有属性都被保存(包括对象),除了枚举它总是存储为null,这是我的实体

@Entity
@Document(indexName="invoices", type="invoices", shards = 1)
public class Invoice {

@Transient
@JsonIgnore
@org.springframework.data.annotation.Id
private String searchIndex;

@Field(type = FieldType.String)
private InvoiceStateEnum state;

有和没有@Field属性状态被保存为null,即使正在保存的对象具有此枚举的值。

感谢任何帮助

3 个答案:

答案 0 :(得分:0)

使用@Enumerated注释:

@Enumerated(EnumType.STRING)
private InvoiceStateEnum state;

Enum Types

  1. ORDINAL:将枚举类型属性或字段保留为整数。
  2. STRING:将枚举类型属性或字段保留为字符串。
  3. 参考:http://docs.oracle.com/javaee/7/api/javax/persistence/Enumerated.html

答案 1 :(得分:0)

由于spring-data-elasticsearch使用Jackson,您可以将@JsonFormat.Shape.STRING注释添加到枚举中:

@JsonFormat.Shape.STRING
public enum InvoiceStateEnum {
  // your enum code
}

答案 2 :(得分:0)

我能够通过删除项目下的文件夹数据并重新运行应用程序来解决问题,似乎由于某种原因弹性搜索没有更新记录,所以我从最近添加属性后变为空。