diff --git a/persistence-modules/spring-data-jpa-query-3/src/main/java/com/baeldung/spring/data/jpa/query/datetime/ArticleRepository.java b/persistence-modules/spring-data-jpa-query-3/src/main/java/com/baeldung/spring/data/jpa/query/datetime/ArticleRepository.java index 9ec14884f4..726764b411 100644 --- a/persistence-modules/spring-data-jpa-query-3/src/main/java/com/baeldung/spring/data/jpa/query/datetime/ArticleRepository.java +++ b/persistence-modules/spring-data-jpa-query-3/src/main/java/com/baeldung/spring/data/jpa/query/datetime/ArticleRepository.java @@ -14,6 +14,9 @@ public interface ArticleRepository extends JpaRepository { List
findAllByPublicationTimeBetween(Date publicationTimeStart, Date publicationTimeEnd); + Article findByPublicationTimeBetween(Date publicationTimeStart, + Date publicationTimeEnd); + @Query("select a from Article a where a.creationDateTime <= :creationDateTime") List
findAllWithCreationDateTimeBefore( @Param("creationDateTime") Date creationDateTime); diff --git a/persistence-modules/spring-data-jpa-query-3/src/test/java/com/baeldung/spring/data/jpa/query/nonuniqueresultexception/NonUniqueResultExceptionIntegrationTest.java b/persistence-modules/spring-data-jpa-query-3/src/test/java/com/baeldung/spring/data/jpa/query/nonuniqueresultexception/NonUniqueResultExceptionIntegrationTest.java new file mode 100644 index 0000000000..b032819ad6 --- /dev/null +++ b/persistence-modules/spring-data-jpa-query-3/src/test/java/com/baeldung/spring/data/jpa/query/nonuniqueresultexception/NonUniqueResultExceptionIntegrationTest.java @@ -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")); + } +}