在将域对象从数据库转换为客户端的资源对象期间,我遇到延迟加载字段的问题。
-
Customer
:使用惰性字段从数据库加载实体 -
FullCustomer
:将发送给客户端的实体。
服务层:
@Transactional(readOnly=true)
public Customer getById(Long id){
return customerRepository.getById(id);
}
Controller :
@Autowired
private ResourceAssembler<Customer, FullCustomer> converter;
@RequestMapping(...)
public final FullCustomer getCustomerById(long cid) {
Customer customer = customerService.getById(cid);
return converter.convert(customer);
}
转换器 ( ResourceAssembler<Customer, FullCustomer>
)
@Override
@Transactional(readOnly = true)
public FullCustomer convert(Customer input) {
System.err.println("Is open: " + TransactionSynchronizationManager.isActualTransactionActive()); //prints true
FullCustomer fullCustomer = new FullCustomer();
BeanUtils.copyProperties(input, fullCustomer); //Fails
return fullCustomer;
}
所以我的 Controller 使用转换器将数据库实体转换为客户端的实体。该转换会触发其他延迟加载实体的加载。
我的问题:虽然转换函数打开一个新事务( Is open
打印 true)
,但我收到此异常:
org.springframework.http.converter.HttpMessageNotWritableException:
Could not write content: failed to lazily initialize a collection of role: [..], could not initialize proxy - no Session (..);
nested exception is com.fasterxml.jackson.databind.JsonMappingException: failed to lazily initialize a collection of role: ...
在使用 BeanUtils 之前访问延迟加载的字段时,我得到以下信息:
org.hibernate.LazyInitializationException:
failed to lazily initialize a collection of role: [..], could not initialize proxy - no Session
为什么会发生这种情况?
最佳答案
您从一笔交易中加载客户,并且该交易不会初始化延迟加载的字段。然后提交该事务并关闭关联的 Hibernate session ,从而使客户实体分离。然后,您启动另一个事务,尝试初始化分离客户的惰性字段。
这是行不通的:您需要从与加载客户的事务相同的事务中加载惰性字段。所以
- 使您的 Controller 方法具有事务性,或者
- 初始化 getById() 中的惰性字段,或者
- 在存储库中使用联接提取,以确保查询加载客户和所需的关联,或者
- 使 getById() 返回 FullCustomer
- 使用 OpenEntityManagerInView 拦截器/过滤器(并使您的转换器成为非事务性的)
关于java - 在事务中转换实体时出现 LazyInitializationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31639529/