如何通过标头在 Apache Camel JPA 中传递 namedQuery 参数? [英] how to pass namedQuery parameters in Apache Camel JPA by header?

查看:12
本文介绍了如何通过标头在 Apache Camel JPA 中传递 namedQuery 参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这条骆驼路线:

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

这是我的用户实体:

@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");

但它不起作用

有什么方法可以通过标题设置 jpa 属性吗?骆驼文档在 parameters 选项中引用了这个,但我没有找到示例

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

选项:参数

此选项基于注册表,需要 # 符号.这键/值映射用于构建查询参数.它是预计是泛型类型 java.util.Map 其中键是给定 JPA 查询的命名参数,值是是您要为其选择的相应有效值.骆驼2.19:也可以用于生产者.用于生产者时,可以使用简单表达式作为参数值.它允许您从消息正文标题中检索参数值等等.

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.

推荐答案

希望现在回答还为时不晚.在任何情况下,我的项目中都有类似的问题,客户端使用参数 id 执行 HTTP GET,JPA 查询使用该参数,结果最终编组回 HTTP 客户端.我在 Spring 应用程序中运行骆驼.

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.

这是定义路由的RouteBuilder:

@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)
}

这是 StringToMapTypeConverter 类,否则骆驼无法将 {"id": X} 转换为地图

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);
    }
}

记得将它添加到上下文中.在 Spring 中是这样的:

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

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

参考:

这篇关于如何通过标头在 Apache Camel JPA 中传递 namedQuery 参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆