Package org.springframework.orm.jpa

Examples of org.springframework.orm.jpa.JpaCallback


        endpoint = camelContext.getEndpoint(endpointUri, 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;
            }
        });
View Full Code Here


        assertEntitiesInDatabase(0, Address.class.getName());
    }
   
    @SuppressWarnings("unchecked")
    protected void save(final Customer customer) {
        transactionStrategy.execute(new JpaCallback() {
            public Object doInJpa(EntityManager entityManager) throws PersistenceException {
                entityManager.persist(customer);
                entityManager.flush();
                return null;
            }
View Full Code Here

    protected String entityName = MultiSteps.class.getName();
    protected String queryText = "select o from " + entityName + " o where o.step = 1";

    @Test
    public void testProducerInsertsIntoDatabaseThenConsumerFiresMessageExchange() throws Exception {
        transactionStrategy.execute(new JpaCallback() {
            public Object doInJpa(EntityManager entityManager) throws PersistenceException {
                // lets delete any exiting records before the test
                entityManager.createQuery("delete from " + entityName).executeUpdate();

                // now lets create a dummy entry
                MultiSteps dummy = new MultiSteps("cheese");
                dummy.setStep(4);
                entityManager.persist(dummy);
                return null;
            }
        });

        List results = jpaTemplate.find(queryText);
        assertEquals("Should have no results: " + results, 0, results.size());

        // lets produce some objects
        template.send(endpoint, new Processor() {
            public void process(Exchange exchange) {
                exchange.getIn().setBody(new MultiSteps("foo@bar.com"));
            }
        });

        // now lets assert that there is a result
        results = jpaTemplate.find(queryText);
        assertEquals("Should have results: " + results, 1, results.size());
        MultiSteps mail = (MultiSteps)results.get(0);
        assertEquals("address property", "foo@bar.com", mail.getAddress());

        // now lets create a consumer to consume it
        consumer = endpoint.createConsumer(new Processor() {
            public void process(Exchange e) {
                LOG.info("Received exchange: " + e.getIn());
                receivedExchange = e;
                latch.countDown();
            }
        });
        consumer.start();

        boolean received = latch.await(50, TimeUnit.SECONDS);
        assertTrue("Did not receive the message!", received);

        assertReceivedResult(receivedExchange);

        // lets now test that the database is updated
        // we need to sleep as we will be invoked from inside the transaction!
        Thread.sleep(1000);

        transactionStrategy.execute(new JpaCallback() {
            @SuppressWarnings("unchecked")
            public Object doInJpa(EntityManager entityManager) throws PersistenceException {

                // now lets assert that there are still 2 entities left
                List<MultiSteps> rows = entityManager.createQuery("select x from MultiSteps x").getResultList();
View Full Code Here

    @Test
    public void produceExistingEntity() throws Exception {
        setUp("jpa://" + Customer.class.getName() + "?usePersist=false");
       
        final Customer customer = createDefaultCustomer();
        transactionStrategy.execute(new JpaCallback() {
            public Object doInJpa(EntityManager entityManager) throws PersistenceException {
                entityManager.persist(customer);
                entityManager.flush();
                return null;
            }
View Full Code Here

    public void process(final Exchange exchange) {
        exchange.getIn().setHeader(JpaConstants.JPA_TEMPLATE, endpoint.getTemplate());
        final Object values = expression.evaluate(exchange, Object.class);
        if (values != null) {
            template.execute(new JpaCallback() {
                @SuppressWarnings("unchecked")
                public Object doInJpa(EntityManager entityManager) throws PersistenceException {
                    if (values.getClass().isArray()) {
                        Object[] array = (Object[]) values;
                        for (int index = 0; index < array.length; index++) {
View Full Code Here

   */
  @SuppressWarnings({ "unchecked", "rawtypes" })
  public int delete(final Integer id)
  {
    int rowcount = 0;
    rowcount = (Integer) getJpaTemplate().execute(new JpaCallback()
    {
      public Object doInJpa(EntityManager em) throws PersistenceException
      {
        Query query = em.createQuery("DELETE FROM " + entityClassName + " WHERE id =" + id);
        return query.executeUpdate();
View Full Code Here

   */
  @SuppressWarnings({ "unchecked", "rawtypes" })
  public int delete(final Long id)
  {
    int rowcount = 0;
    rowcount = (Integer) getJpaTemplate().execute(new JpaCallback()
    {
      public Object doInJpa(EntityManager em) throws PersistenceException
      {
        Query query = em.createQuery("DELETE FROM " + entityClassName + " WHERE id =" + id);
        return query.executeUpdate();
View Full Code Here

   */
  @SuppressWarnings({ "unchecked", "rawtypes" })
  public int delete(final String ids)
  {
    int rowcount = 0;
    rowcount = (Integer) getJpaTemplate().execute(new JpaCallback()
    {
      public Object doInJpa(EntityManager em) throws PersistenceException
      {
        Query query = em.createQuery("DELETE FROM " + entityClassName + " WHERE id in(" + ids + ")");
        return query.executeUpdate();
View Full Code Here

   */
  @SuppressWarnings({ "unchecked", "rawtypes" })
  public int deleteByModel(final QueryModel model)
  {
    int rowcount = 0;
    rowcount = (Integer) getJpaTemplate().execute(new JpaCallback()
    {
      public Object doInJpa(EntityManager em) throws PersistenceException
      {
        Query query = null;
        if (null != model)
View Full Code Here

   */
  @SuppressWarnings({ "unchecked", "rawtypes" })
  public int deleteByModel(final Class<T> cls, final QueryModel model)
  {
    int rowcount = 0;
    rowcount = (Integer) getJpaTemplate().execute(new JpaCallback()
    {
      public Object doInJpa(EntityManager em) throws PersistenceException
      {
        Query query = null;
        if (null != model)
View Full Code Here

TOP

Related Classes of org.springframework.orm.jpa.JpaCallback

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.