BAEL-6055 NonUniqueResultException in Spring Data JPA (#13173)

* BAEL-6055 NonUniqueResultException in Spring Data JPA

* update using AssertJ Exception

* Update persistence-modules/spring-data-jpa-query-3/src/test/java/com/baeldung/spring/data/jpa/query/nonuniqueresultexception/NonUniqueResultExceptionIntegrationTest.java

Co-authored-by: KevinGilmore <kpg102@gmail.com>

* Update persistence-modules/spring-data-jpa-query-3/src/test/java/com/baeldung/spring/data/jpa/query/nonuniqueresultexception/NonUniqueResultExceptionIntegrationTest.java

Co-authored-by: KevinGilmore <kpg102@gmail.com>

Co-authored-by: KevinGilmore <kpg102@gmail.com>
This commit is contained in:
Kilian Schneider 2022-12-30 19:03:21 +01:00 committed by GitHub
parent 6579553e8d
commit a26acf8a0c
2 changed files with 38 additions and 0 deletions

View File

@ -14,6 +14,9 @@ public interface ArticleRepository extends JpaRepository<Article, Integer> {
List<Article> findAllByPublicationTimeBetween(Date publicationTimeStart,
Date publicationTimeEnd);
Article findByPublicationTimeBetween(Date publicationTimeStart,
Date publicationTimeEnd);
@Query("select a from Article a where a.creationDateTime <= :creationDateTime")
List<Article> findAllWithCreationDateTimeBefore(
@Param("creationDateTime") Date creationDateTime);

View File

@ -0,0 +1,35 @@
package com.baeldung.spring.data.jpa.query.nonuniqueresultexception;
import com.baeldung.spring.data.jpa.query.datetime.ArticleRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.test.context.junit4.SpringRunner;
import javax.persistence.NonUniqueResultException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
@RunWith(SpringRunner.class)
@DataJpaTest(properties = "spring.sql.init.data-locations=classpath:import_entities.sql", showSql = false)
public class NonUniqueResultExceptionIntegrationTest {
@Autowired
private ArticleRepository repository;
@Test
public void givenImportedArticles_whenFindByPublicationTimeBetween_thenIncorrectResultSizeDataAccessExceptionThrown() {
assertThatThrownBy(() -> repository.findByPublicationTimeBetween(new SimpleDateFormat("HH:mm").parse("15:15"), new SimpleDateFormat("HH:mm").parse("16:30")))
.isInstanceOf(IncorrectResultSizeDataAccessException.class)
.hasCauseInstanceOf(NonUniqueResultException.class);
}
@Test
public void givenImportedArticles_whenFindAllByPublicationTimeBetween_thenSuccess() throws ParseException {
repository.findAllByPublicationTimeBetween(new SimpleDateFormat("HH:mm").parse("15:15"), new SimpleDateFormat("HH:mm").parse("16:30"));
}
}