Search in sources :

Example 6 with ParameterSource

use of org.springframework.integration.jpa.support.parametersource.ParameterSource in project spring-integration by spring-projects.

the class AbstractJpaOperationsTests method testExecuteUpdate.

public void testExecuteUpdate() {
    final JpaOperations jpaOperations = getJpaOperations(entityManager);
    final StudentDomain student = JpaTestUtils.getTestStudent();
    List<?> students = jpaOperations.getResultListForClass(StudentDomain.class, 0, 0);
    Assert.assertTrue(students.size() == 3);
    ParameterSourceFactory requestParameterSourceFactory = new ExpressionEvaluatingParameterSourceFactory(mock(BeanFactory.class));
    ParameterSource source = requestParameterSourceFactory.createParameterSource(student);
    int updatedRecords = jpaOperations.executeUpdate("update Student s " + "set s.lastName = :lastName, s.lastUpdated = :lastUpdated " + "where s.rollNumber in (select max(a.rollNumber) from Student a)", source);
    entityManager.flush();
    Assert.assertTrue(1 == updatedRecords);
    Assert.assertNull(student.getRollNumber());
}
Also used : StudentDomain(org.springframework.integration.jpa.test.entity.StudentDomain) ParameterSource(org.springframework.integration.jpa.support.parametersource.ParameterSource) BeanFactory(org.springframework.beans.factory.BeanFactory) ParameterSourceFactory(org.springframework.integration.jpa.support.parametersource.ParameterSourceFactory) ExpressionEvaluatingParameterSourceFactory(org.springframework.integration.jpa.support.parametersource.ExpressionEvaluatingParameterSourceFactory) ExpressionEvaluatingParameterSourceFactory(org.springframework.integration.jpa.support.parametersource.ExpressionEvaluatingParameterSourceFactory)

Example 7 with ParameterSource

use of org.springframework.integration.jpa.support.parametersource.ParameterSource in project spring-integration by spring-projects.

the class JpaExecutor method poll.

/**
 * Execute a (typically retrieving) JPA operation. The <i>requestMessage</i>
 * can be used to provide additional query parameters using
 * {@link JpaExecutor#parameterSourceFactory}. If the
 * <i>requestMessage</i> parameter is null then
 * {@link JpaExecutor#parameterSource} is being used for providing query parameters.
 * @param requestMessage May be null.
 * @return The payload object, which may be null.
 */
@SuppressWarnings("unchecked")
public Object poll(final Message<?> requestMessage) {
    final Object payload;
    if (this.idExpression != null) {
        Object id = this.idExpression.getValue(this.evaluationContext, requestMessage);
        Class<?> entityClass = this.entityClass;
        if (entityClass == null) {
            entityClass = requestMessage.getPayload().getClass();
        }
        payload = this.jpaOperations.find(entityClass, id);
    } else {
        final List<?> result;
        int maxNumberOfResults = this.evaluateExpressionForNumericResult(requestMessage, this.maxResultsExpression);
        if (requestMessage == null) {
            result = this.doPoll(this.parameterSource, 0, maxNumberOfResults);
        } else {
            int firstResult = 0;
            if (this.firstResultExpression != null) {
                firstResult = this.getFirstResult(requestMessage);
            }
            ParameterSource parameterSource = this.determineParameterSource(requestMessage);
            result = this.doPoll(parameterSource, firstResult, maxNumberOfResults);
        }
        if (result.isEmpty()) {
            payload = null;
        } else {
            if (this.expectSingleResult) {
                if (result.size() == 1) {
                    payload = result.iterator().next();
                } else {
                    throw new MessagingException(requestMessage, "The Jpa operation returned more than 1 result object but expectSingleResult was 'true'.");
                }
            } else {
                payload = result;
            }
        }
    }
    if (payload != null && this.deleteAfterPoll) {
        if (payload instanceof Iterable) {
            if (this.deleteInBatch) {
                this.jpaOperations.deleteInBatch((Iterable<Object>) payload);
            } else {
                for (Object entity : (Iterable<?>) payload) {
                    this.jpaOperations.delete(entity);
                }
            }
        } else {
            this.jpaOperations.delete(payload);
        }
        if (this.flush) {
            this.jpaOperations.flush();
        }
    }
    return payload;
}
Also used : ParameterSource(org.springframework.integration.jpa.support.parametersource.ParameterSource) MessagingException(org.springframework.messaging.MessagingException)

Aggregations

ParameterSource (org.springframework.integration.jpa.support.parametersource.ParameterSource)7 BeanFactory (org.springframework.beans.factory.BeanFactory)4 ExpressionEvaluatingParameterSourceFactory (org.springframework.integration.jpa.support.parametersource.ExpressionEvaluatingParameterSourceFactory)4 StudentDomain (org.springframework.integration.jpa.test.entity.StudentDomain)4 ParameterSourceFactory (org.springframework.integration.jpa.support.parametersource.ParameterSourceFactory)3 Test (org.junit.Test)1 AbstractMessageChannel (org.springframework.integration.channel.AbstractMessageChannel)1 JpaExecutor (org.springframework.integration.jpa.core.JpaExecutor)1 JpaOperations (org.springframework.integration.jpa.core.JpaOperations)1 MessagingException (org.springframework.messaging.MessagingException)1