无法通过 jpa 出站网关插入

Not able insert through jpa outbound gateway

@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]

你的

@Component
@Transactional
public class ReIMJpaHandlers {

对于

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

最后一个是 bean,它存在于自己的生命周期中,并且它的所有方法调用都已经在 ReIMJpaHandlers 上的 @Transactional 之外发生了。

您需要考虑为 .handle(reimJpaHandlers.reimStgInsertJpaHandler()):

配置一个 TX 管理器
.handle(reimJpaHandlers.reimStgInsertJpaHandler(), e -> e.transactional())

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

class上的@Transactional用于业务方法,而不是@Bean方法,在我们创建bean时只调用一次。