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