2

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

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

ERROR SpringApplication Application startup failed
 org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'elasticSearchApplication': Unsatisfied dependency expressed through field 'customerRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customerRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Unsupported ID type class elastic.model.Identifier
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:581)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:91)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:367)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1340)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:312)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:756)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:868)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:122)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:751)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:387)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:327)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1245)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1233)
    at elastic.service.ElasticSearchApplication.main(ElasticSearchApplication.java:24)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customerRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Unsupported ID type class elastic.model.Identifier
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1704)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:583)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:312)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:251)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1133)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1060)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:578)
    ... 19 more
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.

Loui
  • 533
  • 17
  • 33
  • Possible duplicate of [How to map a composite key with Hibernate?](https://stackoverflow.com/questions/3585034/how-to-map-a-composite-key-with-hibernate) – msp Nov 13 '17 at 15:45
  • Have you tried creating a [composite primary key](http://www.objectdb.com/java/jpa/entity/id#Composite_Primary_Key_)? – Val Nov 13 '17 at 15:48
  • @Val no i haven't tried that. Let me check that. Thanks. – Loui Nov 13 '17 at 15:51
  • @Val i get the same exception. – Loui Nov 13 '17 at 16:17
  • an identifier in elasticsearch is a single field. Please dont treat it like any other DB. – MohamedSanaulla Nov 13 '17 at 17:53
  • @Loui Did you found solution for this issue. I am facing the issue for a `Float` type primary key. – Avhi Nov 21 '18 at 14:42

0 Answers0