Spring MVC 和休眠 Web 应用程序启用延迟加载无法正常工作



这是启用延迟加载插件

<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 的配置属性就像 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;
}

这就是坚持.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>

在运行时,我收到以下异常

org.springframework.beans.factory.BeanCreationException:创建在com.orsbv.hcs.config.HCSRepositoryContext中定义的名称为"entityManagerFactory"的bean时出错:初始化方法的调用失败;嵌套异常是java.lang.IllegalStateException:必须从Java代理开始才能使用InstrumentationLoadTimeWeaver。请参阅弹簧文档。 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) 原因:java.lang.IllegalStateException:必须从 Java 代理开始才能使用 InstrumentationLoadTimeWeaver。请参阅弹簧文档。 at org.springframework.util.Assert.state(Assert.java:73) at org.springframework.instrument.classload.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.(实体经理工厂建设者Impl.java:251) at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.(实体经理工厂建设者Impl.java:164) at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:51)

最后延迟加载适用于以下配置

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;
}

最新更新