You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by golivamsi84 <go...@gmail.com> on 2013/03/04 16:31:08 UTC

Update database using Java DSL JPA

Hello,

I am pretty new in using camel and we are just trying to incorporate it in
our architecture, so everyone here is pretty new to Camel. So please forgive
me if this so basic question.

Question:
I am trying to access and update oracle database using JPA Hibernate. So I
created my route for now which reads data from the database, I believe it
just runs a select query and I can see the results.

from("oracleJpa://com.nationwide.financialresponsibility.model.Driver?consumeDelete=false&maximumResults=3&consumer.delay=5000")
		.to("stream:out");

So can someone help me how I can update records in the database.Please
explain me in detail if you can so that I can understand the flow. We are
struggling on this one for a while now.

thanks



--
View this message in context: http://camel.465427.n5.nabble.com/Update-database-using-Java-DSL-JPA-tp5728463.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Update database using Java DSL JPA

Posted by Baalu <go...@gmail.com>.
Are you asking to do something like this:

from("jpa1://Entity1")
 bean:beanName[?method=someMethod]
  .to("jpa1://Entity2")  

If possible can you tell me the actual flow...

I have Entity1 and Entity2, I need to access Entity1, get a value of a
column from Entity1 based on a criteria then take that value and update a
column in Entity2.



--
View this message in context: http://camel.465427.n5.nabble.com/Update-database-using-Java-DSL-JPA-tp5728463p5728776.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Update database using Java DSL JPA

Posted by Claus Ibsen <cl...@gmail.com>.
On Tue, Mar 5, 2013 at 9:55 PM, Baalu <go...@gmail.com> wrote:
> I looked at your example and it used Camel 2.11, but we are using 2.10.3
> where we dont have a hibernate component.
>
> We are using JPA component, is there any way we can use consumer.query or
> consumer.nativequery or a namedquery on it to update the database. What is
> the best way to update one column in a route something like this:
>

I think these are only in use if you use camel-jpa in the from in a route, eg
from("jpa:....")


> from(direct:input)
> .to(create an update query)
> .to(jpa:entity)
>
> Can we use a parametrized UPDATE query somehow to specify the columns and
> values to update?
>

You can use the JPA API to do that. The idea with JPA is to abstract
the database layer and work with the objects.
So you fetch the entity using the JPA API. And then change the object
(entity) and then use the JPA API to merge / flush the changes.

Though for the latter, you can also use the Camel JPA producer, eg to in a route
.to("jpa:myEntityClassName")


PS: Sometimes working with SQL is easier.


>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Update-database-using-Java-DSL-JPA-tp5728463p5728601.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
Red Hat, Inc.
FuseSource is now part of Red Hat
Email: cibsen@redhat.com
Web: http://fusesource.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen

Re: Update database using Java DSL JPA

Posted by Baalu <go...@gmail.com>.
Hello guys, can anyone help me on this?



--
View this message in context: http://camel.465427.n5.nabble.com/Update-database-using-Java-DSL-JPA-tp5728463p5728768.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Update database using Java DSL JPA

Posted by Baalu <go...@gmail.com>.
I looked at your example and it used Camel 2.11, but we are using 2.10.3
where we dont have a hibernate component.

We are using JPA component, is there any way we can use consumer.query or
consumer.nativequery or a namedquery on it to update the database. What is
the best way to update one column in a route something like this:

from(direct:input)
.to(create an update query)
.to(jpa:entity)

Can we use a parametrized UPDATE query somehow to specify the columns and
values to update?



--
View this message in context: http://camel.465427.n5.nabble.com/Update-database-using-Java-DSL-JPA-tp5728463p5728601.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Update database using Java DSL JPA

Posted by Claus Ibsen <cl...@gmail.com>.
On Mon, Mar 4, 2013 at 8:14 PM, golivamsi84 <go...@gmail.com> wrote:
> thanks, it worked.
>
> But this is my other question, is it ok to write that logic in the Entity
> bean class? or should this logic be part of the class where the route is
> defined. Asking, so that I am following best practices.
>

JPA is about abstracting the database and work with Java beans.

You can take a look at some examples using SQL or Hibernate
http://camel.apache.org/sql-example.html
http://camel.apache.org/hibernate-example.html


> Since I am already here, I will ask the next question: Say I have table A
> and table B
>
> Now I need to update one of the column in table B with a value that I get
> from table A. To be exact, so based on some criteria I need to pull a column
> value in table A, then take that column value and update a column in table B
> with the value I pulled from table A.
>

Yeah with JPA its possible easier to use the JPA API directly from a
java bean and call that bean from a Camel route.

If using SQL, MyBatis you can though just call the SQL that would do
this update.


>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Update-database-using-Java-DSL-JPA-tp5728463p5728482.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
Red Hat, Inc.
FuseSource is now part of Red Hat
Email: cibsen@redhat.com
Web: http://fusesource.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen

Re: Update database using Java DSL JPA

Posted by golivamsi84 <go...@gmail.com>.
thanks, it worked.

But this is my other question, is it ok to write that logic in the Entity
bean class? or should this logic be part of the class where the route is
defined. Asking, so that I am following best practices.

Since I am already here, I will ask the next question: Say I have table A
and table B

Now I need to update one of the column in table B with a value that I get
from table A. To be exact, so based on some criteria I need to pull a column
value in table A, then take that column value and update a column in table B
with the value I pulled from table A.



--
View this message in context: http://camel.465427.n5.nabble.com/Update-database-using-Java-DSL-JPA-tp5728463p5728482.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Update database using Java DSL JPA

Posted by Claus Ibsen <cl...@gmail.com>.
On Mon, Mar 4, 2013 at 4:31 PM, golivamsi84 <go...@gmail.com> wrote:
> Hello,
>
> I am pretty new in using camel and we are just trying to incorporate it in
> our architecture, so everyone here is pretty new to Camel. So please forgive
> me if this so basic question.
>

Welcome to the community.


> Question:
> I am trying to access and update oracle database using JPA Hibernate. So I
> created my route for now which reads data from the database, I believe it
> just runs a select query and I can see the results.
>
> from("oracleJpa://com.nationwide.financialresponsibility.model.Driver?consumeDelete=false&maximumResults=3&consumer.delay=5000")
>                 .to("stream:out");
>
> So can someone help me how I can update records in the database.Please
> explain me in detail if you can so that I can understand the flow. We are
> struggling on this one for a while now.
>

It depends what you want to update.

The JPA component in the <from> is designed to be a data transfer with
a table having the data.
So the idea is that Camel polls the table for new row(s). And after
they have been processed.
The row(s) can either be DELETED or UPDATED. And for updated you would
for example need
to set some column as a "flag" to indicate they have been processed.
So on the next poll these row(s)
is not picked up again.

There is a @Consumed  annotation you should add to a method on your
Entity bean. Then Camel calls that to update the entity. Then you can
use java code to set the "flag".

There is a little example from unti test here
https://svn.apache.org/repos/asf/camel/trunk/components/camel-jpa/src/test/java/org/apache/camel/examples/MultiSteps.java

> thanks
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Update-database-using-Java-DSL-JPA-tp5728463.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
Red Hat, Inc.
FuseSource is now part of Red Hat
Email: cibsen@redhat.com
Web: http://fusesource.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen