Error creating bean with name ‘entityManagerFactory’ defined in class path resource

I am trying to start a spring boot application that should connect to ElasticSearch using Hiberate Search. I am facing the error and could not able to understand the root cause of it. Your help is greatly appreciated

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-10-20 11:00:09.662 ERROR 34192 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is java.lang.NullPointerException
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1796) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:595) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:226) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1109) ~[spring-context-5.2.7.RELEASE.jar:5.2.7.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:869) ~[spring-context-5.2.7.RELEASE.jar:5.2.7.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:551) ~[spring-context-5.2.7.RELEASE.jar:5.2.7.RELEASE]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143) ~[spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758) ~[spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750) ~[spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE]
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) ~[spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) ~[spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237) ~[spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) ~[spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE]
	at com.rbbn.ems.fm.NosqlApplication.main(NosqlApplication.java:19) ~[classes/:na]
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is java.lang.NullPointerException
	at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:403) ~[spring-orm-5.2.7.RELEASE.jar:5.2.7.RELEASE]
	at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:378) ~[spring-orm-5.2.7.RELEASE.jar:5.2.7.RELEASE]
	at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341) ~[spring-orm-5.2.7.RELEASE.jar:5.2.7.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1855) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1792) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
	... 17 common frames omitted
Caused by: java.lang.NullPointerException: null
	at org.hibernate.search.mapper.orm.mapping.impl.HibernateOrmIndexedTypeContext.<init>(HibernateOrmIndexedTypeContext.java:36) ~[hibernate-search-mapper-orm-6.0.0.Beta8.jar:6.0.0.Beta8]
	at org.hibernate.search.mapper.orm.mapping.impl.HibernateOrmIndexedTypeContext.<init>(HibernateOrmIndexedTypeContext.java:24) ~[hibernate-search-mapper-orm-6.0.0.Beta8.jar:6.0.0.Beta8]
	at org.hibernate.search.mapper.orm.mapping.impl.HibernateOrmIndexedTypeContext$Builder.build(HibernateOrmIndexedTypeContext.java:127) ~[hibernate-search-mapper-orm-6.0.0.Beta8.jar:6.0.0.Beta8]
	at org.hibernate.search.mapper.orm.mapping.impl.HibernateOrmTypeContextContainer.<init>(HibernateOrmTypeContextContainer.java:41) ~[hibernate-search-mapper-orm-6.0.0.Beta8.jar:6.0.0.Beta8]
	at org.hibernate.search.mapper.orm.mapping.impl.HibernateOrmTypeContextContainer.<init>(HibernateOrmTypeContextContainer.java:26) ~[hibernate-search-mapper-orm-6.0.0.Beta8.jar:6.0.0.Beta8]
	at org.hibernate.search.mapper.orm.mapping.impl.HibernateOrmTypeContextContainer$Builder.build(HibernateOrmTypeContextContainer.java:143) ~[hibernate-search-mapper-orm-6.0.0.Beta8.jar:6.0.0.Beta8]
	at org.hibernate.search.mapper.orm.mapping.impl.HibernateOrmMappingPartialBuildState.bindToSessionFactory(HibernateOrmMappingPartialBuildState.java:30) ~[hibernate-search-mapper-orm-6.0.0.Beta8.jar:6.0.0.Beta8]
	at org.hibernate.search.mapper.orm.bootstrap.impl.HibernateOrmIntegrationBooterImpl.lambda$doBootSecondPhase$5(HibernateOrmIntegrationBooterImpl.java:274) ~[hibernate-search-mapper-orm-6.0.0.Beta8.jar:6.0.0.Beta8]
	at org.hibernate.search.engine.common.impl.SearchIntegrationPartialBuildStateImpl$SearchIntegrationFinalizerImpl.finalizeMapping(SearchIntegrationPartialBuildStateImpl.java:125) ~[hibernate-search-engine-6.0.0.Beta8.jar:6.0.0.Beta8]
	at org.hibernate.search.mapper.orm.bootstrap.impl.HibernateOrmIntegrationBooterImpl.doBootSecondPhase(HibernateOrmIntegrationBooterImpl.java:272) ~[hibernate-search-mapper-orm-6.0.0.Beta8.jar:6.0.0.Beta8]
	at org.hibernate.search.mapper.orm.bootstrap.impl.HibernateOrmIntegrationBooterImpl.bootNow(HibernateOrmIntegrationBooterImpl.java:198) ~[hibernate-search-mapper-orm-6.0.0.Beta8.jar:6.0.0.Beta8]
	at java.base/java.util.concurrent.CompletableFuture$UniApply.tryFire(CompletableFuture.java:642) ~[na:na]
	at java.base/java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:506) ~[na:na]
	at java.base/java.util.concurrent.CompletableFuture.complete(CompletableFuture.java:2073) ~[na:na]
	at org.hibernate.search.mapper.orm.bootstrap.impl.HibernateSearchSessionFactoryObserver.sessionFactoryCreated(HibernateSearchSessionFactoryObserver.java:41) ~[hibernate-search-mapper-orm-6.0.0.Beta8.jar:6.0.0.Beta8]
	at org.hibernate.internal.SessionFactoryObserverChain.sessionFactoryCreated(SessionFactoryObserverChain.java:35) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
	at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:382) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
	at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:469) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
	at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1259) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
	at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:58) ~[spring-orm-5.2.7.RELEASE.jar:5.2.7.RELEASE]
	at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365) ~[spring-orm-5.2.7.RELEASE.jar:5.2.7.RELEASE]
	at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:391) ~[spring-orm-5.2.7.RELEASE.jar:5.2.7.RELEASE]
	... 21 common frames omitted

Resolved this error by adding a Bean to the main application class.
@Bean(name=“entityManagerFactory”)
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
return sessionFactory;
}

I believe the NPE was replaced with a more explicit error message in Hibernate Search 6.0.0.Beta11 (HSEARCH-4025).

One possible cause: you’re trying to put @Indexed on an entity with an @IdClass. Entities with @IdClass cannot be mapped “as-is”; they are a bit more complicated than regular entities.

I recommend switching from from @IdClass to @EmbeddedId, then applying a custom identifier bridge on your ID property as explained here.

Alternatively, if you really need to stick with @IdClass, you can solve the problem by picking a unique property in your entity and annotating it with @DocumentId. What that property will contain is up to you, but it must be unique.