Spring Data Elasticsearch requires a property named id

I started working with Spring Data Elasticsearch and found the problem. When running a test that calls findAll () through the repository, I get:

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

      

When I add the field @Transient private Long id;

to my Project object, I can get the results correctly. But I don't want to add this field because I have already defined a primary key named projectId

. There is an annotation for this field as well @Id

, so why is my field projectId

not being treated as an identifier? Seems like annotation @Id

doesn't work for spring-data-elasticsearch, is it possible?

What can I do to avoid adding a temporary field to my object id

? This is more of a workaround than a solution ...

Project class:

@Entity
@Document(indexName = "project_list", type = "external")
public class Project implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @SequenceGenerator(name = "PROJECT_ID_GENERATOR", sequenceName = "PROJECT_SEQ", initialValue = 100, allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "PROJECT_ID_GENERATOR")
    @Column(name = "PROJECT_ID")
    private Long projectId;

    .... other fields and getters/setters
}

      

Repository:

@Repository
public interface EsProjectRepository extends ElasticsearchRepository<Project, Long> {

    List<Project> findByName(String name);
}

      

Test:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath*:test-es-context.xml" })
public class ProjectRepositoryTest {

    @Autowired
    private EsProjectRepository esProjectRepository;

    @Test
    public void shouldGetAllDocuments() {
        // when
        Iterable<Project> actuals = esProjectRepository.findAll();
        // then
        assertThat(actuals).isNotEmpty();
    }
}

      

Configuration (test-es-context.xml):

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:elasticsearch="http://www.springframework.org/schema/data/elasticsearch"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/data/elasticsearch http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch.xsd">

    <context:annotation-config />

    <context:property-placeholder location="classpath:test.properties" />

    <context:component-scan base-package="com.example.domain.entity, com.example.elasticsearch.*" />

    <elasticsearch:repositories base-package="com.example.elasticsearch.repository" />

    <elasticsearch:transport-client id="client" cluster-name="${es.cluster}" cluster-nodes="${es.host}:${es.port}" />

    <bean name="elasticsearchTemplate" class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
        <constructor-arg name="client" ref="client"/>
    </bean>

</beans>

      

+3


source to share


2 answers


Use this code and in the import statement use a different import of class Id



@Entity
@Document(indexName = "project_list", type = "external")
public class Project implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @org.springframework.data.annotation.Id
    @SequenceGenerator(name = "PROJECT_ID_GENERATOR", sequenceName = "PROJECT_SEQ", initialValue = 100, allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "PROJECT_ID_GENERATOR")
    @Column(name = "PROJECT_ID")
    private Long projectId;

    .... other fields and getters/setters
}

      

+1


source


We had similar problems. While this may not be a perfect match with what you are trying to do, I came up with this question on SO so that it might help others as well.



import org.springframework.data.annotation.Id;

@Id
private String _id = UUID.randomUUID().toString();

      

0


source







All Articles