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();
}
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");
}
};
}
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();
}
Aggregations