问题
I started to work with Spring Data Elasticsearch and find issue. During run test which is calling findAll() through repository I am getting:
No id property found for class com.example.domain.entity.Project!
When I add field @Transient private Long id;
in my Project entity then I am able to retrieve results correctly. But I do not want to add this field because I have already defined primary key named projectId
. There is annotation @Id
for that field also, so why my field projectId
is not treated as ID? It looks like @Id
annotation is not working for spring-data-elasticsearch, is it possible?
What should I do to avoid adding transient id
field to my entity? It is more like workaround than 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>
回答1:
Use this Code and in the import statement use the 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 and getters/setters
}
回答2:
We had similar issues. While it might not be a perfect match with what you are trying to do, I did arrive at this question in SO, so this might help others as well.
import org.springframework.data.annotation.Id;
@Id
private String _id = UUID.randomUUID().toString();
来源:https://stackoverflow.com/questions/29855280/spring-data-elasticsearch-requires-property-named-id