无法通过JPA出站网关插入

时间:2019-04-01 11:38:02

标签: spring-integration spring-integration-dsl

@Bean
public IntegrationFlow reimInvJpaOutbound() {
    return IntegrationFlows
                    .from("reimInvProcessChannel")
                    .handle(reimJpaHandlers.reimStgInsertJpaHandler())
                    .log()
                    .get(); 
}

@Component
@Transactional
public class ReIMJpaHandlers {
Logger logger = LoggerFactory.getLogger(this.getClass()); 

@PersistenceContext
protected EntityManager entityManager;

@Autowired
ReIMHistInvHdrStgRepository histRepo; 

@Autowired
ReIMInvHdrStgRepository stgRepo; 

@Autowired
ReIMErrInvHdrStgRepository errRepo; 

String responseQueryString = "select * from RMS16DEV.TSC_IM_DOC_HEAD_TEMP where error_ind != null"; 

@Bean
public JpaUpdatingOutboundEndpointSpec reimStgInsertJpaHandler() {
    System.out.println("Writing to reim stg");
    return Jpa
        .updatingGateway(entityManager)
        .entityClass(TSC_IM_DOC_HEAD_TEMP.class)
        ; 
}

@Bean
public JpaPollingChannelAdapter reimStgResponseJpaInboundAdapter() {
    return Jpa
            .inboundAdapter(entityManager)
            .nativeQuery(responseQueryString)
            .maxResults(100)
            .get(); 
}
}

但是我遇到了以下错误:

javax.persistence.TransactionRequiredException: No EntityManager with actual transaction available for current thread - cannot reliably process 'merge' call
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:292) ~[spring-orm-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at com.sun.proxy.$Proxy189.merge(Unknown Source) ~[na:na]

1 个答案:

答案 0 :(得分:1)

您的

@Component
@Transactional
public class ReIMJpaHandlers {

无关紧要
@Bean
public JpaUpdatingOutboundEndpointSpec reimStgInsertJpaHandler() {

最后一个是bean,它生活在自己的生命周期中,其所有方法调用都已经在@Transactional上的ReIMJpaHandlers外部进行了。

您需要考虑为.handle(reimJpaHandlers.reimStgInsertJpaHandler())完全配置TX管理器:

.handle(reimJpaHandlers.reimStgInsertJpaHandler(), e -> e.transactional())

假设您有一个名称为transactionManager的bean。

该类上的@Transactional适用于业务方法,但不适用于@Bean方法,当我们创建bean时,这些方法仅被调用一次。