Spring Data Elasticsearch requires property named id -
i started work spring data elasticsearch , find issue. during run test calling findall() through repository getting:
no id property found class com.example.domain.entity.project! when add field @transient private long id; in project entity able retrieve results correctly. not want add field because have defined primary key named projectid. there annotation @id field also, why field projectid not treated id? looks @id annotation not working spring-data-elasticsearch, possible?
what should avoid adding transient id field entity? more workaround 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 , 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(); // 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>
use code , in import statement use other id class import
@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 , getters/setters }
Comments
Post a Comment