未能懒惰地初始化角色集合,无法初始化代理 - 没有会话

时间:2018-06-12 11:32:01

标签: hibernate spring-mvc java-ee spring-data-jpa lazy-initialization

我在延迟加载时遇到了问题。这是正常的加载渴望。 以下是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;
    }

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

1 个答案:

答案 0 :(得分:0)

我认为在访问惰性子元素之前你需要Hibernate.initialize(session.getSmsStudentArchives())

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

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

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

使用此Hibernate.initialize

,所有会话元素都可以使用归档元素