how to pass namedQuery parameters in Apache Camel JPA by header?

牧云@^-^@ 提交于 2019-12-24 03:16:48

问题


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.


回答1:


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:

  • http://camel.apache.org/jpa.html
  • http://camel.apache.org/message-endpoint.html#MessageEndpoint-DynamicTo
  • http://camel.apache.org/type-converter.html#TypeConverter-Addtypeconverterclassesatruntime


来源:https://stackoverflow.com/questions/43836518/how-to-pass-namedquery-parameters-in-apache-camel-jpa-by-header

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!