'How to pass parameter to a namedQuery from a camel route?

I have an application running camel on spring-boot. I want to pass a parameter retriesAllowed to a namedQuery from a camel route.

namedQuery:

@NamedQuery(name = "findFailedMessages", query = RawMessageTx.HQL_FIND_FAILED_MESSAGES)
public class RawMessageTx extends BaseEntityWithTransmitStatus implements Serializable {

public static final String HQL_FIND_FAILED_MESSAGES = "SELECT x from RawMessageTx x WHERE x.status = 'FAILED' and x.tryAgain = 1 and x.retriesAttempted <= :retriesAllowed ORDER BY x.created ";

Route:

from("seda:retry_poll_failed_messages").routeId("retry_poll_failed_messages")
    .setHeader("retriesAllowed", constant(retriesAllowed)) 
    .toF("jpa:%s?namedQuery=findFailedMessages&maximumResults=%d", RawMessageTx.class.getName(), 
     maxMessagesPerPollAttempt)
       .split(body())
       .to("direct:anotherEndpoint");

I tried different things and it does not work and I can't find a good example online on this.



Solution 1:[1]

This is explained in the doc of JPA component:

parameters: This key/value mapping is used for building the query parameters. It is expected to be of the generic type java.util.Map where the keys are the named parameters of a given JPA query

This means that you have to do something like:

to("jpa:...?parameters=#myMap")

Where "myMap" is the name of a bean of type 'java.util.Map' that is available in Camel registry.

One possible way (among others) to register such bean could be a Camel Processor (to invoke before the jpa endpoint of course)

public void process(Exchange exchange) throws Exception {
    Map<String, Object> params = Map.of("status", "demo");
    exchange.getContext().getRegistry().bind("myMap", params); 
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 TacheDeChoco