首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring集成JpaExecutor

Spring集成JpaExecutor
EN

Stack Overflow用户
提问于 2019-02-14 23:50:58
回答 1查看 273关注 0票数 0

我想用JpaExecutor做update语句,如下所示

代码语言:javascript
复制
JdbcPollingChannelAdapter adapter = new JdbcPollingChannelAdapter(jdbcTemplate,"select * from EPAM_EVENT_STORE_T where EVENT_STATUS = 0");
adapter.setUpdateSql("update EPAM_EVENT_STORE_T set EVENT_STATUS = 1 where event_store_id in (:eventStoreId)");

但是JpaExecutor没有更新方法。

我在这个questionthere中见过肮脏的黑客。但说真的,有没有可能在没有肮脏的黑客的情况下做到这一点?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-15 16:31:24

所以,这就是我解决这个问题的方法。

代码语言:javascript
复制
@MessageEndpoint
@Log
public class MessageFromEvent {

  private final EntityManagerFactory entityManagerFactory;
  private final JpaTransactionManager transactionManager;

  @Autowired
  public MessageFromEvent(EntityManagerFactory entityManagerFactory,
      JpaTransactionManager transactionManager) {
    this.entityManagerFactory = entityManagerFactory;
    this.transactionManager = transactionManager;
  }

  @Bean
  @InboundChannelAdapter(channel = "selectEventChannel", poller = @Poller(fixedDelay = "10000"))
  public MessageSource<?> selectEvent() {
    log.info("STEP 1: Getting data form EventStore table");
    return new JpaPollingChannelAdapter(selectEventExecutor());
  }

  @Bean
  public JpaExecutor selectEventExecutor() {
    JpaExecutor executor = new JpaExecutor(this.entityManagerFactory);
    executor.setJpaQuery("select event from Event event where event.eventStatus = 0");
    executor.setUsePayloadAsParameterSource(true);
    executor.setEntityClass(Event.class);
    return executor;
  }

  @Bean
  @ServiceActivator(inputChannel = "selectEventChannel")
  public MessageHandler updateEventStatus(JpaExecutor updateEventExecutor) {
    log.info("STEP 2: Updating event status");
    JpaOutboundGateway gateway = new JpaOutboundGateway(updateEventExecutor);
    gateway.setGatewayType(OutboundGatewayType.UPDATING);

    MatchAlwaysTransactionAttributeSource attributeSource = new MatchAlwaysTransactionAttributeSource();
    attributeSource.setTransactionAttribute(new DefaultTransactionAttribute());
    TransactionInterceptor interceptor = new TransactionInterceptor(transactionManager, attributeSource);
    gateway.setAdviceChain(singletonList(interceptor));

    return gateway;
  }

  @Bean
  public JpaExecutor updateEventExecutor() {
    JpaExecutor executor = new JpaExecutor(this.entityManagerFactory);
    executor.setJpaQuery("update Event E set E.eventStatus = 1 where E.eventStoreId in (:eventStoreId)");
    executor.setJpaParameters(Collections.singletonList(new JpaParameter("Event.eventStoreId", null, "payload")));
    executor.setUsePayloadAsParameterSource(true);
    executor.setEntityClass(Event.class);
    return executor;
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54694276

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档