[Solved]-"cannot resolve reference to bean 'jpasharedem_entitymanagerfactory' while setting bean property 'entitymanager'"


Check out the documentation for the @EnableJpaRepositories annotation.

In the Optional Element Summary, you'll find:

String entityManagerFactoryRef

Configures the name of the EntityManagerFactory bean definition to be used to create repositories discovered through this annotation.

Scrolling down the page to the details section, you'll see:

entityManagerFactoryRef

public abstract String entityManagerFactoryRef

Configures the name of the EntityManagerFactory bean definition to be used to create repositories discovered through this annotation. Defaults to entityManagerFactory.

Returns:

Default: "entityManagerFactory"

So, this "conventional" default configuration is specified by the @EnableJpaRepositories annotation itself.

Error Solution:

When encountering the error message Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument;, it indicates that Spring cannot find a bean named 'entityManagerFactory' to inject into a constructor argument.

To resolve this error, follow these steps:

  1. Ensure that you have defined a bean named 'entityManagerFactory' in your Spring configuration. This bean is typically responsible for creating and managing JPA entity managers.
  2. Check the package scanning configuration of your Spring application context to make sure that the class containing the 'entityManagerFactory' bean definition is being scanned and loaded by Spring.
  3. If you're using Spring Boot, verify that your application properties or configuration files are correctly configured to create the 'entityManagerFactory' bean.
  4. Make sure that the 'entityManagerFactory' bean definition is not misspelled and matches the name used in your code.
  5. If you're using component scanning, ensure that the class containing the 'entityManagerFactory' bean definition is annotated with @Configuration or @Component to make it discoverable by Spring.
  6. If you're still unable to resolve the issue, check for any circular dependencies or conflicts in your bean definitions that might be causing Spring to fail to create the 'entityManagerFactory' bean.

1. The exception indicates that there is no bean with the name entityManagerFactory. Place a debug point in the entityManagerFactory method of org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration class and check why the bean is not getting initialized.

Oh, from the discussion, I've concluded that the mentioned issue is likely due to older Hibernate dependencies. Additionally, ensure that the @Id annotation imported for all your entities is from javax.persistence.id.

"Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument"