Search in sources :

Example 1 with StudentDomain

use of org.springframework.integration.jpa.test.entity.StudentDomain in project spring-integration by spring-projects.

the class JpaOutboundChannelAdapterTests method saveEntityWithMergeWithoutSpecifyingEntityClass.

@Test
public void saveEntityWithMergeWithoutSpecifyingEntityClass() throws InterruptedException {
    List<?> results1 = this.jdbcTemplate.queryForList("Select * from Student");
    Assert.assertNotNull(results1);
    Assert.assertTrue(results1.size() == 3);
    JpaExecutor jpaExecutor = new JpaExecutor(entityManager);
    jpaExecutor.setBeanFactory(mock(BeanFactory.class));
    jpaExecutor.afterPropertiesSet();
    final JpaOutboundGateway jpaOutboundChannelAdapter = new JpaOutboundGateway(jpaExecutor);
    jpaOutboundChannelAdapter.setProducesReply(false);
    StudentDomain testStudent = JpaTestUtils.getTestStudent();
    final Message<StudentDomain> message = MessageBuilder.withPayload(testStudent).build();
    TransactionTemplate transactionTemplate = new TransactionTemplate(this.transactionManager);
    transactionTemplate.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            jpaOutboundChannelAdapter.handleMessage(message);
        }
    });
    List<?> results2 = this.jdbcTemplate.queryForList("Select * from Student");
    Assert.assertNotNull(results2);
    Assert.assertTrue(results2.size() == 4);
    Assert.assertNull(testStudent.getRollNumber());
}
Also used : StudentDomain(org.springframework.integration.jpa.test.entity.StudentDomain) JpaExecutor(org.springframework.integration.jpa.core.JpaExecutor) BeanFactory(org.springframework.beans.factory.BeanFactory) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.Test)

Example 2 with StudentDomain

use of org.springframework.integration.jpa.test.entity.StudentDomain in project spring-integration by spring-projects.

the class JpaOutboundGatewayIntegrationTests method testFindByPayloadType.

@Test
public void testFindByPayloadType() throws Exception {
    this.handler = message -> {
        assertThat(message.getPayload(), Matchers.instanceOf(StudentDomain.class));
        StudentDomain student = (StudentDomain) message.getPayload();
        assertEquals("First Two", student.getFirstName());
    };
    this.responseChannel.subscribe(this.handler);
    StudentDomain payload = new StudentDomain();
    payload.setRollNumber(1002L);
    Message<StudentDomain> message = MessageBuilder.withPayload(payload).build();
    this.findByPayloadTypeChannel.send(message);
}
Also used : StudentDomain(org.springframework.integration.jpa.test.entity.StudentDomain) Test(org.junit.Test)

Example 3 with StudentDomain

use of org.springframework.integration.jpa.test.entity.StudentDomain 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 4 with StudentDomain

use of org.springframework.integration.jpa.test.entity.StudentDomain in project spring-integration by spring-projects.

the class AbstractJpaOperationsTests method testMergeCollection.

public void testMergeCollection() {
    final JpaOperations jpaOperations = getJpaOperations(entityManager);
    final StudentDomain student1 = JpaTestUtils.getTestStudent();
    final StudentDomain student2 = JpaTestUtils.getTestStudent();
    final StudentDomain student3 = JpaTestUtils.getTestStudent();
    student1.setFirstName("Karl");
    student2.setFirstName("Otto");
    student3.setFirstName("Wilhelm");
    List<StudentDomain> students = new ArrayList<StudentDomain>(3);
    students.add(student1);
    students.add(student2);
    students.add(student3);
    Assert.assertNull(student1.getRollNumber());
    Assert.assertNull(student2.getRollNumber());
    Assert.assertNull(student3.getRollNumber());
    Object savedStudents = jpaOperations.merge(students, 10, true);
    Assert.assertTrue(savedStudents instanceof List<?>);
    @SuppressWarnings("unchecked") List<StudentDomain> savedStudentCollection = (List<StudentDomain>) savedStudents;
    Assert.assertNotNull(savedStudentCollection.get(0).getRollNumber());
    Assert.assertNotNull(savedStudentCollection.get(1).getRollNumber());
    Assert.assertNotNull(savedStudentCollection.get(2).getRollNumber());
}
Also used : StudentDomain(org.springframework.integration.jpa.test.entity.StudentDomain) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 5 with StudentDomain

use of org.springframework.integration.jpa.test.entity.StudentDomain 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)

Aggregations

StudentDomain (org.springframework.integration.jpa.test.entity.StudentDomain)35 Test (org.junit.Test)21 BeanFactory (org.springframework.beans.factory.BeanFactory)7 ArrayList (java.util.ArrayList)5 TransactionStatus (org.springframework.transaction.TransactionStatus)5 Transactional (org.springframework.transaction.annotation.Transactional)5 JpaExecutor (org.springframework.integration.jpa.core.JpaExecutor)4 ExpressionEvaluatingParameterSourceFactory (org.springframework.integration.jpa.support.parametersource.ExpressionEvaluatingParameterSourceFactory)4 ParameterSource (org.springframework.integration.jpa.support.parametersource.ParameterSource)4 Calendar (java.util.Calendar)3 Date (java.util.Date)3 ParameterSourceFactory (org.springframework.integration.jpa.support.parametersource.ParameterSourceFactory)3 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)3 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)3 List (java.util.List)2 JdbcTemplate (org.springframework.jdbc.core.JdbcTemplate)2 DefaultTransactionDefinition (org.springframework.transaction.support.DefaultTransactionDefinition)2 SimpleDateFormat (java.text.SimpleDateFormat)1 Collection (java.util.Collection)1 Map (java.util.Map)1