'Spring Data JPA without Spring Boot: ClassNotFoundException JpaMetamodelCacheCleanup

I'm using spring-data-jpa and spring-context without spring boot in an application and every time I try to start my application with repositories I'm getting the following exception:

org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.springframework.data.jpa.util.JpaMetamodelCacheCleanup] for bean with name 'org.springframework.data.jpa.util.JpaMetamodelCacheCleanup'; nested exception is java.lang.ClassNotFoundException: org.springframework.data.jpa.util.JpaMetamodelCacheCleanup

It looks to me like this class is currently Package-Private scoped so I wouldn't be able to initialize it? Do I need to add an additional dependency or configuration?

My project is currently using:

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>2.6.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.15</version>
        </dependency>

I've configured the application to manually return an EntityManagerFactory (the project was using plain Hibernate before this) in a @Configuration class.

// main class in com.mybasebackage
@Configuration
@EnableJpaRepositories(basePackages = "com.mybasepackage.repository")
@ComponentScan(basePackages = {"com.mybasepackage"})
public class MyApplication {

    private ApplicationContext springApplicationContext;
    private AutowireCapableBeanFactory beanFactory;

    // called by external module hook (I'm integrating into an existing application)
    public void startup() {
        // exception on this line
        springApplicationContext = new AnnotationConfigApplicationContext(MyApplication.class); 
    }

}

// configuration class in com.mybasepackage
@Configuration
@EnableTransactionManagement
public class JPAConfig {

    @Bean
    public EntityManagerFactory entityManagerFactory() {
        // tldr getting an entity manager factory from an existing API that I know works
        return MyApplicationPersistenceContext.getEntityManagerFactory();
    }

}

// entity definition in com.mybasepackage.models
@Entity
public class MyEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    protected Long id;

    // etc...

}

// repository definition in com.mybasepackage.repository
@Repository
public interface MyEntityRepo extends CrudRepository<MyEntity, Long> {}

I'm also NOT using the maven plugin because I believe I don't need that if I'm not using Spring Boot?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source