J'ai commencé à travailler avec Spring Data Elasticsearch et j'ai trouvé un problème. Durant le test de fonctionnement qui appelle findAll() par dépôt que je reçois:Données de ressort Elasticsearch requiert la propriété ID
No id property found for class com.example.domain.entity.Project!
Quand j'ajouter dans mon champ @Transient private Long id;
entité projet alors je suis en mesure de récupérer les résultats correctement. Mais je ne veux pas ajouter ce champ car j'ai déjà défini la clé primaire nommée projectId
. Il y a aussi l'annotation @Id
pour ce champ, alors pourquoi mon champ projectId
n'est pas traité comme ID? Il semble que l'annotation @Id
ne fonctionne pas pour spring-data-elasticsearch, est-ce possible?
Que dois-je faire pour éviter d'ajouter un champ id
transitoire à mon entité? Il est plus comme solution que la solution ...
classe du projet:
@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-contexte. 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>
pouvez-vous confirmer @Id est de org.springframework.data.annotation.Id? –
J'utilise '@ Id' de javax.persistence.Id; Je suis d'accord que l'utilisation de '@ Id 'supplémentaire de org.springframework résout le problème mais est toujours nécessaire pour que les deux annotations forment javax et spring parce que je l'utilise sur mon modèle général. – Roman