The org.springframework.beans.factory.UnsatisfiedDependencyException means your Spring Boot application couldn’t wire up the JpaTransactionManager because it’s missing a crucial dependency: a configured EntityManagerFactory.

Here’s why that’s the problem: The JpaTransactionManager relies on an EntityManagerFactory to actually perform database operations within a transaction. If Spring can’t find a properly configured EntityManagerFactory, it can’t create a transaction manager, and your app won’t start.

Common Causes and Fixes:

  1. Missing or Misconfigured DataSource: The EntityManagerFactory needs a DataSource to connect to the database. If you haven’t defined one, or it’s pointing to an invalid URL, username, or password, the EntityManagerFactory creation will fail.

    • Diagnosis: Check your application.properties or application.yml for spring.datasource.url, spring.datasource.username, and spring.datasource.password. Ensure they are correct and the database is accessible from where your application is running.
    • Fix:
      • In application.properties:
        spring.datasource.url=jdbc:postgresql://localhost:5432/mydatabase
        spring.datasource.username=myuser
        spring.datasource.password=mypassword
        spring.datasource.driver-class-name=org.postgresql.Driver
        
      • In application.yml:
        spring:
          datasource:
            url: jdbc:postgresql://localhost:5432/mydatabase
            username: myuser
            password: mypassword
            driver-class-name: org.postgresql.Driver
        
    • Why it works: This provides Spring Boot with the necessary connection details to establish a database connection, which is fundamental for creating an EntityManagerFactory.
  2. Missing JPA/Hibernate Dependency: Spring Boot auto-configures JPA and Hibernate if the relevant starter dependency is present. If it’s missing, the EntityManagerFactory won’t be created.

    • Diagnosis: Look for spring-boot-starter-data-jpa in your pom.xml (Maven) or build.gradle (Gradle).
    • Fix:
      • Maven (pom.xml):
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        
      • Gradle (build.gradle):
        implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
        
    • Why it works: This dependency brings in Hibernate (or another JPA provider) and Spring’s JPA support, enabling the auto-configuration of the EntityManagerFactory.
  3. Conflicting Transaction Manager Beans: If you manually define a PlatformTransactionManager or JpaTransactionManager bean, but Spring Boot’s auto-configuration also tries to create one (e.g., because you have multiple datasources), you can get conflicts.

    • Diagnosis: Search your project for @Bean definitions of PlatformTransactionManager or JpaTransactionManager.
    • Fix: If you have multiple datasources and need custom transaction managers, explicitly configure them using @Configuration and @EnableTransactionManagement, ensuring each has its own @Qualifier for the correct EntityManagerFactory. For a single datasource, usually, the auto-configuration is sufficient. If you must define one manually, ensure it’s correctly wired:
      import org.springframework.context.annotation.Bean;
      import org.springframework.context.annotation.Configuration;
      import org.springframework.orm.jpa.JpaTransactionManager;
      import org.springframework.transaction.PlatformTransactionManager;
      import javax.persistence.EntityManagerFactory;
      
      @Configuration
      public class TransactionManagerConfig {
      
          @Bean
          public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
              JpaTransactionManager transactionManager = new JpaTransactionManager();
              transactionManager.setEntityManagerFactory(entityManagerFactory);
              return transactionManager;
          }
      }
      
    • Why it works: This ensures that when you manually define a transaction manager, you are explicitly linking it to the EntityManagerFactory that Spring Boot has successfully configured.
  4. Incorrect Hibernate DDL-Auto Setting: While less common for UnsatisfiedDependencyException, an incorrect spring.jpa.hibernate.ddl-auto setting can sometimes prevent the EntityManagerFactory from initializing properly if it causes schema generation errors during startup.

    • Diagnosis: Check spring.jpa.hibernate.ddl-auto in your application.properties or application.yml.
    • Fix: For development, spring.jpa.hibernate.ddl-auto=update or create-drop is often used. For production, it should typically be validate or none. If you suspect this is an issue, try temporarily setting it to update to see if the factory initializes.
      • In application.properties:
        spring.jpa.hibernate.ddl-auto=update
        
      • In application.yml:
        spring:
          jpa:
            hibernate:
              ddl-auto: update
        
    • Why it works: update tells Hibernate to try and alter the database schema to match your entities. If the schema is fundamentally incompatible or missing, this can sometimes unblock the EntityManagerFactory initialization, especially if the error was a subtle schema mismatch.
  5. Missing JPA Provider Configuration: Spring Boot expects a JPA provider (like Hibernate) to be available. If you’ve explicitly excluded it or have a non-standard setup, the EntityManagerFactory won’t be created.

    • Diagnosis: Review your pom.xml or build.gradle for exclusions related to hibernate-core or jakarta.persistence-api.
    • Fix: Ensure you have a JPA provider dependency. If using Hibernate, it’s usually brought in transitively by spring-boot-starter-data-jpa. If you’ve overridden it, ensure you have something like:
      • Maven (pom.xml):
        <dependency>
            <groupId>org.hibernate.orm</groupId>
            <artifactId>hibernate-core</artifactId>
        </dependency>
        
      • Gradle (build.gradle):
        implementation 'org.hibernate.orm:hibernate-core'
        
    • Why it works: This explicitly adds the Hibernate ORM library, which Spring Boot’s JPA auto-configuration relies on to build the EntityManagerFactory.
  6. Custom EntityManagerFactory Bean with Incorrect Configuration: If you’ve created a custom @Bean for EntityManagerFactory and it’s not correctly configured (e.g., missing entity scan packages), it won’t be usable by the JpaTransactionManager.

    • Diagnosis: Search for a @Bean method returning LocalContainerEntityManagerFactoryBean or EntityManagerFactory. Check its configuration, especially setPackagesToScan().
    • Fix: Ensure your custom EntityManagerFactory bean correctly specifies the packages containing your JPA entities.
      import org.springframework.context.annotation.Bean;
      import org.springframework.context.annotation.Configuration;
      import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
      import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
      import javax.sql.DataSource;
      import java.util.Properties;
      
      @Configuration
      public class JpaConfig {
      
          @Bean
          public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) {
              LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
              emf.setDataSource(dataSource);
              emf.setPackagesToScan("com.yourcompany.yourapp.model"); // <-- Crucial line
              emf.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
      
              Properties properties = new Properties();
              properties.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
              properties.setProperty("hibernate.show_sql", "true");
              emf.setJpaProperties(properties);
      
              return emf;
          }
      }
      
    • Why it works: setPackagesToScan tells the JPA provider where to find your @Entity annotated classes, which are necessary for building the persistence unit and thus the EntityManagerFactory.

After resolving these, you might encounter a NoSuchBeanDefinitionException for your repository interfaces if the entity scan path was incorrect, preventing Spring Data JPA from finding and creating them.

Want structured learning?

Take the full Spring-boot course →