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

时间:2019-04-11 07:04:32

标签: spring hibernate configuration

这是启用延迟加载插件

<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的配置属性如下

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

这是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>

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

org.springframework.beans.factory.BeanCreationException:在com.orsbv.hcs.config.HCSRepositoryContext中定义的名称为'entityManagerFactory'的bean创建时出错:调用init方法失败;嵌套异常为java.lang.IllegalStateException:必须以Java代理开头才能使用InstrumentationLoadTimeWeaver。参见Spring文档。     在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1704)     在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:583)     在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502)     在org.springframework.beans.factory.support.AbstractBeanFactory.lambda $ doGetBean $ 0(AbstractBeanFactory.java:312)     在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)     在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310)     在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)     在org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1083)     在org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:858)     在org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549)     在org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:409)     在org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:291)     在org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:103)     在org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4939)     在org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5434)     在org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)     在org.apache.catalina.core.ContainerBase $ StartChild.call(ContainerBase.java:1559)     在org.apache.catalina.core.ContainerBase $ StartChild.call(ContainerBase.java:1549)     在java.util.concurrent.FutureTask.run(FutureTask.java:266)     在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)     在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:624)     在java.lang.Thread.run(Thread.java:748) 原因:java.lang.IllegalStateException:必须以Java代理开头才能使用InstrumentationLoadTimeWeaver。参见Spring文档。     在org.springframework.util.Assert.state(Assert.java:73)     在org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver.addTransformer(InstrumentationLoadTimeWeaver.java:89)处     在org.springframework.orm.jpa.persistenceunit.SpringPersistenceUnitInfo.addTransformer(SpringPersistenceUnitInfo.java:85)处     在org.hibernate.jpa.boot.internal.PersistenceUnitInfoDescriptor.pushClassTransformer(PersistenceUnitInfoDescriptor.java:113)     在org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl。(EntityManagerFactoryBuilderImpl.java:251)     在org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl。(EntityManagerFactoryBuilderImpl.java:164)     在org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:51)

1 个答案:

答案 0 :(得分:1)

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

var result = [{
    "id": 1,
    "pid": 0,
    "name": "a",
    "select": 0,
    "child": [{
      "id": 7,
      "pid": 1,
      "name": "a-1",
      "select": 1,
      "child": [{
          "id": 8,
          "pid": 7,
          "name": "a-1-2",
          "select": 1
        },
        {
          "id": 9,
          "pid": 7,
          "name": "a-2-2",
          "select": 1
        }
      ]
    }]
  },
  {
    "id": 2,
    "pid": 0,
    "name": "b",
    "select": 0,
    "child": [{
      "id": 10,
      "pid": 2,
      "name": "b-1",
      "select": 0,
      "child": [{
          "id": 11,
          "pid": 10,
          "name": "b-1-1",
          "select": 1
        },
        {
          "id": 12,
          "pid": 10,
          "name": "b-1-2",
          "select": 1
        }
      ]
    }]
  },
  {
    "id": 3,
    "pid": 0,
    "name": "c",
    "select": 0,
    "child": [{
      "id": 13,
      "pid": 3,
      "name": "c-1",
      "select": 1,
      "child": [{
          "id": 14,
          "pid": 13,
          "name": "c-1-1",
          "select": 0
        },
        {
          "id": 15,
          "pid": 13,
          "name": "c-1-2",
          "select": 1
        }
      ]
    }]
  }
]

var selected = []
for (let i = 0; i < result.length; i++) {
  const a1 = result[i].child

  for (let j = 0; j < a1.length; j++) {
    console.log(a1[j].name)

    const a2 = a1[j].child

    for (let k = 0; k < a2.length; k++) {

      if (a1[j].id === a2[k].pid) {
        if (a2[k].select === 1) {
          console.log(a1[j].id)
          console.log(a2[k].name)

          console.log('yyyyyyyy', a2[k].id)

        }
      }
    }
  }
}

console.log(result)