Search in sources :

Example 16 with StudentDomain

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

the class AbstractJpaOperationsTests method testExecuteSelectWithNativeQueryReturningEntityClass.

public void testExecuteSelectWithNativeQueryReturningEntityClass() throws ParseException {
    final JpaOperations jpaOperations = getJpaOperations(entityManager);
    String selectSqlQuery = "select * from Student where lastName = 'Last One'";
    Class<?> entityClass = StudentDomain.class;
    List<?> students = jpaOperations.getResultListForNativeQuery(selectSqlQuery, entityClass, null, 0, 0);
    Assert.assertTrue(students.size() == 1);
    StudentDomain retrievedStudent = (StudentDomain) students.iterator().next();
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
    assertEquals(formatter.parse("1980/01/01"), retrievedStudent.getDateOfBirth());
    assertEquals("First One", retrievedStudent.getFirstName());
    assertEquals(Gender.MALE, retrievedStudent.getGender());
    assertEquals("Last One", retrievedStudent.getLastName());
    assertNotNull(retrievedStudent.getLastUpdated());
}
Also used : StudentDomain(org.springframework.integration.jpa.test.entity.StudentDomain) SimpleDateFormat(java.text.SimpleDateFormat)

Example 17 with StudentDomain

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

the class AbstractJpaOperationsTests method testPersistCollectionWithNullElement.

public void testPersistCollectionWithNullElement() {
    final JpaOperations jpaOperations = getJpaOperations(entityManager);
    final List<?> studentsFromDbBeforeTest = jpaOperations.getResultListForClass(StudentDomain.class, 0, 0);
    Assert.assertEquals(3, studentsFromDbBeforeTest.size());
    final StudentDomain student1 = JpaTestUtils.getTestStudent();
    final StudentDomain student2 = null;
    final StudentDomain student3 = JpaTestUtils.getTestStudent();
    student1.setFirstName("Karl");
    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);
    Assert.assertNull(student3.getRollNumber());
    jpaOperations.persist(students, 10, false);
    Assert.assertNotNull(student1.getRollNumber());
    Assert.assertNotNull(student3.getRollNumber());
    final List<?> studentsFromDb = jpaOperations.getResultListForClass(StudentDomain.class, 0, 0);
    Assert.assertNotNull(studentsFromDb);
    Assert.assertEquals(5, studentsFromDb.size());
}
Also used : StudentDomain(org.springframework.integration.jpa.test.entity.StudentDomain) ArrayList(java.util.ArrayList)

Example 18 with StudentDomain

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

the class AbstractJpaOperationsTests method testMergeCollectionWithNullElement.

public void testMergeCollectionWithNullElement() {
    final JpaOperations jpaOperations = getJpaOperations(entityManager);
    final List<?> studentsFromDbBeforeTest = jpaOperations.getResultListForClass(StudentDomain.class, 0, 0);
    Assert.assertEquals(3, studentsFromDbBeforeTest.size());
    final StudentDomain student1 = JpaTestUtils.getTestStudent();
    final StudentDomain student2 = null;
    final StudentDomain student3 = JpaTestUtils.getTestStudent();
    student1.setFirstName("Karl");
    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);
    Assert.assertNull(student3.getRollNumber());
    Object savedStudents = jpaOperations.merge(students);
    entityManager.flush();
    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());
}
Also used : StudentDomain(org.springframework.integration.jpa.test.entity.StudentDomain) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 19 with StudentDomain

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

Example 20 with StudentDomain

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

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