Spring Data Elasticsearch需要名为id的属性

时间:2015-04-24 18:49:42

标签: spring elasticsearch spring-data-elasticsearch

我开始使用Spring Data Elasticsearch并发现问题。 在运行测试期间,我通过存储库调用findAll():

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

当我在Project实体中添加字段@Transient private Long id;时,我能够正确检索结果。但我不想添加此字段,因为我已经定义了名为projectId的主键。该字段也有注释@Id,为什么我的字段projectId不被视为ID?看起来@Id注释不适用于spring-data-elasticsearch,是否可能?

我应该怎么做才能避免向我的实体添加临时id字段?它更像是解决方法而不是解决方案...

项目类:

@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
public interface EsProjectRepository extends ElasticsearchRepository<Project, Long> {

    List<Project> findByName(String name);
}

测试:

@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();
    }
}

配置(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>

2 个答案:

答案 0 :(得分:1)

使用此代码并在import语句中使用其他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 :(得分:0)

我们有类似的问题。虽然它可能不是你想要做的完美匹配,但我确实在SO中提出了这个问题,所以这也可能对其他人有所帮助。

import org.springframework.data.annotation.Id;

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