vladmihalcea / hypersistence-optimizer

Hypersistence Optimizer allows you to get the most out of JPA and Hibernate. By scanning your application configuration and mappings, Hypersistence Optimizer can tell you what changes you need to do to speed up your data access layer.
https://vladmihalcea.com/hypersistence-optimizer/
Apache License 2.0
318 stars 43 forks source link

IllegalAccessError while loading ApplicationContext #224

Open beoRich opened 11 months ago

beoRich commented 11 months ago

I am trying evaluate the hypersistence-optimizer in our spring boot hibernate application. After following installation guide and userguide and also succesfully testing out hypersistence-optimizer-public-parent I am running into a dead end with the following error:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [com/beontra/services/budgetforecast/common/config/TestHibernateConfig.class]: Invocation of init method failed; nested exception is java.lang.IllegalAccessError: class io.hypersistence.optimizer.hibernate.decorator.AbstractHibernate56SessionFactoryDecorator tried to access field org.hibernate.internal.FastSessionServices.eventListenerGroup_LOCK (io.hypersistence.optimizer.hibernate.decorator.AbstractHibernate56SessionFactoryDecorator and org.hibernate.internal.FastSessionServices are in unnamed module of loader 'app')
    at app//org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1786)
    at app//org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:602)
    at app//org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524)
    at app//org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
    at app//org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
    at app//org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
    at app//org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
    at app//org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1154)
    at app//org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:908)
    at app//org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583)
    at app//org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754)
    at app//org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:434)
    at app//org.springframework.boot.SpringApplication.run(SpringApplication.java:338)
    at app//org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:123)
    at app//org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:99)
    at app//org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
    ... 85 more
Caused by: java.lang.IllegalAccessError: class io.hypersistence.optimizer.hibernate.decorator.AbstractHibernate56SessionFactoryDecorator tried to access field org.hibernate.internal.FastSessionServices.eventListenerGroup_LOCK (io.hypersistence.optimizer.hibernate.decorator.AbstractHibernate56SessionFactoryDecorator and org.hibernate.internal.FastSessionServices are in unnamed module of loader 'app')
`

Hibernate Config File:

@Configuration
@Profile("test")
public class TestHibernateConfig {

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) {
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();

        setHibernateLogLevel();
        em.setDataSource(dataSource);
        em.setPackagesToScan(
<packages>
        );

        em.setJpaPropertyMap(hibernateProperties());
        em.setPersistenceProviderClass(HibernatePersistenceProvider.class);
        em.setJpaVendorAdapter(this.jpaVendorAdapter());

        return em;
    }

    private Map<String, Object> hibernateProperties() {
        Map<String, Object> hibernateProperties = new HashMap<>();
        hibernateProperties.put("hibernate.dialect", "com.beontra.services.budgetforecast.common.config.CustomDialect");
        hibernateProperties.put("hibernate.hbm2ddl.auto", "update");
        hibernateProperties.put("hibernate.show_sql", "true");

        return hibernateProperties;
    }
}

Hypersistence Configuration:

@Configuration
@Profile("test")
public class HypersistenceConfiguration {
    @Bean
    public HypersistenceOptimizer hypersistenceOptimizer(
            EntityManagerFactory entityManagerFactory) {
        return new HypersistenceOptimizer(
                new JpaConfig(entityManagerFactory)
        );
    }
}

Gradle dependencies: +--- org.springframework.boot:spring-boot-starter-web -> 2.5.2 () +--- org.springframework.boot:spring-boot-starter-data-jpa:2.6.4 | +--- org.springframework.boot:spring-boot-starter-aop:2.6.4 -> 2.5.2 | | +--- org.springframework.boot:spring-boot-starter:2.5.2 () | +--- org.springframework.boot:spring-boot-starter-jdbc:2.6.4 -> 2.5.2 | | +--- org.springframework.boot:spring-boot-starter:2.5.2 () | | +--- com.zaxxer:HikariCP:4.0.3 | | | --- org.slf4j:slf4j-api:1.7.30 -> 1.7.31 | | --- org.springframework:spring-jdbc:5.3.8 | | +--- org.springframework:spring-beans:5.3.8 () | | +--- org.springframework:spring-core:5.3.8 () | | --- org.springframework:spring-tx:5.3.8 () | +--- jakarta.transaction:jakarta.transaction-api:1.3.3 | +--- jakarta.persistence:jakarta.persistence-api:2.2.3 | +--- org.hibernate:hibernate-core:5.6.5.Final -> 5.4.32.Final | | +--- org.jboss.logging:jboss-logging:3.4.1.Final -> 3.4.2.Final

and

    testImplementation 'com.h2database:h2:1.4.200'
    testImplementation "io.hypersistence:hypersistence-optimizer:2.7.1-trial"

Also tried a different configuration aproach via HibernateTransactionManagerBootstrapExecutorConfiguration and run into the same issue.

Any help or hints would be appreciated!

vladmihalcea commented 11 months ago

@beoRich Try running your application with java --add-opens to allow Reflection access on the Hibernate classes.

beoRich commented 11 months ago

Thanks for your quick response.

Tried multiple variation (gradle test config or VM Options) --add-opens io.hypersistence.optimizer=org.hibernate.internal --add-open =org.hibernate.internal

But the error still remains

vladmihalcea commented 11 months ago

@beoRich Try to replicate it on the trial branch of this repository. It has many modules, including a Spring Boot one. If you can replicate it in this repository, then I will be able to investigate it and help you find the solution to this issue.