Search in sources :

Example 1 with ParameterSource

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

the class AbstractJpaOperationsTests method testExecuteUpdateWithNamedQuery.

public void testExecuteUpdateWithNamedQuery() {
    final JpaOperations jpaOperations = getJpaOperations(entityManager);
    final StudentDomain student = JpaTestUtils.getTestStudent();
    ParameterSourceFactory requestParameterSourceFactory = new ExpressionEvaluatingParameterSourceFactory(mock(BeanFactory.class));
    ParameterSource source = requestParameterSourceFactory.createParameterSource(student);
    int updatedRecords = jpaOperations.executeUpdateWithNamedQuery("updateStudent", 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 2 with ParameterSource

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

the class AbstractJpaOperationsTests method testExecuteUpdateWithNativeQuery.

public void testExecuteUpdateWithNativeQuery() {
    final JpaOperations jpaOperations = getJpaOperations(entityManager);
    final StudentDomain student = JpaTestUtils.getTestStudent();
    ExpressionEvaluatingParameterSourceFactory requestParameterSourceFactory = new ExpressionEvaluatingParameterSourceFactory(mock(BeanFactory.class));
    ParameterSource source = requestParameterSourceFactory.createParameterSource(student);
    int updatedRecords = jpaOperations.executeUpdateWithNativeQuery("update Student " + "set lastName = :lastName, lastUpdated = :lastUpdated " + "where 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) ExpressionEvaluatingParameterSourceFactory(org.springframework.integration.jpa.support.parametersource.ExpressionEvaluatingParameterSourceFactory)

Example 3 with ParameterSource

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

the class JpaInboundChannelAdapterParserTests method testJpaInboundChannelAdapterParser.

@Test
public void testJpaInboundChannelAdapterParser() throws Exception {
    AbstractMessageChannel outputChannel = TestUtils.getPropertyValue(this.jpaInboundChannelAdapter1, "outputChannel", AbstractMessageChannel.class);
    assertEquals("out", outputChannel.getComponentName());
    JpaExecutor jpaExecutor = TestUtils.getPropertyValue(this.jpaInboundChannelAdapter1, "source.jpaExecutor", JpaExecutor.class);
    assertNotNull(jpaExecutor);
    Class<?> entityClass = TestUtils.getPropertyValue(jpaExecutor, "entityClass", Class.class);
    assertEquals("org.springframework.integration.jpa.test.entity.StudentDomain", entityClass.getName());
    JpaOperations jpaOperations = TestUtils.getPropertyValue(jpaExecutor, "jpaOperations", JpaOperations.class);
    assertNotNull(jpaOperations);
    assertTrue(TestUtils.getPropertyValue(jpaExecutor, "expectSingleResult", Boolean.class));
    ParameterSource parameterSource = this.context.getBean(ParameterSource.class);
    assertSame(parameterSource, TestUtils.getPropertyValue(jpaExecutor, "parameterSource"));
}
Also used : AbstractMessageChannel(org.springframework.integration.channel.AbstractMessageChannel) JpaExecutor(org.springframework.integration.jpa.core.JpaExecutor) ParameterSource(org.springframework.integration.jpa.support.parametersource.ParameterSource) JpaOperations(org.springframework.integration.jpa.core.JpaOperations) Test(org.junit.Test)

Example 4 with ParameterSource

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

the class JpaExecutor method executeOutboundJpaOperation.

/**
 * Execute the actual Jpa Operation. Call this method, if you need access to
 * process return values. This methods return a Map that contains either
 * the number of affected entities or the affected entity itself.
 *<p>Keep in mind that the number of entities effected by the operation may
 * not necessarily correlate with the number of rows effected in the database.
 * @param message The message.
 * @return Either the number of affected entities when using a JPQL query.
 * When using a merge/persist the updated/inserted itself is returned.
 */
public Object executeOutboundJpaOperation(final Message<?> message) {
    final Object result;
    ParameterSource parameterSource = null;
    if (this.jpaQuery != null || this.nativeQuery != null || this.namedQuery != null) {
        parameterSource = this.determineParameterSource(message);
    }
    if (this.jpaQuery != null) {
        result = this.jpaOperations.executeUpdate(this.jpaQuery, parameterSource);
    } else if (this.nativeQuery != null) {
        result = this.jpaOperations.executeUpdateWithNativeQuery(this.nativeQuery, parameterSource);
    } else if (this.namedQuery != null) {
        result = this.jpaOperations.executeUpdateWithNamedQuery(this.namedQuery, parameterSource);
    } else {
        if (PersistMode.PERSIST.equals(this.persistMode)) {
            this.jpaOperations.persist(message.getPayload(), this.flushSize, this.clearOnFlush);
            result = message.getPayload();
        } else if (PersistMode.MERGE.equals(this.persistMode)) {
            result = this.jpaOperations.merge(message.getPayload(), this.flushSize, this.clearOnFlush);
        } else if (PersistMode.DELETE.equals(this.persistMode)) {
            this.jpaOperations.delete(message.getPayload());
            if (this.flush) {
                this.jpaOperations.flush();
            }
            result = message.getPayload();
        } else {
            throw new IllegalStateException(String.format("Unsupported PersistMode: '%s'", this.persistMode.name()));
        }
    }
    return result;
}
Also used : ParameterSource(org.springframework.integration.jpa.support.parametersource.ParameterSource)

Example 5 with ParameterSource

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

the class AbstractJpaOperationsTests method testExecuteUpdateWithNativeNamedQuery.

public void testExecuteUpdateWithNativeNamedQuery() {
    final JpaOperations jpaOperations = getJpaOperations(entityManager);
    final StudentDomain student = JpaTestUtils.getTestStudent();
    ParameterSourceFactory requestParameterSourceFactory = new ExpressionEvaluatingParameterSourceFactory(mock(BeanFactory.class));
    ParameterSource source = requestParameterSourceFactory.createParameterSource(student);
    int updatedRecords = jpaOperations.executeUpdateWithNamedQuery("updateStudentNativeQuery", 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)

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