1

I am trying to get the primary key which is autogenerated after inserting data into the database using Camel. I saw, there was a JIRA issue(https://issues.apache.org/jira/browse/CAMEL-7313) and it was resolved.

                     from("file:data/source?noop=true")
                        .to("validator:books.xsd")
                        .split()
                        .tokenizeXML("book")
                        .unmarshal(jaxb)
                        .to("jpa:com.labs.Book")
                        .process(new Processor() {
                            public void process(Exchange exchange)
                                    throws Exception {
                                //here i want to get that primary key
                            }
                        });

Can someone point me to an example for this ....

Jared Farrish
  • 48,585
  • 17
  • 95
  • 104
Raju
  • 1,427
  • 2
  • 18
  • 31
  • I suggest to check how to do that with JPA as that is what you use for the database access. And there is likely ways to configure JPA to update the Entity with the primary key. – Claus Ibsen Jul 25 '14 at 20:43
  • Added the JPA tag as this is a JPA related question. Javaman a tip: tagging your questions with more tags will help it get decent exposure. – Namphibian Jul 25 '14 at 22:37
  • possible duplicate of [How to get Id of last persisted entity using JPA](http://stackoverflow.com/questions/9649515/how-to-get-id-of-last-persisted-entity-using-jpa) – Jared Farrish Jul 25 '14 at 22:58
  • @JaredFarrish Thank you Jared. But i want to know how to get it in camel routing. When i print headers after jpa persist, i don't find any header regarding generated keys. – Raju Jul 26 '14 at 04:43
  • @ClausIbsen Thank you claus. But i want to know how to get the sequence generated primary key after persisting. – Raju Jul 26 '14 at 04:44

2 Answers2

3

Yeah after some googling and suggestions from the experts above. I got the answer for my question

from("file:data/source?noop=true")
                        .to("validator:books.xsd")
                        .split()
                        .tokenizeXML("book")
                        .unmarshal(jaxb)
                        .to("jpa:com.labs.Book")
                        .process(new Processor() {
                            public void process(Exchange exchange)
                                    throws Exception {
                                //here it is
                                int Id = exchange.getIn().getBody(Book.class).getId();
                            }
                    });

Thank you everyone.

Raju
  • 1,427
  • 2
  • 18
  • 31
1

The primary key gets set on the entity class, so check that class. You need to configure your JPA entity with the JPA annotations, where you define which field is the primary key.

Just search the web or this site for jpa primary key

Claus Ibsen
  • 56,060
  • 7
  • 50
  • 65