1
votes

This is the enable lazy load plugin

<plugin>
                    <groupId>org.hibernate.orm.tooling</groupId>
                    <artifactId>hibernate-enhance-maven-plugin</artifactId>
                    <version>${hibernate.version}</version>
                    <executions>
                        <execution>
                            <configuration>
                                <!--<failOnError>true</failOnError>-->
                                <enableLazyInitialization>true</enableLazyInitialization>
                                <!--<enableDirtyTracking>true</enableDirtyTracking>-->
                                <!--<enableAssociationManagement>true</enableAssociationManagement>-->
                            </configuration>
                            <goals>
                                <goal>enhance</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>

Hibernate java based config properties are as folows

private Properties hibernateProperties(DataSourceConfiguration dataSourceConfiguration) {

        Properties properties = new Properties();

//        properties.setProperty("hibernate.hbm2ddl.auto", dataSourceConfiguration.getDdlGeneration());
        properties.put("hibernate.dialect", dataSourceConfiguration.getDialect());
        properties.put("hibernate.enable_lazy_load_no_trans", true);
        properties.put("hibernate.jpa.compliance.transaction", true);
        properties.put("hibernate.jpa.compliance.query", true);
        properties.put("hibernate.jdbc.batch_size", 30);
        properties.put("hibernate.order_inserts", true);
        properties.put("hibernate.jdbc.batch_versioned_data", true);
        properties.put("hibernate.ejb.use_class_enhancer",true);
        properties.put("hibernate.enhancer.enableLazyInitialization",true);
//        properties.setProperty("hibernate.enhancer.enableAssociationManagement","true");
        properties.put("hibernate.current_session_context_class", dataSourceConfiguration.getCurrentSession());
        properties.put("hibernate.show_sql", dataSourceConfiguration.getShowsql());
        properties.put("hibernate.format_sql", dataSourceConfiguration.getFormatsql());
        properties.put("hibernate.discriminator.ignore_explicit_for_joined", "true");


        return properties;
    }

and this is the persistance.xml

http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"> org.hibernate.jpa.HibernatePersistenceProvider

        <non-jta-data-source></non-jta-data-source>
        <properties>
            <!--<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/hcs?useSSL=false"/>
            <property name="javax.persistence.jdbc.user" value="root"/>
            <property name="javax.persistence.jdbc.password" value="root"/>-->

            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
            <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/hcs?useSSL=false"/>
            <property name="hibernate.connection.username" value="root"/>
            <property name="hibernate.connection.password" value="root"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
            <property name="hibernate.show_sql" value="true"/>
            <!--<property name="spring.jpa.properties.hibernate.ejb.use_class_enhancer" value="true"/>-->
            <!--<property name="hibernate.enhancer.enableLazyInitialization" value="true"/>-->
            <!-- <property name="hibernate.hbm2ddl.auto" value="validate"/>-->
            <property name="hibernate.discriminator.ignore_explicit_for_joined" value="true"/>
        </properties>
    </persistence-unit>

</persistence>

At runtime i am getting the below exception

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in com.orsbv.hcs.config.HCSRepositoryContext: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Must start with Java agent to use InstrumentationLoadTimeWeaver. See Spring documentation. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1704) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:583) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502) at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:312) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1083) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:858) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:409) at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:291) at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:103) at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4939) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5434) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748) Caused by: java.lang.IllegalStateException: Must start with Java agent to use InstrumentationLoadTimeWeaver. See Spring documentation. at org.springframework.util.Assert.state(Assert.java:73) at org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver.addTransformer(InstrumentationLoadTimeWeaver.java:89) at org.springframework.orm.jpa.persistenceunit.SpringPersistenceUnitInfo.addTransformer(SpringPersistenceUnitInfo.java:85) at org.hibernate.jpa.boot.internal.PersistenceUnitInfoDescriptor.pushClassTransformer(PersistenceUnitInfoDescriptor.java:113) at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.(EntityManagerFactoryBuilderImpl.java:251) at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.(EntityManagerFactoryBuilderImpl.java:164) at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:51)

1

1 Answers

2
votes

finally lazy loading works with the below configuration

private Properties hibernateProperties(DataSourceConfiguration dataSourceConfiguration) {
        Properties properties = new Properties();
        properties.setProperty(AvailableSettings.HBM2DDL_AUTO, dataSourceConfiguration.getDdlGeneration());
        properties.setProperty(AvailableSettings.DIALECT, "org.hibernate.dialect.MySQLInnoDBDialect");
        properties.setProperty(AvailableSettings.IGNORE_EXPLICIT_DISCRIMINATOR_COLUMNS_FOR_JOINED_SUBCLASS, "true");
        properties.setProperty("hibernate.jpa.compliance.transaction", "true");
        properties.setProperty("hibernate.jpa.compliance.query", "true");
        properties.setProperty("hibernate.jpa.compliance.list", "true");
        properties.setProperty(AvailableSettings.JPA_ID_GENERATOR_GLOBAL_SCOPE_COMPLIANCE, "true");
        properties.setProperty(AvailableSettings.JPAQL_STRICT_COMPLIANCE, "true");
        properties.setProperty(AvailableSettings.ENABLE_LAZY_LOAD_NO_TRANS, "true");
        properties.setProperty(AvailableSettings.SHOW_SQL, dataSourceConfiguration.getShowsql());
        properties.setProperty(AvailableSettings.FORMAT_SQL, "false");
        properties.setProperty(AvailableSettings.CONNECTION_PROVIDER_DISABLES_AUTOCOMMIT, "true");
        properties.setProperty(AvailableSettings.MAX_FETCH_DEPTH, "4");
        properties.setProperty(AvailableSettings.DEFAULT_BATCH_FETCH_SIZE, "16");
        properties.setProperty(AvailableSettings.ORDER_UPDATES, "true");
        properties.setProperty(AvailableSettings.USE_SECOND_LEVEL_CACHE, "true");
        properties.setProperty(AvailableSettings.CACHE_REGION_FACTORY,
                "org.hibernate.cache.jcache.JCacheRegionFactory");
        properties.setProperty("hibernate.javax.cache.provider", "org.ehcache.jsr107.EhcacheCachingProvider");
        properties.setProperty(AvailableSettings.MULTI_TENANT, "DATABASE");
        properties.setProperty(AvailableSettings.MULTI_TENANT_IDENTIFIER_RESOLVER,
                TenantIdentifierResolver.class.getName());

        properties.put(AvailableSettings.MULTI_TENANT_CONNECTION_PROVIDER, multiTenantConnectionProvider());
        return properties;
    }