Search in sources :

Example 1 with JpaEndpoint

use of org.apache.camel.component.jpa.JpaEndpoint in project camel by apache.

the class JpaProducerPassingEntityManagerTest method testRouteJpa.

@Test
public void testRouteJpa() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:result");
    context.startRoute("foo");
    JpaEndpoint jpa = context.getEndpoint("jpa://" + SendEmail.class.getName(), JpaEndpoint.class);
    EntityManagerFactory emf = jpa.getEntityManagerFactory();
    // The entity instance is different if it is retrieved from different EntityManager instance
    EntityManager entityManager = emf.createEntityManager();
    template.sendBody("direct:start", new SendEmail("foo@beer.org"));
    Exchange exchange = mock.getReceivedExchanges().get(0);
    SendEmail persistedEntity = exchange.getIn().getBody(SendEmail.class);
    SendEmail emfindEntity = entityManager.find(SendEmail.class, persistedEntity.getId());
    assertNotSame(emfindEntity, persistedEntity);
    entityManager.close();
    mock.reset();
    // The same EntityManager returns same entity instance from its 1st level cache
    entityManager = emf.createEntityManager();
    template.sendBodyAndHeader("direct:start", new SendEmail("bar@beer.org"), JpaConstants.ENTITYMANAGER, entityManager);
    exchange = mock.getReceivedExchanges().get(0);
    persistedEntity = exchange.getIn().getBody(SendEmail.class);
    emfindEntity = entityManager.find(SendEmail.class, persistedEntity.getId());
    assertSame(emfindEntity, persistedEntity);
    entityManager.close();
}
Also used : Exchange(org.apache.camel.Exchange) EntityManager(javax.persistence.EntityManager) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) EntityManagerFactory(javax.persistence.EntityManagerFactory) JpaEndpoint(org.apache.camel.component.jpa.JpaEndpoint) SendEmail(org.apache.camel.examples.SendEmail) Test(org.junit.Test)

Example 2 with JpaEndpoint

use of org.apache.camel.component.jpa.JpaEndpoint in project camel by apache.

the class JpaRouteEndpointTest method createRouteBuilder.

@Override
protected RouteBuilder createRouteBuilder() {
    return new SpringRouteBuilder() {

        public void configure() throws Exception {
            JpaEndpoint jpa = new JpaEndpoint();
            jpa.setCamelContext(context);
            jpa.setEntityType(SendEmail.class);
            jpa.setEntityManagerFactory(context.getRegistry().lookupByNameAndType("entityManagerFactory", EntityManagerFactory.class));
            from("direct:start").to(jpa).to("mock:result");
        }
    };
}
Also used : SpringRouteBuilder(org.apache.camel.spring.SpringRouteBuilder) EntityManagerFactory(javax.persistence.EntityManagerFactory) JpaEndpoint(org.apache.camel.component.jpa.JpaEndpoint)

Example 3 with JpaEndpoint

use of org.apache.camel.component.jpa.JpaEndpoint in project camel by apache.

the class JpaProducerRemoveTest method testRouteJpa.

@Test
public void testRouteJpa() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:result");
    context.startRoute("foo");
    context.startRoute("foo1");
    JpaEndpoint jpa = context.getEndpoint("jpa://" + SendEmail.class.getName(), JpaEndpoint.class);
    EntityManagerFactory emf = jpa.getEntityManagerFactory();
    EntityManager entityManager = emf.createEntityManager();
    template.sendBody("direct:start", new SendEmail("foo@beer.org"));
    Exchange exchange = mock.getReceivedExchanges().get(0);
    SendEmail persistedEntity = exchange.getIn().getBody(SendEmail.class);
    SendEmail emfindEntity = entityManager.find(SendEmail.class, persistedEntity.getId());
    assertNotSame(emfindEntity, persistedEntity);
    entityManager.close();
    mock.reset();
    entityManager = emf.createEntityManager();
    template.sendBody("direct:remove", persistedEntity);
    exchange = mock.getReceivedExchanges().get(0);
    persistedEntity = exchange.getIn().getBody(SendEmail.class);
    emfindEntity = entityManager.find(SendEmail.class, persistedEntity.getId());
    assertNull(emfindEntity);
    entityManager.close();
}
Also used : Exchange(org.apache.camel.Exchange) EntityManager(javax.persistence.EntityManager) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) EntityManagerFactory(javax.persistence.EntityManagerFactory) JpaEndpoint(org.apache.camel.component.jpa.JpaEndpoint) SendEmail(org.apache.camel.examples.SendEmail) Test(org.junit.Test)

Aggregations

EntityManagerFactory (javax.persistence.EntityManagerFactory)3 JpaEndpoint (org.apache.camel.component.jpa.JpaEndpoint)3 EntityManager (javax.persistence.EntityManager)2 Exchange (org.apache.camel.Exchange)2 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)2 SendEmail (org.apache.camel.examples.SendEmail)2 Test (org.junit.Test)2 SpringRouteBuilder (org.apache.camel.spring.SpringRouteBuilder)1