5
votes

I am getting started to use Spring Data Elasticsearch. I read:

One of the attributes of the class needs to be an id, either by annotating it with @Id or using one of the automatically found names id or documentId.

but when I marked my Project entity field projectId with @Id then elasticsearch still is saying:

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

I figured out that I am using annotation @Id from JPA package: javax.persistence.Id. When I add another @Id annotation @org.springframework.data.annotation.Id for my field then fetching from repository is working!

The issue is that I do not want to use 2 kind of @Id annotation at the same time. Moreover, I would like to use JPA annotation only because of other module is using JPA based repository layer (Spring Data JPA).

Does Spring Data Elasticsearch supports @Id annotation from JPA? It is very important to know because further what about embedded id? Does @EmbeddedId annotation is supported by Spring Data Elasticsearch?

My entity:

@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
}
2
Did you ever find a fix for this? I guess you can just leave the data.annotation.id on there?James111

2 Answers

0
votes

Yes, 1.3.0 does support @Id but you need a getter (maybe a bug?)

ElasticsearchTemplate.getPersistentEntityId takes your entity, tries to find the annotation @Id then returns the value of the id only if there is a getter defined.

However It doesn't seem to support @EmbeddedId: SimpleElasticsearchPersistentProperty.SUPPORTED_ID_PROPERTY_NAMES

0
votes

I have a similar issue, I am also using JPA and Elastic search both and it resolved after changing

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

to

javax.persistence.Id;

the default name of column id

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