1

In the Camel-JDBC component, we can send a select statement as a body to the jdbc endpoint, which returns the results.

Below sample code is from the Camel-JDBC website:

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

Why is such an option not present in the Camel-JPA component?

Using JPA endpoint as a consumer will poll the database. But, all I want is to just get the data once.

ndsurendra
  • 413
  • 1
  • 5
  • 15

1 Answers1

1

Camel doesn't have that feature because the JPA supports named queries. You can do something along these lines.

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
}
Matthew Fontana
  • 3,790
  • 2
  • 30
  • 50
  • But, this will still poll the DB right? I just want to query the DB once, not poll it. – ndsurendra May 23 '16 at 04:18
  • No this solution isn't polling. The 'pollEnrich' name is slightly confusing. Here is a comparison between enrich and pollEnrich: http://stackoverflow.com/questions/19098466/camel-content-enricher-enrich-vs-pollenrich – Matthew Fontana May 23 '16 at 11:04
  • For clarification Yes you will just call the database once and replace the contents of your exchange. Please feel free to try it out. – Matthew Fontana May 23 '16 at 11:04
  • Thanks @Matthew. My only concern with jpa is that the default behavior of "consumeDelete" being true seems very dangerous, just in case someone forgets to override it. – ndsurendra Jun 03 '16 at 09:57
  • @ndsurendra I completely understand that the first time I tried the component I hit that issue. Lucky this issue can easily be caught before an application goes to production. If your still somewhat nervous you could always write a quick maven or gradle plugin that found JPA endpoints and verified that consumeDelete="false". – Matthew Fontana Jun 03 '16 at 10:35