如何通过标头在Apache Camel JPA中传递namedQuery参数? [英] how to pass namedQuery parameters in Apache Camel JPA by header?
问题描述
我有这条骆驼路线:
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" +
"¶meters={\"id\":${headers.id}}", dataClass.getName());
from("jetty://http://localhost:8080/test").toD(jpaString) // note the .toD
.marshal(format)
}
这是StringToMapTypeConverter
类,否则camel 无法将{"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);
}
}
记得将它添加到上下文中.在春天是这样的:
Remember to add it to the context. In Spring is something like:
<bean id="myStringToMapTypeConverter" class="....StringToMapTypeConverter" />
参考:
- http://camel.apache.org/jpa.html
- http://camel.apache.org/message-endpoint.html#MessageEndpoint-DynamicTo
- http://camel.apache.org/type-converter.html#TypeConverter-Addtypeconverterclassesatruntime
这篇关于如何通过标头在Apache Camel JPA中传递namedQuery参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!