简体   繁体   English

如何从 MessagingGateway 设置 Jpa 查询参数?

[英]How do I set the Jpa Query parameter from MessagingGateway?

I'm implementing Transactional Outbox pattern using Java. The Message Relay Service will poll the Outbox table for entries, and after an outbox message is found and processed it will update the Outbox entry.我正在使用 Java 实现事务性发件箱模式。消息中继服务将轮询发件箱表中的条目,在找到并处理发件箱消息后,它将更新发件箱条目。

Question is, how do I set the parameter from MessagingGateway to the Jpa Query?问题是,如何将参数从 MessagingGateway 设置为 Jpa 查询?

@Bean
public JpaExecutor jpaUpdateStateExecutor() {
    JpaExecutor jpaExecutor = new JpaExecutor(this.entityManagerFactory);
    jpaExecutor.setNamedQuery("myQuery");
    jpaExecutor.setUsePayloadAsParameterSource(true);
    jpaExecutor.setExpectSingleResult(true);
    return jpaExecutor;
}

@Bean
@ServiceActivator(inputChannel = "jpaChannel")
public MessageHandler jpaOutbound() {
    JpaOutboundGateway gateway = new JpaOutboundGateway(jpaUpdateStateExecutor());
    gateway.setGatewayType(OutboundGatewayType.UPDATING);
    return gateway;
}

My Gateway:我的网关:

@MessagingGateway
public interface MyGateway {
    @Gateway(requestChannel = "jpaChannel")
    @Transactional
    void jpaActions(Long idOfEntity);
}

My domain object:我的域名 object:

@Entity
@Getter
@Setter
@ToString
@NoArgsConstructor
@Table
@NamedQuery(name = "myQuery", query = "UPDATE MyTable SET state = 'PROCESSED' WHERE id = :id")
public class Outbox {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private Long id;

    private String state;
}

It would be great to see your query and how you'd like to have that Long idOfEntity to be mapped to some query param.很高兴看到您的查询以及您希望如何将Long idOfEntity映射到某个查询参数。

The doc for that setUsePayloadAsParameterSource looks like this:setUsePayloadAsParameterSource的文档如下所示:

use-payload-as-parameter-source使用有效负载作为参数源

If set to true, the payload of the Message is used as a source for parameters.如果设置为 true,消息的有效负载将用作参数源。 If set to false, the entire Message is available as a source for parameters.如果设置为 false,则整个消息都可用作参数源。 If no JPA Parameters are passed in, this property defaults to true.如果没有传入 JPA 参数,则该属性默认为true。 This means that, if you use a default BeanPropertyParameterSourceFactory , the bean properties of the payload are used as a source for parameter values for the JPA query.这意味着,如果您使用默认的BeanPropertyParameterSourceFactory ,有效负载的 bean 属性将用作 JPA 查询的参数值的来源。 However, if JPA Parameters are passed in, this property, by default, evaluates to false.但是,如果传入 JPA 参数,则默认情况下,此属性的计算结果为 false。 The reason is that JPA Parameters let you provide SpEL Expressions.原因是 JPA 参数让你提供 SpEL 表达式。 Therefore, it is highly beneficial to have access to the entire Message, including the headers.因此,访问整个消息(包括标题)是非常有益的。 Optional.选修的。

https://docs.spring.io/spring-integration/docs/current/reference/html/jpa.html#jpa-outbound-gateway-common-parameters https://docs.spring.io/spring-integration/docs/current/reference/html/jpa.html#jpa-outbound-gateway-common-parameters

Perhaps you'd prefer to have that option as false and then in your query you could use a param for that idOfEntity as just a :payload placeholder.也许您更愿意将该选项设置为false ,然后在您的查询中您可以使用该idOfEntity的参数作为:payload占位符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM