You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2019/10/29 05:26:21 UTC

[camel] branch master updated: CAMEL-14108: camel-jpa - while persisting list of entity, no id returned for entity

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/master by this push:
     new f96914b  CAMEL-14108: camel-jpa - while persisting list of entity, no id returned for entity
f96914b is described below

commit f96914b1e7a5b1b00db5605716a4c55efaa3c7c6
Author: Claus Ibsen <cl...@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 2db38da..8411e17 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;
 
@@ -254,24 +255,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 9f3588e..2b53c65 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;
@@ -46,6 +47,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;
@@ -102,6 +104,43 @@ public class JpaTest extends Assert {
         assertEquals("address property", "foo@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("foo@bar.com"));
+                list.add(new SendEmail("foo2@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", "foo@bar.com", mail.getAddress());
+        assertNotNull("id", mail.getId());
+
+        SendEmail mail2 = (SendEmail) results.get(1);
+        assertEquals("address property", "foo2@bar.com", mail2.getAddress());
+        assertNotNull("id", mail2.getId());
+    }
+
     @Before
     public void setUp() throws Exception {
         template = camelContext.createProducerTemplate();
@@ -112,6 +151,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();
     }