gpt4 book ai didi

java - Spring @Async : null hibernate session on LAZY collection

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:05:56 28 4
gpt4 key购买 nike

我在服务层方法上使用了 @Async 注释。

当我 EAGERLY 加载 @OneToMany 集合字段时一切正常,但是当我尝试访问 LAZY 加载元素时我发现 Hibernate SessionImplementor 对象 session 为空。这显然给了我一个异常(exception):

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role:
....

这是我的 Collection 字段:

@OneToMany(mappedBy="abc", fetch=FetchType.LAZY, cascade=CascadeType.REMOVE)
@OrderBy(value="xsd asc")
@JsonIgnore
private Set<Item> items = new HashSet<Item>();

如何绑定(bind) hibernate session 以便在 @Async 上下文中延迟加载我的对象?

编辑

这是我的 trancactionManager/entityManager 配置

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

<tx:annotation-driven transaction-manager="transactionManager" />

<bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter">

</property>
<property name="packagesToScan" value="it.domain"/>

<property name="persistenceUnitName" value="persistenceUnit"/>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
<!--${hibernate.format_sql} -->
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<!-- ${hibernate.show_sql} -->
<prop key="hibernate.show_sql">false</prop>

<prop key="hibernate.connection.charSet">UTF-8</prop>

<prop key="hibernate.max_fetch_depth">3</prop>
<prop key="hibernate.jdbc.fetch_size">50</prop>
<prop key="hibernate.jdbc.batch_size">20</prop>

<prop key="org.hibernate.envers.audit_table_suffix">_H</prop>
<prop key="org.hibernate.envers.revision_field_name">AUDIT_REVISION</prop>
<prop key="org.hibernate.envers.revision_type_field_name">ACTION_TYPE</prop>
<prop key="org.hibernate.envers.audit_strategy">org.hibernate.envers.strategy.ValidityAuditStrategy</prop>
<prop key="org.hibernate.envers.audit_strategy_validity_end_rev_field_name">AUDIT_REVISION_END</prop>
<prop key="org.hibernate.envers.audit_strategy_validity_store_revend_timestamp">True</prop>
<prop key="org.hibernate.envers.audit_strategy_validity_revend_timestamp_field_name">AUDIT_REVISION_END_TS</prop>
</props>
</property>
</bean>

<jpa:repositories base-package="it.repository"
entity-manager-factory-ref="emf"
transaction-manager-ref="transactionManager"/>

<jpa:auditing auditor-aware-ref="auditorAwareBean" />
<bean id="auditorAwareBean" class="it.auditor.AuditorAwareBean"/>

最佳答案

我遇到了同样的问题,花了几天时间试图找到解决方案,终于找到了解决方案。我想与可能遇到相同问题的人分享我发现的详细信息。

第 1 - 您的 @Async 注释方法应该在单独的 bean 中声明,而不是 @Controller@RestController 注释 bean 。

第二 - 您当然需要声明从 @Async 声明的方法中调用的方法 @Transactional。但是,从 @Async 方法调用的第一个方法必须定义为 @Transactional。我在方法执行堆栈的第二级或第三级有 @Transactional 方法,因此问题没有解决,我花了两天时间试图弄清楚发生了什么。

所以最好的办法是

@Controller
ControllerBean {

@Autowired
AsyncService asyncService;

public controllerMethod() {
asyncService.asyncMethod();
}
}

@Service
AsyncService {
@Autowired
TransactionalService transactionalService;

@Async
public asyncMethod() {
transactionalService.transactionalMethod();
}
}

@Service
TransactionalService {
@Autowired
SomeOtherService someOtherService;

@Autowired
EntityRepository entityRepository;

@Transactional
public transactionalMethod() {
Entity entity = entityRepository.findOne(12345);

someOtherService.doWork(entity);
}
}

@Service
SomeOtherService {

@Autowired
EntityRepository entityRepository;

@Transactional
public doWork(Entity entity) {
// fetch lazy properties, save, etc. without any session problems...
entity.getLazyProperties();

entityRepository.save(entity);
}
}

关于java - Spring @Async : null hibernate session on LAZY collection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25083295/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com