Spring Data Elasticsearch需要一个名为id的属性。

5
我开始使用Spring Data Elasticsearch,并遇到了问题。当我运行调用repository的findAll()测试时,出现以下错误:
No id property found for class com.example.domain.entity.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>

3
你能确认 @Id 是来自于 org.springframework.data.annotation.Id 吗? - Mohsin Husen
жҲ‘жӯЈеңЁдҪҝз”ЁжқҘиҮӘjavax.persistence.Idзҡ„@Id; жҲ‘еҗҢж„ҸдҪҝз”ЁжқҘиҮӘorg.springframeworkзҡ„йўқеӨ–@IdеҸҜд»Ҙи§ЈеҶій—®йўҳпјҢдҪҶд»Қ然йңҖиҰҒеҗҢж—¶е…·жңүjavaxе’Ңspringзҡ„дёӨдёӘжіЁйҮҠпјҢеӣ дёәжҲ‘е°Ҷе…¶з”ЁдәҺжҲ‘зҡ„йҖҡз”ЁжЁЎеһӢгҖӮ - Roman
2个回答

4
使用此代码,并在导入语句中使用其他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
}

对我有用。谢谢。 - goroncy

1

我们遇到了类似的问题。虽然它可能不完全符合你正在尝试做的事情,但我在SO上找到了这个问题,所以这也可能对其他人有帮助。

import org.springframework.data.annotation.Id;

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接