I am trying to create an application where i can perform elastic search capabilities via spring boot and java, but i am facing java.lang.IllegalArgumentException: Unsupported ID type class elastic.model.Identifier
Here is how the code looks
I have model classes as
package elastic.model;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
@Document(indexName="customCustomer", type="elkcustomer")
public class Customer {
@Id
private Identifier identifier;
private Order order;
private String homeAddress;
private String contact;
//getters, setters, hashcode, equals, toString()
Identifier as
package elastic.model;
import java.io.Serializable;
public class Identifier implements Serializable {
private String id;
private String firstName;
private String lastName;
//getters, setters, hashcode,equals, toString()
Order as
package elastic.model;
import java.io.Serializable;
public class Order implements Serializable {
private int orderId;
private String orderDescription;
private float price;
//getters, setters, hashcode, equals, toString
Repository as
package elastic.repository;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;
import elastic.model.Customer;
import elastic.model.Identifier;
@Repository
public interface CustomerRepository extends ElasticsearchRepository<Customer, Identifier> {
}
And main runner class as
package elastic.service;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
import elastic.model.Customer;
import elastic.model.Identifier;
import elastic.model.Order;
import elastic.repository.CustomerRepository;
@SpringBootApplication
@EnableElasticsearchRepositories(basePackages="elastic")
public class ElasticSearchApplication implements CommandLineRunner {
@Autowired private CustomerRepository customerRepository;
public static void main(String[] args) {
SpringApplication.run(ElasticSearchApplication.class);
}
public void run(String... args) throws Exception {
this.customerRepository.deleteAll();
saveCustomers();
}
private void saveCustomers() {
this.customerRepository.saveAll(generateCustomers());
}
private List<Customer> generateCustomers() {
List<Customer> customers = new ArrayList<Customer>();
Customer customer1 = new Customer();
Identifier identifier1 = new Identifier();
identifier1.setId("1001");
identifier1.setFirstName("Rahul");
identifier1.setLastName("Lao");
customer1.setIdentifier(identifier1);
Order order1 = new Order();
order1.setOrderId(1);
order1.setOrderDescription("Order 1");
order1.setPrice(23.45f);
customer1.setOrder(order1);
customer1.setHomeAddress("Germany");
customer1.setContact("9874563210");
customers.add(customer1);
return customers;
}
}
And exception that i face is
Caused by: java.lang.IllegalArgumentException: Unsupported ID type class elastic.model.Identifier
at org.springframework.data.elasticsearch.repository.support.ElasticsearchRepositoryFactory.getRepositoryBaseClass(ElasticsearchRepositoryFactory.java:90)
at org.springframework.data.repository.core.support.RepositoryFactorySupport.lambda$getRepositoryInformation$2(RepositoryFactorySupport.java:378)
at java.util.concurrent.ConcurrentMap.computeIfAbsent(ConcurrentMap.java:324)
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepositoryInformation(RepositoryFactorySupport.java:376)
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:294)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.lambda$afterPropertiesSet$3(RepositoryFactoryBeanSupport.java:287)
at org.springframework.data.util.Lazy.getNullable(Lazy.java:141)
at org.springframework.data.util.Lazy.get(Lazy.java:63)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:290)
at org.springframework.data.elasticsearch.repository.support.ElasticsearchRepositoryFactoryBean.afterPropertiesSet(ElasticsearchRepositoryFactoryBean.java:67)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1763)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1700)
... 29 more
How can i resolve this, so that i can take custom classes as ID and not just primitive data type such as String, long, int as id.
Thanks.