1

在 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 端点作为消费者将轮询数据库。但是,我想要的只是获取一次数据。

4

1 回答 1

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
}
于 2016-05-12T11:38:06.887 回答