Spring Data Elasticsearch支持JPA的@Id注释吗



我开始使用Spring Data Elasticsearch。我读到:

类的一个属性需要是id,通过使用@Id或使用自动找到的名称之一对其进行注释id或documentId。

但当我用@Id标记我的项目实体字段projectId时,弹性搜索仍然在说:

No id property found for class com.example.domain.entity.Project!

我发现我使用的是JPA包中的annotation@Id:javax.persistence.Id。当我为我的字段添加另一个@Id注释@org.springframework.data.annotation.Id时,从存储库中提取就可以了!

问题是我不想同时使用两种@Id注释。此外,我只想使用JPA注释,因为其他模块使用的是基于JPA的存储库层(SpringDataJPA)。

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和Elastic搜索,在更改后解决了这个问题

@Column(name = "PROJECT_ID")
private Long projectId;

javax.persistence.Id;

列id 的默认名称

@Column(name = "id")
private Long id;

最新更新