我开始使用Spring Data Elasticsearch。我读:
该类的一个属性需要是一个id,可以通过使用@Id注释它,也可以使用自动找到的名称id或documentId之一。
但当我用@Id标记我的Project实体字段projectId时,elasticsearch仍在说:
No id property found for class com.example.domain.entity.Project!
我发现我正在使用JPA包中的注释@Id:javax.persistence.Id
。当我为我的字段添加另一个@Id注释@org.springframework.data.annotation.Id
时,从存储库中取出工作正常!
问题是我不想同时使用2种@Id注释。而且,我想使用JPA注释只是因为其他模块正在使用基于JPA的存储库层(Spring Data JPA)。
Spring Data Elasticsearch是否支持JPA的@Id注释?知道因为嵌入式id更进一步,这一点非常重要吗? Spring Data Elasticsearch是否支持@EmbeddedId注释?
我的实体:
@Entity
@Document(indexName = "project_list", type = "external")
public class Project implements Serializable {
@Id
@org.springframework.data.annotation.Id <-- without it Spring Data Elasticsearch is complaining that 'No id property found'
@Column(name = "PROJECT_ID")
private Long projectId;
.... other fields and getters/setters
}
是的,1.3.0确实支持@Id
,但你需要一个getter(也许是一个bug?)
ElasticsearchTemplate.getPersistentEntityId
获取您的实体,尝试查找注释@Id然后仅在定义了getter时才返回id的值。
但它似乎不支持@EmbeddedId:SimpleElasticsearchPersistentProperty.SUPPORTED_ID_PROPERTY_NAMES
我有一个类似的问题,我也使用JPA和弹性搜索两者,它在更改后解决
@Column(name = "PROJECT_ID")
private Long projectId;
至
javax.persistence.Id;
列id的默认名称
@Column(name = "id")
private Long id;