Camel-JPA:没有向JPA组件发送命令消息的选项(如在JDBC comopnent中)

时间:2016-05-12 08:43:41

标签: jpa apache-camel camel-jdbc

在Camel-JDBC组件中,我们可以将一个select语句作为正文发送到jdbc端点,该端点返回结果。

以下示例代码来自Camel-JDBC网站:

from("direct:projects")
   .setHeader("lic", constant("ASF"))
   .setHeader("min", constant(123))
   .setBody("select * from projects where license = :?lic and id > :?min order by id")
   .to("jdbc:myDataSource?useHeadersAsParameters=true")

为什么Camel-JPA组件中不存在这样的选项?

将JPA端点用作使用者将轮询数据库。但是,我想要的只是获取一次数据。

1 个答案:

答案 0 :(得分:1)

Camel没有该功能,因为JPA支持命名查询。你可以沿着这些方向做点什么。

from("direct:start")
    .pollEnrich("jpa:" + MyEntity.class.getName() + "?consumeDelete=false&consumer.namedQuery=myNamedQuery&consumer.parameters=#params", new MyAggregationStrategy())
.log(LoggingLevel.INFO, "call my entity toString method ${body}");



//spring context OR you can use camel registry
<util:map id="params" key-type="java.lang.String">
    <entry key="param1" value="1"/>
    <entry key="param2" value="2"/>
</util:map>

//JPA model
@Entity
@Table(name = "MyTable")
@NamedQuery(name = "myNamedQuery", query = "SELECT t FROM MyTable t WHERE t.columnName1 = :param1 AND t.columnName2 = :param2")
public class MyEntity implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Column(name = "PrimaryKey", updatable = false, nullable = false, length = 20)
    private String primaryKey;

    @Column(name = "Param1", length = 5)
    private String param1;

    @Column(name = "Param2", length = 5)
    private String param2;

    //removed setters getters, hascode, equals, and toString for brevity
}