Skip to content

Commit dd11c12

Browse files
dupirefrmaibin
authored andcommitted
dupirefr/dupire.francois+pro@gmail.com [BAEL-1981] Query entities by dates and times with Spring Data JPA (eugenp#4737)
* [BAEL-1981] Article entity and repository + tests * [BAEL-1981] Removing unnecessary fields
1 parent 5d38063 commit dd11c12

5 files changed

Lines changed: 115 additions & 1 deletion

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.domain;
2+
3+
import javax.persistence.*;
4+
import java.util.Date;
5+
6+
@Entity
7+
public class Article {
8+
9+
@Id
10+
@GeneratedValue
11+
private Integer id;
12+
@Temporal(TemporalType.DATE)
13+
private Date publicationDate;
14+
@Temporal(TemporalType.TIME)
15+
private Date publicationTime;
16+
@Temporal(TemporalType.TIMESTAMP)
17+
private Date creationDateTime;
18+
19+
public Integer getId() {
20+
return id;
21+
}
22+
23+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.repository;
2+
3+
import com.baeldung.domain.Article;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
import org.springframework.data.jpa.repository.Query;
6+
import org.springframework.data.repository.query.Param;
7+
8+
import java.util.Date;
9+
import java.util.List;
10+
11+
public interface ArticleRepository extends JpaRepository<Article, Integer> {
12+
13+
List<Article> findAllByPublicationDate(Date publicationDate);
14+
15+
List<Article> findAllByPublicationTimeBetween(Date publicationTimeStart,
16+
Date publicationTimeEnd);
17+
18+
@Query("select a from Article a where a.creationDateTime <= :creationDateTime")
19+
List<Article> findAllWithCreationDateTimeBefore(
20+
@Param("creationDateTime") Date creationDateTime);
21+
22+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.baeldung.repository;
2+
3+
import com.baeldung.domain.Article;
4+
import org.junit.Test;
5+
import org.junit.runner.RunWith;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
8+
import org.springframework.test.context.junit4.SpringRunner;
9+
10+
import java.text.SimpleDateFormat;
11+
import java.util.Arrays;
12+
import java.util.List;
13+
14+
import static org.junit.Assert.assertEquals;
15+
import static org.junit.Assert.assertTrue;
16+
17+
@RunWith(SpringRunner.class)
18+
@DataJpaTest
19+
public class ArticleRepositoryIntegrationTest {
20+
21+
@Autowired
22+
private ArticleRepository repository;
23+
24+
@Test
25+
public void givenImportedArticlesWhenFindAllByPublicationDateThenArticles1And2Returned()
26+
throws Exception {
27+
List<Article> result = repository.findAllByPublicationDate(
28+
new SimpleDateFormat("yyyy-MM-dd").parse("2018-01-01")
29+
);
30+
31+
assertEquals(2, result.size());
32+
assertTrue(result.stream()
33+
.map(Article::getId)
34+
.allMatch(id -> Arrays.asList(1, 2).contains(id))
35+
);
36+
}
37+
38+
@Test
39+
public void givenImportedArticlesWhenFindAllByPublicationTimeBetweenThenArticles2And3Returned()
40+
throws Exception {
41+
List<Article> result = repository.findAllByPublicationTimeBetween(
42+
new SimpleDateFormat("HH:mm").parse("15:15"),
43+
new SimpleDateFormat("HH:mm").parse("16:30")
44+
);
45+
46+
assertEquals(2, result.size());
47+
assertTrue(result.stream()
48+
.map(Article::getId)
49+
.allMatch(id -> Arrays.asList(2, 3).contains(id))
50+
);
51+
}
52+
53+
@Test
54+
public void givenImportedArticlesWhenFindAllWithCreationDateTimeBeforeThenArticles2And3Returned() throws Exception {
55+
List<Article> result = repository.findAllWithCreationDateTimeBefore(
56+
new SimpleDateFormat("yyyy-MM-dd HH:mm").parse("2017-12-15 10:00")
57+
);
58+
59+
assertEquals(2, result.size());
60+
assertTrue(result.stream()
61+
.map(Article::getId)
62+
.allMatch(id -> Arrays.asList(2, 3).contains(id))
63+
);
64+
}
65+
66+
}

‎spring-boot-persistence/src/test/resources/application.properties‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ hibernate.cache.use_query_cache=true
1313
hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory
1414

1515
spring.jpa.properties.hibernate.hbm2ddl.import_files=migrated_users.sql
16-
spring.datasource.data=import_*_users.sql
16+
spring.datasource.data=import_*_users.sql,import_articles.sql
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
insert into Article(id, publication_date, publication_time, creation_date_time) values(1, TO_DATE('01/01/2018', 'DD/MM/YYYY'), TO_DATE('15:00', 'HH24:MI'), TO_DATE('31/12/2017 07:30', 'DD/MM/YYYY HH24:MI'));
2+
insert into Article(id, publication_date, publication_time, creation_date_time) values(2, TO_DATE('01/01/2018', 'DD/MM/YYYY'), TO_DATE('15:30', 'HH24:MI'), TO_DATE('15/12/2017 08:00', 'DD/MM/YYYY HH24:MI'));
3+
insert into Article(id, publication_date, publication_time, creation_date_time) values(3, TO_DATE('15/12/2017', 'DD/MM/YYYY'), TO_DATE('16:00', 'HH24:MI'), TO_DATE('01/12/2017 13:45', 'DD/MM/YYYY HH24:MI'));

0 commit comments

Comments
 (0)