hibernate - 无法延迟初始化角色集合,无法初始化代理 - 没有会话
问题描述
我在延迟加载时遇到了问题。这适用于急切加载。下面是 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;
}
请为此建议我一个解决方案。
解决方案
我假设您Hibernate.initialize(session.getSmsStudentArchives())
在访问惰性子元素之前需要。
但这会导致每个会话的额外查询。您可以使用 NamedEntityGraph 来减少数据库访问:
@NamedEntityGraph(
name = "smsSchoolSessionWithStudentArchives",
attributeNodes = { @NamedAttributeNode("smsStudentArchives") })
public class SmsSchoolSession {
@EntityGraph("smsSchoolSessionWithStudentArchives")
List<SmsSchoolSession> findBySmsSchool_IdOrderByStartDateDesc(whatever id);
有了这个,所有会话元素都将具有可用的归档元素,而无需使用Hibernate.initialize
.
推荐阅读
- c++ - 所有 C++ 运算符都采用操作数吗?
- python - 如何在一个窗口中使用一个 selenium webdriver 进行多个浏览选项卡并单独访问它们?
- c++ - 包装 C++ 类时不完整的类型 GLFWwindow
- c# - 如何在c#中添加Rfc3339日期时间字符串以增加一秒
- python - 在固定宽度文本文件的多个位置添加分隔符的问题
- android - 如何将 Unity 应用程序集成到原生 android studio 项目中
- machine-learning - 什么是负采样方法使用 - sigmoid 或 softmax?
- amazon-web-services - AWS - {lambda function} 可能没有定义授权
- python - Python 3(Linux 和 Windows)中的 CPU 温度
- javascript - 如何使用 ES6 语法导入 pg-promise?