在我的Spring Boot 2.2.5.RELEASE应用程序中,当我尝试获取LAZY字段时遇到问题。
问题发生在服务方法createWayBillFromInvoice
中,该方法标记为@Transactional
。
情况A::我直接从控制器调用此方法,该方法执行没有问题。
案例B:我从另一个本身不是事务性的服务方法中调用了相同的方法 我的LAZY字段无法获取。
所以我认为这与事务管理有关,但是我找不到原因。
未获取我的LAZY字段(并在直接调用中获取)的方法:
@Transactional
public createWayBillFromInvoice(){
Invoice invoice = invoiceRepository.getInvoice(invoiceId);//invoice is selected
Order order = invoice.getOrder();
}
这里我的order
关系不是null,但是没有初始化并且没有代理,因此,如果我调用order.getOtherRelations
,它将返回空。
如果是B,我会打电话给类似的人
public void create(){ //This method IS NOT Transactional
invoiceService.createInvoice();//This method is Transactional
waybillService.createWaybilFromInvoice();//The method declared above
}
我尝试过:
create
方法Transactional
REQUIRES_NEW
的传播设置为createWaybillFromInvoice
以下是一些日志:
2020-06-04 17:17:16.337 DEBUG 1208 --- [nio-9002-exec-4] o.s.orm.jpa.JpaTransactionManager : Creating new transaction with name [io.salesart.billingservice.service.impl.WaybillServiceImpl.createWayBillFromInvoice]:
2020-06-04 17:17:16.376 DEBUG 1208 --- [nio-9002-exec-4] o.s.jdbc.datasource.DataSourceUtils : Changing isolation level of JDBC Connection [HikariProxyConnection@2044564149 wrapping org.postgresql.jdbc.PgConnection@27db45f] to 2
2020-06-04 17:17:16.450 DEBUG 1208 --- [nio-9002-exec-4] o.h.e.t.internal.TransactionImpl : On TransactionImpl creation, JpaCompliance#isJpaTransactionComplianceEnabled == false
2020-06-04 17:17:16.451 DEBUG 1208 --- [nio-9002-exec-4] o.h.e.t.internal.TransactionImpl : begin
2020-06-04 17:17:16.451 DEBUG 1208 --- [nio-9002-exec-4] org.postgresql.jdbc.PgConnection : setAutoCommit = false
2020-06-04 17:17:16.457 DEBUG 1208 --- [nio-9002-exec-4] o.s.orm.jpa.JpaTransactionManager : Exposing JPA transaction as JDBC [org.springframework.orm.jpa.vendor.HibernateJpaDialect$HibernateConnectionHandle@caf1ee0]
2020-06-04 17:17:19.532 DEBUG 1208 --- [nio-9002-exec-4] o.s.orm.jpa.JpaTransactionManager : Found thread-bound EntityManager [SessionImpl(1021250378<open>)] for JPA transaction