You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by cm...@apache.org on 2010/07/23 08:06:57 UTC

svn commit: r966982 - in /camel/trunk/components/camel-jpa/src: main/java/org/apache/camel/component/jpa/ test/java/org/apache/camel/component/jpa/ test/java/org/apache/camel/examples/ test/resources/META-INF/

Author: cmueller
Date: Fri Jul 23 06:06:57 2010
New Revision: 966982

URL: http://svn.apache.org/viewvc?rev=966982&view=rev
Log:
CAMEL-2982: camel-jpa should support an option to switch from EntityManager.merge(entity) to EntityManager.persist(entity)

Added:
    camel/trunk/components/camel-jpa/src/test/java/org/apache/camel/component/jpa/JpaUsePersistTest.java   (with props)
    camel/trunk/components/camel-jpa/src/test/java/org/apache/camel/examples/Address.java   (with props)
    camel/trunk/components/camel-jpa/src/test/java/org/apache/camel/examples/Customer.java   (with props)
Modified:
    camel/trunk/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaEndpoint.java
    camel/trunk/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaProducer.java
    camel/trunk/components/camel-jpa/src/test/resources/META-INF/persistence.xml

Modified: camel/trunk/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaEndpoint.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaEndpoint.java?rev=966982&r1=966981&r2=966982&view=diff
==============================================================================
--- camel/trunk/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaEndpoint.java (original)
+++ camel/trunk/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaEndpoint.java Fri Jul 23 06:06:57 2010
@@ -53,6 +53,7 @@ public class JpaEndpoint extends Schedul
     private boolean consumeLockEntity = true;
     private boolean flushOnSend = true;
     private int maxMessagesPerPoll;
+    private boolean usePersist;
 
     public JpaEndpoint() {
     }
@@ -222,6 +223,14 @@ public class JpaEndpoint extends Schedul
     public void setMaxMessagesPerPoll(int maxMessagesPerPoll) {
         this.maxMessagesPerPoll = maxMessagesPerPoll;
     }
+    
+    public boolean isUsePersist() {
+        return usePersist;
+    }
+
+    public void setUsePersist(boolean usePersist) {
+        this.usePersist = usePersist;
+    }
 
     // Implementation methods
     // -------------------------------------------------------------------------

Modified: camel/trunk/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaProducer.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaProducer.java?rev=966982&r1=966981&r2=966982&view=diff
==============================================================================
--- camel/trunk/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaProducer.java (original)
+++ camel/trunk/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaProducer.java Fri Jul 23 06:06:57 2010
@@ -51,7 +51,11 @@ public class JpaProducer extends Default
                     Iterator iter = ObjectHelper.createIterator(values);
                     while (iter.hasNext()) {
                         Object value = iter.next();
-                        entityManager.merge(value);
+                        if (endpoint.isUsePersist()) {
+                            entityManager.persist(value);
+                        } else {
+                            entityManager.merge(value);
+                        }
                     }
                     if (endpoint.isFlushOnSend()) {
                         entityManager.flush();

Added: camel/trunk/components/camel-jpa/src/test/java/org/apache/camel/component/jpa/JpaUsePersistTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jpa/src/test/java/org/apache/camel/component/jpa/JpaUsePersistTest.java?rev=966982&view=auto
==============================================================================
--- camel/trunk/components/camel-jpa/src/test/java/org/apache/camel/component/jpa/JpaUsePersistTest.java (added)
+++ camel/trunk/components/camel-jpa/src/test/java/org/apache/camel/component/jpa/JpaUsePersistTest.java Fri Jul 23 06:06:57 2010
@@ -0,0 +1,191 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.jpa;
+
+import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+import javax.persistence.EntityManager;
+import javax.persistence.PersistenceException;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.Consumer;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.ProducerTemplate;
+import org.apache.camel.examples.Address;
+import org.apache.camel.examples.Customer;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.impl.DefaultExchange;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Ignore;
+import org.junit.Test;
+import org.springframework.orm.jpa.JpaCallback;
+import org.springframework.orm.jpa.JpaTemplate;
+
+import static org.apache.camel.util.ServiceHelper.startServices;
+import static org.apache.camel.util.ServiceHelper.stopServices;
+/**
+ * @version $Revision: 931444 $
+ */
+public class JpaUsePersistTest extends Assert {
+    
+    protected CamelContext camelContext = new DefaultCamelContext();
+    protected ProducerTemplate template;
+    protected JpaEndpoint endpoint;
+    protected TransactionStrategy transactionStrategy;
+    protected JpaTemplate jpaTemplate;
+    protected Consumer consumer;
+    protected Exchange receivedExchange;
+
+    @SuppressWarnings("unchecked")
+    @Before
+    public void setUp() throws Exception {
+        template = camelContext.createProducerTemplate();
+        startServices(template, camelContext);
+
+        endpoint = camelContext.getEndpoint("jpa://" + Customer.class.getName() + "?usePersist=true", JpaEndpoint.class);
+
+        transactionStrategy = endpoint.createTransactionStrategy();
+        jpaTemplate = endpoint.getTemplate();
+        
+        transactionStrategy.execute(new JpaCallback() {
+            public Object doInJpa(EntityManager entityManager) throws PersistenceException {
+                entityManager.createQuery("delete from " + Customer.class.getName()).executeUpdate();
+                return null;
+            }
+        });
+        
+        assertEntitiesInDatabase(0, Customer.class.getName());
+        assertEntitiesInDatabase(0, Address.class.getName());
+    }
+    
+    @After
+    public void tearDown() throws Exception {
+        stopServices(consumer, template, camelContext);
+    }
+    
+    @SuppressWarnings("unchecked")
+    @Test
+    public void produceNewEntity() throws Exception {
+        Customer customer = createDefaultCustomer();
+        
+        Exchange exchange = new DefaultExchange(camelContext);
+        exchange.getIn().setBody(customer);
+        Exchange returnedExchange = template.send(endpoint, exchange);
+        
+        Customer receivedCustomer = returnedExchange.getIn().getBody(Customer.class);
+        assertEquals(customer.getName(), receivedCustomer.getName());
+        assertNotNull(receivedCustomer.getId());
+        assertEquals(customer.getAddress().getAddressLine1(), receivedCustomer.getAddress().getAddressLine1());
+        assertEquals(customer.getAddress().getAddressLine2(), receivedCustomer.getAddress().getAddressLine2());
+        assertNotNull(receivedCustomer.getAddress().getId());
+        
+        List results = jpaTemplate.find("select o from " + Customer.class.getName() + " o");
+        assertEquals(1, results.size());
+        Customer persistedCustomer = (Customer) results.get(0);
+        assertEquals(receivedCustomer.getName(), persistedCustomer.getName());
+        assertEquals(receivedCustomer.getId(), persistedCustomer.getId());
+        assertEquals(receivedCustomer.getAddress().getAddressLine1(), persistedCustomer.getAddress().getAddressLine1());
+        assertEquals(receivedCustomer.getAddress().getAddressLine2(), persistedCustomer.getAddress().getAddressLine2());
+        assertEquals(receivedCustomer.getAddress().getId(), persistedCustomer.getAddress().getId());
+    }
+
+    @SuppressWarnings("unchecked")
+    @Test
+    public void produceExistingEntityShouldThowAnException() throws Exception {
+        final Customer customer = createDefaultCustomer();
+        transactionStrategy.execute(new JpaCallback() {
+            public Object doInJpa(EntityManager entityManager) throws PersistenceException {
+                entityManager.persist(customer);
+                entityManager.flush();
+                return null;
+            }
+        });
+        
+        assertEntitiesInDatabase(1, Customer.class.getName());
+        assertEntitiesInDatabase(1, Address.class.getName());
+        
+        customer.setName("Max Mustermann");
+        customer.getAddress().setAddressLine1("Musterstr. 1");
+        customer.getAddress().setAddressLine2("11111 Enterhausen");
+        
+        Exchange exchange = new DefaultExchange(camelContext);
+        exchange.getIn().setBody(customer);
+        Exchange returnedExchange = template.send(endpoint, exchange);
+        
+        assertTrue(returnedExchange.isFailed());
+        assertNotNull(returnedExchange.getException());
+    }
+    
+    @SuppressWarnings("unchecked")
+    @Test
+    public void consumeEntity() throws Exception {
+        final Customer customer = createDefaultCustomer();
+        transactionStrategy.execute(new JpaCallback() {
+            public Object doInJpa(EntityManager entityManager) throws PersistenceException {
+                entityManager.persist(customer);
+                entityManager.flush();
+                return null;
+            }
+        });
+        
+        final CountDownLatch latch = new CountDownLatch(1);
+        
+        consumer = endpoint.createConsumer(new Processor() {
+            public void process(Exchange e) {
+                receivedExchange = e;
+                assertNotNull(e.getIn().getHeader(JpaConstants.JPA_TEMPLATE, JpaTemplate.class));
+                latch.countDown();
+            }
+        });
+        consumer.start();
+        
+        boolean received = latch.await(50, TimeUnit.SECONDS);
+        
+        assertTrue(received);
+        assertNotNull(receivedExchange);
+        Customer receivedCustomer = receivedExchange.getIn().getBody(Customer.class);
+        assertEquals(customer.getName(), receivedCustomer.getName());
+        assertEquals(customer.getId(), receivedCustomer.getId());
+        assertEquals(customer.getAddress().getAddressLine1(), receivedCustomer.getAddress().getAddressLine1());
+        assertEquals(customer.getAddress().getAddressLine2(), receivedCustomer.getAddress().getAddressLine2());
+        assertEquals(customer.getAddress().getId(), receivedCustomer.getAddress().getId());
+        
+        assertEntitiesInDatabase(0, Customer.class.getName());
+        assertEntitiesInDatabase(0, Address.class.getName());
+    }
+    
+    @SuppressWarnings("unchecked")
+    private void assertEntitiesInDatabase(int count, String entity) {
+        List results = jpaTemplate.find("select o from " + entity + " o");
+        assertEquals(count, results.size());
+    }
+
+    private Customer createDefaultCustomer() {
+        Customer customer = new Customer();
+        customer.setName("Christian Mueller");
+        Address address = new Address();
+        address.setAddressLine1("Hahnstr. 1");
+        address.setAddressLine2("60313 Frankfurt am Main");
+        customer.setAddress(address);
+        return customer;
+    }
+}
\ No newline at end of file

Propchange: camel/trunk/components/camel-jpa/src/test/java/org/apache/camel/component/jpa/JpaUsePersistTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/components/camel-jpa/src/test/java/org/apache/camel/component/jpa/JpaUsePersistTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: camel/trunk/components/camel-jpa/src/test/java/org/apache/camel/examples/Address.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jpa/src/test/java/org/apache/camel/examples/Address.java?rev=966982&view=auto
==============================================================================
--- camel/trunk/components/camel-jpa/src/test/java/org/apache/camel/examples/Address.java (added)
+++ camel/trunk/components/camel-jpa/src/test/java/org/apache/camel/examples/Address.java Fri Jul 23 06:06:57 2010
@@ -0,0 +1,58 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.examples;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+
+/**
+ * @version $Revision: $
+ */
+@Entity
+public class Address {
+    
+    @Id
+    @GeneratedValue
+    private Long id;
+    private String addressLine1;
+    private String addressLine2;
+
+    public Long getId() {
+        return id;
+    }
+
+    public void setId(Long id) {
+        this.id = id;
+    }
+
+    public String getAddressLine1() {
+        return addressLine1;
+    }
+
+    public void setAddressLine1(String addressLine1) {
+        this.addressLine1 = addressLine1;
+    }
+    
+    public String getAddressLine2() {
+        return addressLine2;
+    }
+
+    public void setAddressLine2(String addressLine2) {
+        this.addressLine2 = addressLine2;
+    }
+}
\ No newline at end of file

Propchange: camel/trunk/components/camel-jpa/src/test/java/org/apache/camel/examples/Address.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/components/camel-jpa/src/test/java/org/apache/camel/examples/Address.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: camel/trunk/components/camel-jpa/src/test/java/org/apache/camel/examples/Customer.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jpa/src/test/java/org/apache/camel/examples/Customer.java?rev=966982&view=auto
==============================================================================
--- camel/trunk/components/camel-jpa/src/test/java/org/apache/camel/examples/Customer.java (added)
+++ camel/trunk/components/camel-jpa/src/test/java/org/apache/camel/examples/Customer.java Fri Jul 23 06:06:57 2010
@@ -0,0 +1,61 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.examples;
+
+import javax.persistence.CascadeType;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.OneToOne;
+
+/**
+ * @version $Revision: $
+ */
+@Entity
+public class Customer {
+    
+    @Id
+    @GeneratedValue
+    private Long id;
+    private String name;
+    @OneToOne(cascade = CascadeType.ALL)
+    private Address address;
+
+    public Long getId() {
+        return id;
+    }
+
+    public void setId(Long id) {
+        this.id = id;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+    
+    public Address getAddress() {
+        return address;
+    }
+
+    public void setAddress(Address address) {
+        this.address = address;
+    }
+}
\ No newline at end of file

Propchange: camel/trunk/components/camel-jpa/src/test/java/org/apache/camel/examples/Customer.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/components/camel-jpa/src/test/java/org/apache/camel/examples/Customer.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: camel/trunk/components/camel-jpa/src/test/resources/META-INF/persistence.xml
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jpa/src/test/resources/META-INF/persistence.xml?rev=966982&r1=966981&r2=966982&view=diff
==============================================================================
--- camel/trunk/components/camel-jpa/src/test/resources/META-INF/persistence.xml (original)
+++ camel/trunk/components/camel-jpa/src/test/resources/META-INF/persistence.xml Fri Jul 23 06:06:57 2010
@@ -32,6 +32,8 @@
 
     <class>org.apache.camel.examples.MultiSteps</class>
     <class>org.apache.camel.examples.SendEmail</class>
+    <class>org.apache.camel.examples.Customer</class>
+    <class>org.apache.camel.examples.Address</class>
 
     <properties>
       <property name="openjpa.ConnectionURL" value="jdbc:derby:target/derby;create=true"/>