This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch camel-2.x in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/camel-2.x by this push: new 7419844 CAMEL-14108: camel-jpa - while persisting list of entity, no id returned for entity 7419844 is described below commit 741984463fd1031693dff853c3b2b74efc3bd2bb Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Tue Oct 29 06:25:57 2019 +0100 CAMEL-14108: camel-jpa - while persisting list of entity, no id returned for entity --- .../apache/camel/component/jpa/JpaProducer.java | 32 +++++++++++++---- .../org/apache/camel/component/jpa/JpaTest.java | 41 ++++++++++++++++++++++ 2 files changed, 67 insertions(+), 6 deletions(-) diff --git a/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaProducer.java b/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaProducer.java index 913e4de..06aa150 100644 --- a/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaProducer.java +++ b/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaProducer.java @@ -16,6 +16,7 @@ */ package org.apache.camel.component.jpa; +import java.util.ArrayList; import java.util.Collection; import java.util.Map; @@ -221,24 +222,43 @@ public class JpaProducer extends DefaultProducer { if (values.getClass().isArray()) { Object[] array = (Object[])values; - for (Object element : array) { + // need to create an array to store returned values as they can be updated + // by JPA such as setting auto assigned ids + Object[] managedArray = new Object[array.length]; + Object managedEntity; + for (int i = 0; i < array.length; i++) { + Object element = array[i]; if (!getEndpoint().isRemove()) { - save(element); + managedEntity = save(element); } else { - remove(element); + managedEntity = remove(element); } + managedArray[i] = managedEntity; + } + if (!getEndpoint().isUsePersist()) { + // and copy back to original array + System.arraycopy(managedArray, 0, array, 0, array.length); + exchange.getIn().setBody(array); } } else if (values instanceof Collection) { Collection<?> collection = (Collection<?>)values; + // need to create a list to store returned values as they can be updated + // by JPA such as setting auto assigned ids + Collection managedCollection = new ArrayList<>(collection.size()); + Object managedEntity; for (Object entity : collection) { if (!getEndpoint().isRemove()) { - save(entity); + managedEntity = save(entity); } else { - remove(entity); + managedEntity = remove(entity); } + managedCollection.add(managedEntity); + } + if (!getEndpoint().isUsePersist()) { + exchange.getIn().setBody(managedCollection); } } else { - Object managedEntity = null; + Object managedEntity; if (!getEndpoint().isRemove()) { managedEntity = save(values); } else { diff --git a/components/camel-jpa/src/test/java/org/apache/camel/component/jpa/JpaTest.java b/components/camel-jpa/src/test/java/org/apache/camel/component/jpa/JpaTest.java index e4bc79b..07de08d 100644 --- a/components/camel-jpa/src/test/java/org/apache/camel/component/jpa/JpaTest.java +++ b/components/camel-jpa/src/test/java/org/apache/camel/component/jpa/JpaTest.java @@ -16,6 +16,7 @@ */ package org.apache.camel.component.jpa; +import java.util.ArrayList; import java.util.List; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; @@ -51,6 +52,7 @@ public class JpaTest extends Assert { protected CamelContext camelContext = new DefaultCamelContext(); protected ProducerTemplate template; protected JpaEndpoint endpoint; + protected JpaEndpoint listEndpoint; protected EntityManager entityManager; protected TransactionTemplate transactionTemplate; protected Consumer consumer; @@ -107,6 +109,43 @@ public class JpaTest extends Assert { assertEquals("address property", "f...@bar.com", result.getAddress()); } + @Test + public void testProducerInsertsList() throws Exception { + transactionTemplate.execute(new TransactionCallback<Object>() { + public Object doInTransaction(TransactionStatus status) { + entityManager.joinTransaction(); + // lets delete any exiting records before the test + entityManager.createQuery("delete from " + entityName).executeUpdate(); + return null; + } + }); + + List<?> results = entityManager.createQuery(queryText).getResultList(); + assertEquals("Should have no results: " + results, 0, results.size()); + + // lets produce some objects + template.send(listEndpoint, new Processor() { + public void process(Exchange exchange) { + // use a list + List list = new ArrayList(); + list.add(new SendEmail("f...@bar.com")); + list.add(new SendEmail("f...@bar.com")); + exchange.getIn().setBody(list); + } + }); + + // now lets assert that there is a result + results = entityManager.createQuery(queryText).getResultList(); + assertEquals("Should have results: " + results, 2, results.size()); + SendEmail mail = (SendEmail) results.get(0); + assertEquals("address property", "f...@bar.com", mail.getAddress()); + assertNotNull("id", mail.getId()); + + SendEmail mail2 = (SendEmail) results.get(1); + assertEquals("address property", "f...@bar.com", mail2.getAddress()); + assertNotNull("id", mail2.getId()); + } + @Before public void setUp() throws Exception { template = camelContext.createProducerTemplate(); @@ -117,6 +156,8 @@ public class JpaTest extends Assert { assertTrue("Should be a JPA endpoint but was: " + value, value instanceof JpaEndpoint); endpoint = (JpaEndpoint) value; + listEndpoint = camelContext.getEndpoint(getEndpointUri() + "&entityType=java.util.List", JpaEndpoint.class); + transactionTemplate = endpoint.createTransactionTemplate(); entityManager = endpoint.createEntityManager(); }