Search code examples
javaspringspring-boothibernatespring-data-jpa

java.lang.ClassNotFoundException: Could not load requested class : charset


I am attempting to add spring data jpa to an existing spring boot 2.7.3 web app. We're using hibernate 5.6.9. I've added spring data jpa as a maven dependency via the spring-boot-starter-data-jpa artifact id which is under the org.springframework.boot group id.

When I launch my spring boot web app, I eventually get the following error:

Caused by: java.lang.ClassNotFoundException: Could not load requested class : charset
    at org.hibernate.boot.registry.classloading.internal.AggregatedClassLoader.findClass(AggregatedClassLoader.java:210) ~[hibernate-core-5.6.10.Final.jar:5.6.10.Final]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:587) ~[?:?]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:520) ~[?:?]
    at java.lang.Class.forName0(Native Method) ~[?:?]
    at java.lang.Class.forName(Class.java:467) ~[?:?]
    at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:130) ~[hibernate-core-5.6.10.Final.jar:5.6.10.Final]
    at org.hibernate.boot.internal.ClassLoaderAccessImpl.classForName(ClassLoaderAccessImpl.java:67) ~[hibernate-core-5.6.10.Final.jar:5.6.10.Final]
    at org.hibernate.cfg.annotations.SimpleValueBinder.fillSimpleValue(SimpleValueBinder.java:536) ~[hibernate-core-5.6.10.Final.jar:5.6.10.Final]
    at org.hibernate.cfg.SetSimpleValueTypeSecondPass.doSecondPass(SetSimpleValueTypeSecondPass.java:25) ~[hibernate-core-5.6.10.Final.jar:5.6.10.Final]
    at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1653) ~[hibernate-core-5.6.10.Final.jar:5.6.10.Final]
    at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1621) ~[hibernate-core-5.6.10.Final.jar:5.6.10.Final]
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:295) ~[hibernate-core-5.6.10.Final.jar:5.6.10.Final]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:1460) ~[hibernate-core-5.6.10.Final.jar:5.6.10.Final]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1494) ~[hibernate-core-5.6.10.Final.jar:5.6.10.Final]
    at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:58) ~[spring-orm-5.3.22.jar:5.3.22]
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365) ~[spring-orm-5.3.22.jar:5.3.22]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:409) ~[spring-orm-5.3.22.jar:5.3.22]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:396) ~[spring-orm-5.3.22.jar:5.3.22]
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341) ~[spring-orm-5.3.22.jar:5.3.22]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1863) ~[spring-beans-5.3.22.jar:5.3.22]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1800) ~[spring-beans-5.3.22.jar:5.3.22]

Here is my spring data jpa config:

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        basePackages = "com.bottomline.dm.core.repository.roc",
        entityManagerFactoryRef = "rocJpaEntityManagerFactory",
        transactionManagerRef = "rocJpaTransactionManager"
)
public class RocJpaDatabaseConfig {

    private final DataSource rocDataSource;

    public RocJpaDatabaseConfig(ROCDataSourceProperties rocDataSourceProperties) {
        this.rocDataSource = rocDataSourceProperties.getPostgresHikariDataSource();
        DataSourceInfoLogger infoLogger = new DataSourceInfoLogger(rocDataSource);
        infoLogger.afterPropertiesSet();
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean rocJpaEntityManagerFactory() {
        LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
        factory.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
        factory.setPackagesToScan("com.bottomline.dm.core.domain.entity");
        factory.setDataSource(rocDataSource);
        return factory;
    }

    @Bean
    public PlatformTransactionManager rocJpaTransactionManager(EntityManagerFactory rocJpaEntityManagerFactory) {
        JpaTransactionManager txManager = new JpaTransactionManager();
        txManager.setEntityManagerFactory(rocJpaEntityManagerFactory);
        return txManager;
    }
}

Any ideas how to get passed this class not found on "charset"?


Solution

  • "charset" was a custom org.hibernate.annotations.Type that was defined in one of the existing entities in "com.bottomline.dm.core.domain.entity" which the EntityManagerFactory was scanning.

    Was able to finally get my application up and running by replacing the org.hibernate.annotations.Type annotations with org.hibernate.annotations.TypeDef instead at the class level.