首页 > 解决方案 > 无法延迟初始化角色集合,无法初始化代理 - 没有会话

问题描述

我在延迟加载时遇到了问题。这适用于急切加载。下面是 applicationContext.xml 的代码

    <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans                             http://www.springframework.org/schema/beans/spring-beans.xsd                            http://www.springframework.org/schema/context                             http://www.springframework.org/schema/context/spring-context.xsd                            http://www.springframework.org/schema/jdbc                             http://www.springframework.org/schema/jdbc/spring-jdbc.xsd                            http://www.springframework.org/schema/tx                            http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!-- Root Context: includes all context file -->
    <import resource="applicationContext-data.xml" />
    <import resource="applicationContext-service.xml" />
    <import resource="applicationContext-security-rest.xml" />
    <import resource="applicationContext-security-admin.xml" />
    <import resource="applicationContext-security-parent.xml" />
    <import resource="applicationContext-dozer.xml" />
    <import resource="batch/batch-launch-context.xml" />
    <bean id="appProperties"
        class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="locations">
            <list>
                <!-- Order matters, last one to create a property wins! -->
                <value>classpath:application.properties</value>
                <value>classpath:cron.properties</value>
            </list>
        </property>
        <property name="ignoreResourceNotFound" value="false" />
    </bean>
    <bean id="propertyConfigurer"
        class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
        <property name="properties" ref="appProperties" />
    </bean>

    <!-- Quartz scheduler details -->
</beans>

下面是 applicationContext-data.xml 的代码

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <jpa:repositories base-package="com.tappingpotentials.sms"
        repository-impl-postfix="CustomImpl" />

    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName" value="jdbc/tpSMSDS" />
        <property name="resourceRef" value="true" />
    </bean>

    <bean id="taskExecutor" class="org.springframework.core.task.SimpleAsyncTaskExecutor" />


    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    <tx:annotation-driven />

    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="com.tappingpotentials.sms" />


        <property name="jpaVendorAdapter">
            <bean id="jpaAdapter"
                class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="database" value="MYSQL" />
                <property name="showSql" value="false" />
                <property name="generateDdl" value="true" />
            </bean>
        </property>

        <property name="jpaProperties">
            <props>
                <prop key="hibernate.listeners.envers.autoRegister">false</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>

                <!-- <prop key="hibernate.connection.provider_class"> org.hibernate.c3p0.internal.C3P0ConnectionProvider 
                    </prop> -->
                <prop key="hibernate.c3p0.min_size">
                    1
                </prop>
                <prop key="hibernate.c3p0.max_size">
                    19
                </prop>
                <prop key="hibernate.c3p0.timeout">
                    120
                </prop>
                <prop key="hibernate.c3p0.max_statements">
                    10
                </prop>
            </props>
        </property>
    </bean>

</beans>

我已经尝试了每一件事。我认为事务注释不起作用。我在我的服务类中为该方法使用了@Transactional 注释。这是我的服务班

@Override
    @Transactional
    public SchoolSessionDetail saveSchoolSession(SchoolSessionDetail sessionDetail) {
        List<SmsSchoolSession> sessions = smsSchoolSessionRepository.findBySmsSchool_IdOrderByStartDateDesc(loggedInSchool().getId());
        sessions.forEach(session->{
            if(session.getIsCurrent() == (byte)1) {
                session.setIsCurrent((byte)0);
                saveStudentArchive(session.getSmsClassroom(), session.getSmsStudentArchives());
            }
        });
        SmsSchoolSession session = createNewSchoolSession(sessionDetail);
        sessions.add(session);
        if(smsSchoolSessionRepository.save(sessions) != null) {
            SmsSchoolSession currentSession = smsSchoolSessionRepository.findByIsCurrentAndSmsSchool((byte)1, loggedInSchool());
            updateSchoolSession(currentSession);
            return new SchoolSessionDetail(currentSession);
        }
        return null;
    }

请为此建议我一个解决方案。

标签: hibernatespring-mvcjakarta-eespring-data-jpalazy-initialization

解决方案


我假设您Hibernate.initialize(session.getSmsStudentArchives())在访问惰性子元素之前需要。

但这会导致每个会话的额外查询。您可以使用 NamedEntityGraph 来减少数据库访问:

@NamedEntityGraph( name = "smsSchoolSessionWithStudentArchives", attributeNodes = { @NamedAttributeNode("smsStudentArchives") }) public class SmsSchoolSession {

@EntityGraph("smsSchoolSessionWithStudentArchives") List<SmsSchoolSession> findBySmsSchool_IdOrderByStartDateDesc(whatever id);

有了这个,所有会话元素都将具有可用的归档元素,而无需使用Hibernate.initialize.


推荐阅读