Upgrade to Hibernate 6.2.8.Final
It looks like in the latest Hibernate there is some fix in the persistent context where provided entity is updated by the values from DB. * Fix failing tests * Migrate affected test classes to JUnit 5 **Cherry-pick to `6.1.x`** # Conflicts: # build.gradle
This commit is contained in:
@@ -67,7 +67,7 @@ ext {
|
||||
groovyVersion = '4.0.15'
|
||||
hamcrestVersion = '2.2'
|
||||
hazelcastVersion = '5.2.4'
|
||||
hibernateVersion = '6.2.6.Final'
|
||||
hibernateVersion = '6.2.8.Final'
|
||||
hsqldbVersion = '2.7.2'
|
||||
h2Version = '2.2.224'
|
||||
jacksonVersion = '2.15.2'
|
||||
|
||||
@@ -1,534 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.jpa.core;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import jakarta.persistence.EntityManager;
|
||||
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.integration.jpa.support.parametersource.ExpressionEvaluatingParameterSourceFactory;
|
||||
import org.springframework.integration.jpa.support.parametersource.ParameterSource;
|
||||
import org.springframework.integration.jpa.support.parametersource.ParameterSourceFactory;
|
||||
import org.springframework.integration.jpa.test.JpaTestUtils;
|
||||
import org.springframework.integration.jpa.test.entity.Gender;
|
||||
import org.springframework.integration.jpa.test.entity.StudentDomain;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
import org.springframework.transaction.TransactionDefinition;
|
||||
import org.springframework.transaction.TransactionStatus;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.transaction.support.DefaultTransactionDefinition;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.fail;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* @author Gunnar Hillert
|
||||
* @author Gary Russell
|
||||
* @since 2.2
|
||||
*
|
||||
*/
|
||||
@Transactional
|
||||
public class AbstractJpaOperationsTests {
|
||||
|
||||
@Autowired
|
||||
protected PlatformTransactionManager transactionManager;
|
||||
|
||||
@Autowired
|
||||
protected EntityManager entityManager;
|
||||
|
||||
public void testGetAllStudents() {
|
||||
|
||||
final JpaOperations jpaOperations = getJpaOperations(entityManager);
|
||||
|
||||
final List<?> students = jpaOperations.getResultListForClass(StudentDomain.class, 0, 0);
|
||||
assertThat(students.size() == 3).isTrue();
|
||||
|
||||
}
|
||||
|
||||
public void testGetAllStudentsWithMaxResults() {
|
||||
|
||||
final JpaOperations jpaOperations = getJpaOperations(entityManager);
|
||||
|
||||
final List<?> students = jpaOperations.getResultListForClass(StudentDomain.class, 0, 2);
|
||||
assertThat(students.size() == 2)
|
||||
.as(String.format("Was expecting 2 Students to be returned but got '%s'.", students.size())).isTrue();
|
||||
|
||||
}
|
||||
|
||||
public void testExecuteUpdate() {
|
||||
|
||||
final JpaOperations jpaOperations = getJpaOperations(entityManager);
|
||||
|
||||
final StudentDomain student = JpaTestUtils.getTestStudent();
|
||||
|
||||
List<?> students = jpaOperations.getResultListForClass(StudentDomain.class, 0, 0);
|
||||
assertThat(students.size() == 3).isTrue();
|
||||
|
||||
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();
|
||||
|
||||
assertThat(1 == updatedRecords).isTrue();
|
||||
assertThat(student.getRollNumber()).isNull();
|
||||
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
assertThat(1 == updatedRecords).isTrue();
|
||||
assertThat(student.getRollNumber()).isNull();
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
assertThat(1 == updatedRecords).isTrue();
|
||||
assertThat(student.getRollNumber()).isNull();
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
assertThat(students.size() == 1).isTrue();
|
||||
|
||||
StudentDomain retrievedStudent = (StudentDomain) students.iterator().next();
|
||||
|
||||
SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
|
||||
|
||||
assertThat(retrievedStudent.getDateOfBirth()).isEqualTo(formatter.parse("1980/01/01"));
|
||||
assertThat(retrievedStudent.getFirstName()).isEqualTo("First One");
|
||||
assertThat(retrievedStudent.getGender()).isEqualTo(Gender.MALE);
|
||||
assertThat(retrievedStudent.getLastName()).isEqualTo("Last One");
|
||||
assertThat(retrievedStudent.getLastUpdated()).isNotNull();
|
||||
|
||||
}
|
||||
|
||||
public void testExecuteSelectWithNativeQuery() throws ParseException {
|
||||
final JpaOperations jpaOperations = getJpaOperations(entityManager);
|
||||
|
||||
String selectSqlQuery = "select rollNumber, firstName, lastName, gender, dateOfBirth, lastUpdated " +
|
||||
"from Student where lastName = 'Last One'";
|
||||
|
||||
List<?> students = jpaOperations.getResultListForNativeQuery(selectSqlQuery, null, null, 0, 0);
|
||||
|
||||
assertThat(students.size() == 1).isTrue();
|
||||
|
||||
Object[] retrievedStudent = (Object[]) students.iterator().next();
|
||||
|
||||
SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
|
||||
|
||||
assertThat(retrievedStudent[0]).isNotNull();
|
||||
assertThat(retrievedStudent[1]).isEqualTo("First One");
|
||||
assertThat(retrievedStudent[2]).isEqualTo("Last One");
|
||||
assertThat(retrievedStudent[3]).isEqualTo("M");
|
||||
assertThat(retrievedStudent[4]).isEqualTo(formatter.parse("1980/01/01"));
|
||||
assertThat(retrievedStudent[5]).isNotNull();
|
||||
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
assertThat(1 == updatedRecords).isTrue();
|
||||
assertThat(student.getRollNumber()).isNull();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link org.springframework.integration.jpa.core.DefaultJpaOperations#merge(java.lang.Object)}.
|
||||
*/
|
||||
public void testMerge() {
|
||||
final JpaOperations jpaOperations = getJpaOperations(entityManager);
|
||||
|
||||
final StudentDomain student = JpaTestUtils.getTestStudent();
|
||||
|
||||
assertThat(student.getRollNumber()).isNull();
|
||||
final StudentDomain savedStudent = (StudentDomain) jpaOperations.merge(student);
|
||||
entityManager.flush();
|
||||
assertThat(student.getRollNumber()).isNull();
|
||||
assertThat(savedStudent).isNotNull();
|
||||
assertThat(savedStudent.getRollNumber()).isNotNull();
|
||||
|
||||
assertThat(student != savedStudent).isTrue();
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
assertThat(student1.getRollNumber()).isNull();
|
||||
assertThat(student2.getRollNumber()).isNull();
|
||||
assertThat(student3.getRollNumber()).isNull();
|
||||
|
||||
Object savedStudents = jpaOperations.merge(students, 10, true);
|
||||
|
||||
assertThat(savedStudents instanceof List<?>).isTrue();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
List<StudentDomain> savedStudentCollection = (List<StudentDomain>) savedStudents;
|
||||
|
||||
assertThat(savedStudentCollection.get(0).getRollNumber()).isNotNull();
|
||||
assertThat(savedStudentCollection.get(1).getRollNumber()).isNotNull();
|
||||
assertThat(savedStudentCollection.get(2).getRollNumber()).isNotNull();
|
||||
}
|
||||
|
||||
public void testMergeNullCollection() {
|
||||
final JpaOperations jpaOperations = getJpaOperations(entityManager);
|
||||
|
||||
try {
|
||||
jpaOperations.merge(null);
|
||||
}
|
||||
catch (IllegalArgumentException e) {
|
||||
assertThat(e.getMessage()).isEqualTo("The object to merge must not be null.");
|
||||
return;
|
||||
}
|
||||
|
||||
fail("Expected an IllegalArgumentException to be thrown.");
|
||||
|
||||
}
|
||||
|
||||
public void testMergeCollectionWithNullElement() {
|
||||
final JpaOperations jpaOperations = getJpaOperations(entityManager);
|
||||
|
||||
final List<?> studentsFromDbBeforeTest = jpaOperations.getResultListForClass(StudentDomain.class, 0, 0);
|
||||
|
||||
assertThat(studentsFromDbBeforeTest.size()).isEqualTo(3);
|
||||
|
||||
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);
|
||||
|
||||
assertThat(student1.getRollNumber()).isNull();
|
||||
assertThat(student2).isNull();
|
||||
assertThat(student3.getRollNumber()).isNull();
|
||||
|
||||
Object savedStudents = jpaOperations.merge(students);
|
||||
entityManager.flush();
|
||||
|
||||
assertThat(savedStudents instanceof List<?>).isTrue();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
List<StudentDomain> savedStudentCollection = (List<StudentDomain>) savedStudents;
|
||||
|
||||
assertThat(savedStudentCollection.get(0).getRollNumber()).isNotNull();
|
||||
assertThat(savedStudentCollection.get(1).getRollNumber()).isNotNull();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link org.springframework.integration.jpa.core.DefaultJpaOperations#persist(java.lang.Object)}.
|
||||
*/
|
||||
public void testPersist() {
|
||||
final JpaOperations jpaOperations = getJpaOperations(entityManager);
|
||||
|
||||
final StudentDomain student = JpaTestUtils.getTestStudent();
|
||||
|
||||
assertThat(student.getRollNumber()).isNull();
|
||||
jpaOperations.persist(student, 1, false);
|
||||
assertThat(student.getRollNumber()).isNotNull();
|
||||
|
||||
assertThat(entityManager.contains(student)).isTrue();
|
||||
}
|
||||
|
||||
public void testPersistCollection() {
|
||||
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);
|
||||
|
||||
assertThat(student1.getRollNumber()).isNull();
|
||||
assertThat(student2.getRollNumber()).isNull();
|
||||
assertThat(student3.getRollNumber()).isNull();
|
||||
|
||||
jpaOperations.persist(students, 1, true);
|
||||
assertThat(student1.getRollNumber()).isNotNull();
|
||||
assertThat(student2.getRollNumber()).isNotNull();
|
||||
assertThat(student3.getRollNumber()).isNotNull();
|
||||
|
||||
assertThat(entityManager.contains(student1)).isFalse();
|
||||
assertThat(entityManager.contains(student2)).isFalse();
|
||||
assertThat(entityManager.contains(student3)).isFalse();
|
||||
}
|
||||
|
||||
public void testPersistNullCollection() {
|
||||
final JpaOperations jpaOperations = getJpaOperations(entityManager);
|
||||
|
||||
try {
|
||||
jpaOperations.persist(null);
|
||||
}
|
||||
catch (IllegalArgumentException e) {
|
||||
assertThat(e.getMessage()).isEqualTo("The object to persist must not be null.");
|
||||
return;
|
||||
}
|
||||
|
||||
fail("Expected an IllegalArgumentException to be thrown.");
|
||||
|
||||
}
|
||||
|
||||
public void testPersistCollectionWithNullElement() {
|
||||
final JpaOperations jpaOperations = getJpaOperations(entityManager);
|
||||
|
||||
final List<?> studentsFromDbBeforeTest = jpaOperations.getResultListForClass(StudentDomain.class, 0, 0);
|
||||
|
||||
assertThat(studentsFromDbBeforeTest.size()).isEqualTo(3);
|
||||
|
||||
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);
|
||||
|
||||
assertThat(student1.getRollNumber()).isNull();
|
||||
assertThat(student2).isNull();
|
||||
assertThat(student3.getRollNumber()).isNull();
|
||||
|
||||
jpaOperations.persist(students, 10, false);
|
||||
|
||||
assertThat(student1.getRollNumber()).isNotNull();
|
||||
assertThat(student3.getRollNumber()).isNotNull();
|
||||
|
||||
final List<?> studentsFromDb = jpaOperations.getResultListForClass(StudentDomain.class, 0, 0);
|
||||
|
||||
assertThat(studentsFromDb).isNotNull();
|
||||
assertThat(studentsFromDb.size()).isEqualTo(5);
|
||||
}
|
||||
|
||||
public void testDeleteInBatch() {
|
||||
final JpaOperations jpaOperations = getJpaOperations(entityManager);
|
||||
|
||||
final List<?> students = jpaOperations.getResultListForClass(StudentDomain.class, 0, 0);
|
||||
|
||||
assertThat(students).isNotNull();
|
||||
|
||||
|
||||
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
|
||||
// explicitly setting the transaction name is something that can only be done programmatically
|
||||
def.setName("SomeOtherTxName");
|
||||
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
|
||||
|
||||
TransactionStatus status = transactionManager.getTransaction(def);
|
||||
|
||||
jpaOperations.deleteInBatch(students);
|
||||
|
||||
entityManager.flush();
|
||||
|
||||
transactionManager.commit(status);
|
||||
|
||||
final List<?> studentsFromDb = jpaOperations.getResultListForClass(StudentDomain.class, 0, 0);
|
||||
|
||||
assertThat(studentsFromDb).isNotNull();
|
||||
assertThat(studentsFromDb.size() == 0).isTrue();
|
||||
|
||||
}
|
||||
|
||||
protected JpaOperations getJpaOperations(EntityManager entityManager) {
|
||||
|
||||
final DefaultJpaOperations jpaOperationsImpl = new DefaultJpaOperations();
|
||||
jpaOperationsImpl.setEntityManager(entityManager);
|
||||
jpaOperationsImpl.afterPropertiesSet();
|
||||
|
||||
return jpaOperationsImpl;
|
||||
}
|
||||
|
||||
public void testDelete() {
|
||||
final JpaOperations jpaOperations = getJpaOperations(entityManager);
|
||||
|
||||
final List<?> studentsFromDb = jpaOperations.getResultListForClass(StudentDomain.class, 0, 0);
|
||||
|
||||
assertThat(studentsFromDb).isNotNull();
|
||||
assertThat(studentsFromDb.size() == 3).isTrue();
|
||||
|
||||
|
||||
final StudentDomain student = jpaOperations.find(StudentDomain.class, 1001L);
|
||||
|
||||
assertThat(student).isNotNull();
|
||||
|
||||
|
||||
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
|
||||
// explicitly setting the transaction name is something that can only be done programmatically
|
||||
def.setName("SomeOtherTxName");
|
||||
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
|
||||
|
||||
TransactionStatus status = transactionManager.getTransaction(def);
|
||||
|
||||
jpaOperations.delete(student);
|
||||
|
||||
entityManager.flush();
|
||||
|
||||
transactionManager.commit(status);
|
||||
|
||||
final List<?> studentsFromDbAfterDelete = jpaOperations.getResultListForClass(StudentDomain.class, 0, 0);
|
||||
|
||||
assertThat(studentsFromDbAfterDelete).isNotNull();
|
||||
assertThat(studentsFromDbAfterDelete.size() == 2).isTrue();
|
||||
|
||||
}
|
||||
|
||||
public void testDeleteInBatchWithEmptyCollection() {
|
||||
final JpaOperations jpaOperations = getJpaOperations(entityManager);
|
||||
|
||||
final List<?> students = jpaOperations.getResultListForClass(StudentDomain.class, 0, 0);
|
||||
|
||||
assertThat(students).isNotNull();
|
||||
assertThat(students.size() == 3).isTrue();
|
||||
|
||||
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
|
||||
// explicitly setting the transaction name is something that can only be done programmatically
|
||||
def.setName("SomeOtherTxName");
|
||||
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
|
||||
|
||||
TransactionStatus status = transactionManager.getTransaction(def);
|
||||
|
||||
jpaOperations.deleteInBatch(new ArrayList<StudentDomain>(0));
|
||||
|
||||
entityManager.flush();
|
||||
|
||||
transactionManager.commit(status);
|
||||
|
||||
final List<?> studentsFromDb = jpaOperations.getResultListForClass(StudentDomain.class, 0, 0);
|
||||
|
||||
assertThat(studentsFromDb).isNotNull();
|
||||
assertThat(studentsFromDb.size() == 3).isTrue(); //Nothing should have happened
|
||||
|
||||
}
|
||||
|
||||
public void testGetAllStudentsFromThirdRecord() {
|
||||
JpaOperations jpaOperations = getJpaOperations(entityManager);
|
||||
List<?> results = jpaOperations.getResultListForClass(StudentDomain.class, 2, 0);
|
||||
assertThat(results.size()).isEqualTo(1);
|
||||
}
|
||||
|
||||
|
||||
public void testGetAllStudentsUsingNativeQueryFromThirdRecord() {
|
||||
JpaOperations jpaOperations = getJpaOperations(entityManager);
|
||||
String query = "select * from Student";
|
||||
List<?> results = jpaOperations.getResultListForNativeQuery(query, StudentDomain.class, null, 2, 0);
|
||||
assertThat(results.size()).isEqualTo(1);
|
||||
}
|
||||
|
||||
|
||||
public void testGetAllStudentsUsingNamedQueryFromThirdRecord() {
|
||||
JpaOperations jpaOperations = getJpaOperations(entityManager);
|
||||
List<?> results = jpaOperations.getResultListForNamedQuery("selectAllStudents", null, 2, 0);
|
||||
assertThat(results.size()).isEqualTo(1);
|
||||
}
|
||||
|
||||
public void testGetAllStudentsUsingJPAQueryFromThirdRecord() {
|
||||
JpaOperations jpaOperations = getJpaOperations(entityManager);
|
||||
String query = "select s from Student s";
|
||||
List<?> results = jpaOperations.getResultListForQuery(query, null, 2, 0);
|
||||
assertThat(results.size()).isEqualTo(1);
|
||||
}
|
||||
|
||||
public void testWithNegativeMaxNumberofResults() {
|
||||
JpaOperations jpaOperations = getJpaOperations(entityManager);
|
||||
String query = "select s from Student s";
|
||||
List<?> results = jpaOperations.getResultListForQuery(query, null, 0, -1);
|
||||
assertThat(results.size()).isEqualTo(3);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -17,12 +17,31 @@
|
||||
package org.springframework.integration.jpa.core;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import jakarta.persistence.EntityManager;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.integration.jpa.support.parametersource.ExpressionEvaluatingParameterSourceFactory;
|
||||
import org.springframework.integration.jpa.support.parametersource.ParameterSource;
|
||||
import org.springframework.integration.jpa.support.parametersource.ParameterSourceFactory;
|
||||
import org.springframework.integration.jpa.test.JpaTestUtils;
|
||||
import org.springframework.integration.jpa.test.entity.Gender;
|
||||
import org.springframework.integration.jpa.test.entity.StudentDomain;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
|
||||
import org.springframework.transaction.TransactionDefinition;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.transaction.support.DefaultTransactionDefinition;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* @author Gunnar Hillert
|
||||
@@ -31,122 +50,426 @@ import org.springframework.test.context.junit4.SpringRunner;
|
||||
* @since 2.2
|
||||
*
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringJUnitConfig
|
||||
@DirtiesContext
|
||||
public class HibernateJpaOperationsTests extends AbstractJpaOperationsTests {
|
||||
@Transactional
|
||||
public class HibernateJpaOperationsTests {
|
||||
|
||||
@Test
|
||||
@Override
|
||||
public void testExecuteUpdate() {
|
||||
super.testExecuteUpdate();
|
||||
@Autowired
|
||||
protected EntityManager entityManager;
|
||||
|
||||
DefaultJpaOperations jpaOperations;
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
this.jpaOperations = new DefaultJpaOperations();
|
||||
this.jpaOperations.setEntityManager(this.entityManager);
|
||||
this.jpaOperations.afterPropertiesSet();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Override
|
||||
public void testGetAllStudents() {
|
||||
super.testGetAllStudents();
|
||||
final List<?> students = jpaOperations.getResultListForClass(StudentDomain.class, 0, 0);
|
||||
assertThat(students).hasSize(3);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Override
|
||||
public void testGetAllStudentsWithMaxResults() {
|
||||
super.testGetAllStudentsWithMaxResults();
|
||||
final List<?> students = jpaOperations.getResultListForClass(StudentDomain.class, 0, 2);
|
||||
assertThat(students).hasSize(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExecuteUpdate() {
|
||||
final StudentDomain student = JpaTestUtils.getTestStudent();
|
||||
|
||||
List<?> students = jpaOperations.getResultListForClass(StudentDomain.class, 0, 0);
|
||||
assertThat(students.size() == 3).isTrue();
|
||||
|
||||
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();
|
||||
|
||||
assertThat(updatedRecords).isEqualTo(1);
|
||||
assertThat(student.getRollNumber()).isNull();
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@Override
|
||||
public void testExecuteUpdateWithNamedQuery() {
|
||||
super.testExecuteUpdateWithNamedQuery();
|
||||
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();
|
||||
|
||||
assertThat(updatedRecords).isEqualTo(1);
|
||||
assertThat(student.getRollNumber()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Override
|
||||
public void testExecuteUpdateWithNativeQuery() {
|
||||
super.testExecuteUpdateWithNativeQuery();
|
||||
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();
|
||||
|
||||
assertThat(updatedRecords).isEqualTo(1);
|
||||
assertThat(student.getRollNumber()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Override
|
||||
public void testExecuteSelectWithNativeQueryReturningEntityClass() throws ParseException {
|
||||
super.testExecuteSelectWithNativeQueryReturningEntityClass();
|
||||
String selectSqlQuery = "select * from Student where lastName = 'Last One'";
|
||||
|
||||
Class<?> entityClass = StudentDomain.class;
|
||||
|
||||
List<?> students = jpaOperations.getResultListForNativeQuery(selectSqlQuery, entityClass, null, 0, 0);
|
||||
|
||||
assertThat(students.size() == 1).isTrue();
|
||||
|
||||
StudentDomain retrievedStudent = (StudentDomain) students.iterator().next();
|
||||
|
||||
SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
|
||||
|
||||
assertThat(retrievedStudent.getDateOfBirth()).isEqualTo(formatter.parse("1980/01/01"));
|
||||
assertThat(retrievedStudent.getFirstName()).isEqualTo("First One");
|
||||
assertThat(retrievedStudent.getGender()).isEqualTo(Gender.MALE);
|
||||
assertThat(retrievedStudent.getLastName()).isEqualTo("Last One");
|
||||
assertThat(retrievedStudent.getLastUpdated()).isNotNull();
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@Override
|
||||
public void testExecuteSelectWithNativeQuery() throws ParseException {
|
||||
super.testExecuteSelectWithNativeQuery();
|
||||
String selectSqlQuery = "select rollNumber, firstName, lastName, gender, dateOfBirth, lastUpdated " +
|
||||
"from Student where lastName = 'Last One'";
|
||||
|
||||
List<?> students = jpaOperations.getResultListForNativeQuery(selectSqlQuery, null, null, 0, 0);
|
||||
|
||||
assertThat(students.size() == 1).isTrue();
|
||||
|
||||
Object[] retrievedStudent = (Object[]) students.iterator().next();
|
||||
|
||||
SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
|
||||
|
||||
assertThat(retrievedStudent[0]).isNotNull();
|
||||
assertThat(retrievedStudent[1]).isEqualTo("First One");
|
||||
assertThat(retrievedStudent[2]).isEqualTo("Last One");
|
||||
assertThat(retrievedStudent[3]).isEqualTo("M");
|
||||
assertThat(retrievedStudent[4]).isEqualTo(formatter.parse("1980/01/01"));
|
||||
assertThat(retrievedStudent[5]).isNotNull();
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@Override
|
||||
public void testExecuteUpdateWithNativeNamedQuery() {
|
||||
super.testExecuteUpdateWithNativeNamedQuery();
|
||||
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();
|
||||
|
||||
assertThat(updatedRecords).isEqualTo(1);
|
||||
assertThat(student.getRollNumber()).isNull();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link org.springframework.integration.jpa.core.DefaultJpaOperations#merge(java.lang.Object)}.
|
||||
*/
|
||||
@Test
|
||||
@Override
|
||||
public void testMerge() {
|
||||
super.testMerge();
|
||||
final StudentDomain student = JpaTestUtils.getTestStudent();
|
||||
|
||||
assertThat(student.getRollNumber()).isNull();
|
||||
final StudentDomain savedStudent = (StudentDomain) jpaOperations.merge(student);
|
||||
entityManager.flush();
|
||||
assertThat(savedStudent).isNotNull();
|
||||
assertThat(savedStudent.getRollNumber()).isNotNull();
|
||||
assertThat(student.getRollNumber()).isEqualTo(savedStudent.getRollNumber());
|
||||
|
||||
assertThat(student != savedStudent).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Override
|
||||
public void testMergeCollection() {
|
||||
super.testMergeCollection();
|
||||
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<>(3);
|
||||
|
||||
students.add(student1);
|
||||
students.add(student2);
|
||||
students.add(student3);
|
||||
|
||||
assertThat(student1.getRollNumber()).isNull();
|
||||
assertThat(student2.getRollNumber()).isNull();
|
||||
assertThat(student3.getRollNumber()).isNull();
|
||||
|
||||
Object savedStudents = jpaOperations.merge(students, 10, true);
|
||||
|
||||
assertThat(savedStudents).isInstanceOf(List.class);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
List<StudentDomain> savedStudentCollection = (List<StudentDomain>) savedStudents;
|
||||
|
||||
assertThat(savedStudentCollection.get(0).getRollNumber()).isNotNull();
|
||||
assertThat(savedStudentCollection.get(1).getRollNumber()).isNotNull();
|
||||
assertThat(savedStudentCollection.get(2).getRollNumber()).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Override
|
||||
public void testMergeNullCollection() {
|
||||
super.testMergeNullCollection();
|
||||
assertThatIllegalArgumentException()
|
||||
.isThrownBy(() -> jpaOperations.merge(null))
|
||||
.withMessage("The object to merge must not be null.");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Override
|
||||
public void testMergeCollectionWithNullElement() {
|
||||
super.testMergeCollectionWithNullElement();
|
||||
final List<?> studentsFromDbBeforeTest = jpaOperations.getResultListForClass(StudentDomain.class, 0, 0);
|
||||
|
||||
assertThat(studentsFromDbBeforeTest).hasSize(3);
|
||||
|
||||
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<>(3);
|
||||
|
||||
students.add(student1);
|
||||
students.add(student2);
|
||||
students.add(student3);
|
||||
|
||||
assertThat(student1.getRollNumber()).isNull();
|
||||
assertThat(student2).isNull();
|
||||
assertThat(student3.getRollNumber()).isNull();
|
||||
|
||||
Object savedStudents = jpaOperations.merge(students);
|
||||
entityManager.flush();
|
||||
|
||||
assertThat(savedStudents instanceof List<?>).isTrue();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
List<StudentDomain> savedStudentCollection = (List<StudentDomain>) savedStudents;
|
||||
|
||||
assertThat(savedStudentCollection.get(0).getRollNumber()).isNotNull();
|
||||
assertThat(savedStudentCollection.get(1).getRollNumber()).isNotNull();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link org.springframework.integration.jpa.core.DefaultJpaOperations#persist(java.lang.Object)}.
|
||||
*/
|
||||
@Test
|
||||
@Override
|
||||
public void testPersist() {
|
||||
super.testPersist();
|
||||
final StudentDomain student = JpaTestUtils.getTestStudent();
|
||||
|
||||
assertThat(student.getRollNumber()).isNull();
|
||||
jpaOperations.persist(student, 1, false);
|
||||
assertThat(student.getRollNumber()).isNotNull();
|
||||
|
||||
assertThat(entityManager.contains(student)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Override
|
||||
public void testPersistCollection() {
|
||||
super.testPersistCollection();
|
||||
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<>(3);
|
||||
|
||||
students.add(student1);
|
||||
students.add(student2);
|
||||
students.add(student3);
|
||||
|
||||
assertThat(student1.getRollNumber()).isNull();
|
||||
assertThat(student2.getRollNumber()).isNull();
|
||||
assertThat(student3.getRollNumber()).isNull();
|
||||
|
||||
jpaOperations.persist(students, 1, true);
|
||||
assertThat(student1.getRollNumber()).isNotNull();
|
||||
assertThat(student2.getRollNumber()).isNotNull();
|
||||
assertThat(student3.getRollNumber()).isNotNull();
|
||||
|
||||
assertThat(entityManager.contains(student1)).isFalse();
|
||||
assertThat(entityManager.contains(student2)).isFalse();
|
||||
assertThat(entityManager.contains(student3)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Override
|
||||
public void testPersistNullCollection() {
|
||||
super.testPersistNullCollection();
|
||||
assertThatIllegalArgumentException()
|
||||
.isThrownBy(() -> jpaOperations.persist(null))
|
||||
.withMessage("The object to persist must not be null.");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Override
|
||||
public void testPersistCollectionWithNullElement() {
|
||||
super.testPersistCollectionWithNullElement();
|
||||
final List<?> studentsFromDbBeforeTest = jpaOperations.getResultListForClass(StudentDomain.class, 0, 0);
|
||||
|
||||
assertThat(studentsFromDbBeforeTest.size()).isEqualTo(3);
|
||||
|
||||
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<>(3);
|
||||
|
||||
students.add(student1);
|
||||
students.add(student2);
|
||||
students.add(student3);
|
||||
|
||||
assertThat(student1.getRollNumber()).isNull();
|
||||
assertThat(student2).isNull();
|
||||
assertThat(student3.getRollNumber()).isNull();
|
||||
|
||||
jpaOperations.persist(students, 10, false);
|
||||
|
||||
assertThat(student1.getRollNumber()).isNotNull();
|
||||
assertThat(student3.getRollNumber()).isNotNull();
|
||||
|
||||
final List<?> studentsFromDb = jpaOperations.getResultListForClass(StudentDomain.class, 0, 0);
|
||||
|
||||
assertThat(studentsFromDb).hasSize(5);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Override
|
||||
public void testDeleteInBatch() {
|
||||
super.testDeleteInBatch();
|
||||
final List<?> students = jpaOperations.getResultListForClass(StudentDomain.class, 0, 0);
|
||||
|
||||
assertThat(students).isNotNull();
|
||||
|
||||
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
|
||||
// explicitly setting the transaction name is something that can only be done programmatically
|
||||
def.setName("SomeOtherTxName");
|
||||
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
|
||||
|
||||
jpaOperations.deleteInBatch(students);
|
||||
|
||||
entityManager.flush();
|
||||
|
||||
final List<?> studentsFromDb = jpaOperations.getResultListForClass(StudentDomain.class, 0, 0);
|
||||
|
||||
assertThat(studentsFromDb).hasSize(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Override
|
||||
public void testDelete() {
|
||||
super.testDelete();
|
||||
final List<?> studentsFromDb = jpaOperations.getResultListForClass(StudentDomain.class, 0, 0);
|
||||
|
||||
assertThat(studentsFromDb).isNotNull();
|
||||
assertThat(studentsFromDb.size() == 3).isTrue();
|
||||
|
||||
|
||||
final StudentDomain student = jpaOperations.find(StudentDomain.class, 1001L);
|
||||
|
||||
assertThat(student).isNotNull();
|
||||
|
||||
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
|
||||
// explicitly setting the transaction name is something that can only be done programmatically
|
||||
def.setName("SomeOtherTxName");
|
||||
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
|
||||
|
||||
jpaOperations.delete(student);
|
||||
|
||||
entityManager.flush();
|
||||
|
||||
final List<?> studentsFromDbAfterDelete = jpaOperations.getResultListForClass(StudentDomain.class, 0, 0);
|
||||
|
||||
assertThat(studentsFromDbAfterDelete).isNotNull();
|
||||
assertThat(studentsFromDbAfterDelete).hasSize(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Override
|
||||
public void testDeleteInBatchWithEmptyCollection() {
|
||||
super.testDeleteInBatchWithEmptyCollection();
|
||||
final List<?> students = jpaOperations.getResultListForClass(StudentDomain.class, 0, 0);
|
||||
|
||||
assertThat(students).isNotNull();
|
||||
assertThat(students.size() == 3).isTrue();
|
||||
|
||||
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
|
||||
// explicitly setting the transaction name is something that can only be done programmatically
|
||||
def.setName("SomeOtherTxName");
|
||||
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
|
||||
|
||||
jpaOperations.deleteInBatch(new ArrayList<StudentDomain>(0));
|
||||
|
||||
entityManager.flush();
|
||||
|
||||
final List<?> studentsFromDb = jpaOperations.getResultListForClass(StudentDomain.class, 0, 0);
|
||||
|
||||
assertThat(studentsFromDb).hasSize(3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAllStudentsFromThirdRecord() {
|
||||
List<?> results = jpaOperations.getResultListForClass(StudentDomain.class, 2, 0);
|
||||
assertThat(results).hasSize(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAllStudentsUsingNativeQueryFromThirdRecord() {
|
||||
String query = "select * from Student";
|
||||
List<?> results = jpaOperations.getResultListForNativeQuery(query, StudentDomain.class, null, 2, 0);
|
||||
assertThat(results).hasSize(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAllStudentsUsingNamedQueryFromThirdRecord() {
|
||||
List<?> results = jpaOperations.getResultListForNamedQuery("selectAllStudents", null, 2, 0);
|
||||
assertThat(results).hasSize(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAllStudentsUsingJPAQueryFromThirdRecord() {
|
||||
String query = "select s from Student s";
|
||||
List<?> results = jpaOperations.getResultListForQuery(query, null, 2, 0);
|
||||
assertThat(results).hasSize(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWithNegativeMaxNumberOfResults() {
|
||||
String query = "select s from Student s";
|
||||
List<?> results = jpaOperations.getResultListForQuery(query, null, 0, -1);
|
||||
assertThat(results).hasSize(3);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2016-2022 the original author or authors.
|
||||
* Copyright 2016-2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -166,7 +166,7 @@ public class JpaTests {
|
||||
StudentDomain mergedStudent = (StudentDomain) receive.getPayload();
|
||||
assertThat(mergedStudent.getFirstName()).isEqualTo(student.getFirstName());
|
||||
assertThat(mergedStudent.getRollNumber()).isNotNull();
|
||||
assertThat(student.getRollNumber()).isNull();
|
||||
assertThat(student.getRollNumber()).isEqualTo(mergedStudent.getRollNumber());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2022 the original author or authors.
|
||||
* Copyright 2002-2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -74,7 +74,6 @@ public class JpaOutboundChannelAdapterTests {
|
||||
|
||||
@Test
|
||||
public void saveEntityWithMerge() {
|
||||
|
||||
List<?> results1 = this.jdbcTemplate.queryForList("Select * from Student");
|
||||
assertThat(results1).isNotNull();
|
||||
assertThat(results1.size() == 3).isTrue();
|
||||
@@ -101,16 +100,13 @@ public class JpaOutboundChannelAdapterTests {
|
||||
});
|
||||
|
||||
List<?> results2 = this.jdbcTemplate.queryForList("Select * from Student");
|
||||
assertThat(results2).isNotNull();
|
||||
assertThat(results2.size() == 4).isTrue();
|
||||
|
||||
assertThat(testStudent.getRollNumber()).isNull();
|
||||
assertThat(results2).hasSize(4);
|
||||
|
||||
assertThat(testStudent.getRollNumber()).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void saveEntityWithMergeWithoutSpecifyingEntityClass() throws InterruptedException {
|
||||
|
||||
public void saveEntityWithMergeWithoutSpecifyingEntityClass() {
|
||||
List<?> results1 = this.jdbcTemplate.queryForList("Select * from Student");
|
||||
assertThat(results1).isNotNull();
|
||||
assertThat(results1.size() == 3).isTrue();
|
||||
@@ -136,16 +132,13 @@ public class JpaOutboundChannelAdapterTests {
|
||||
});
|
||||
|
||||
List<?> results2 = this.jdbcTemplate.queryForList("Select * from Student");
|
||||
assertThat(results2).isNotNull();
|
||||
assertThat(results2.size() == 4).isTrue();
|
||||
|
||||
assertThat(testStudent.getRollNumber()).isNull();
|
||||
assertThat(results2).hasSize(4);
|
||||
|
||||
assertThat(testStudent.getRollNumber()).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void saveEntityWithPersist() throws InterruptedException {
|
||||
|
||||
public void saveEntityWithPersist() {
|
||||
List<?> results1 = this.jdbcTemplate.queryForList("Select * from Student");
|
||||
assertThat(results1).isNotNull();
|
||||
assertThat(results1.size() == 3).isTrue();
|
||||
@@ -185,9 +178,8 @@ public class JpaOutboundChannelAdapterTests {
|
||||
assertThat(testStudent.getRollNumber()).isNotNull();
|
||||
}
|
||||
|
||||
@Test //INT-2557
|
||||
public void saveEntityWithPersistWithinChain() throws InterruptedException {
|
||||
|
||||
@Test
|
||||
public void saveEntityWithPersistWithinChain() {
|
||||
List<?> results1 = this.jdbcTemplate.queryForList("Select * from Student");
|
||||
assertThat(results1).isNotNull();
|
||||
assertThat(results1.size() == 3).isTrue();
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2022 the original author or authors.
|
||||
* Copyright 2002-2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -20,8 +20,7 @@ import java.util.List;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
@@ -32,7 +31,7 @@ import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessageChannel;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@@ -44,7 +43,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
* @since 2.2
|
||||
*
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringJUnitConfig
|
||||
@DirtiesContext
|
||||
public class JpaOutboundChannelAdapterTransactionalTests {
|
||||
|
||||
@@ -60,8 +59,8 @@ public class JpaOutboundChannelAdapterTransactionalTests {
|
||||
List<?> results1 =
|
||||
new JdbcTemplate(this.dataSource)
|
||||
.queryForList("Select * from Student");
|
||||
assertThat(results1).isNotNull();
|
||||
assertThat(results1.size()).isEqualTo(3);
|
||||
|
||||
assertThat(results1).hasSize(3);
|
||||
|
||||
StudentDomain testStudent = JpaTestUtils.getTestStudent();
|
||||
Message<StudentDomain> message = MessageBuilder.withPayload(testStudent).build();
|
||||
@@ -71,10 +70,9 @@ public class JpaOutboundChannelAdapterTransactionalTests {
|
||||
List<?> results2 =
|
||||
new JdbcTemplate(this.dataSource)
|
||||
.queryForList("Select * from Student");
|
||||
assertThat(results2).isNotNull();
|
||||
assertThat(results2.size()).isEqualTo(4);
|
||||
|
||||
assertThat(testStudent.getRollNumber()).isNull();
|
||||
assertThat(results2).hasSize(4);
|
||||
assertThat(testStudent.getRollNumber()).isNotNull();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user