3

I have this camel route:

from("direct:getUser")
    .pollEnrich("jpa://User?namedQuery=User.findById&consumeDelete=false");

This is my user Entity:

@Entity
@NamedQueries({
    @NamedQuery(name="User.findAll", query="SELECT u FROM User u"),
    @NamedQuery(name="User.findById", query="SELECT u FROM User u WHERE u.id = :id")
})
public class User{
    @Id
    private String id;
}

I have tried this route by setting the header:

from("direct:getUser")
    .setHeader("id", simple("myid"))
    .pollEnrich("jpa://User?namedQuery=User.findById&consumeDelete=false");

But it is not working

Is there any method to set jpa properties by the headers? The camel documentation quote this in parameters option but i don't found the examples

Options: parameters

This option is Registry based which requires the # notation. 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 and the values are their corresponding effective values you want to select for. Camel 2.19: it can be used for producer as well. When it's used for producer, Simple expression can be used as a parameter value. It allows you to retrieve parameter values from the message body header and etc.

smftr
  • 923
  • 3
  • 17
  • 31
  • Possible duplicate of [Camel JPA query parameters not being seen](http://stackoverflow.com/questions/36609830/camel-jpa-query-parameters-not-being-seen) – Strelok May 08 '17 at 03:16
  • But i don't understand how to use Registry. Nowhere else i found any examples. Could you give examples? – smftr May 08 '17 at 03:38

1 Answers1

5

I hope it's not too late to answer. In any case I had a similar issue in my project, the client does a HTTP GET with a parameter id, which is used by the JPA query and the result is finally marshalled back to the HTTP client. I'm running camel in a Spring application.

I finally figured out how to achieve it in a reasonably clean way.

This is the RouteBuilder where the route is defined:

@Override
public void configure() throws Exception {

    Class dataClass = SomeClass.class;
    JacksonDataFormat format = new JacksonDataFormat();
    format.setUnmarshalType(dataClass);

    String jpaString = String
            .format("jpa://%1$s?resultClass=%1$s&namedQuery=q1" +
                    "&parameters={\"id\":${headers.id}}", dataClass.getName());

    from("jetty://http://localhost:8080/test").toD(jpaString) // note the .toD
        .marshal(format)
}

And this is the StringToMapTypeConverter class, otherwise camel cannot convert {"id": X} to a map

public class StringToMapTypeConverter implements TypeConverters {

    private static final ObjectMapper mapper = new ObjectMapper();
    private static JavaType mapType;

    static {
        mapType = mapper.getTypeFactory().constructMapType(Map.class,
                String.class, Object.class);
    }

    @Converter
    public Map<String, Object> toMap(String map) throws IOException {
        return mapper.readValue(map, mapType);
    }
}

Remember to add it to the context. In Spring is something like:

<bean id="myStringToMapTypeConverter" class="....StringToMapTypeConverter" />

Refs:

flavio
  • 66
  • 3