Reputation: 3242
I have a Spring Boot application with some JPA repositories. I defined the following method inside one of the repository interfaces:
public List<Post> findAllByOrderByPublishedOnDesc();
Quite obviously, I would like to retrive the list of all the Post
s sorted by publishOn
DESC. Strangely, I get the order wrong when two or more of the posts have a value of publishedOn
that differs only for the minute component of the LocalDateTime class (see the comments in the code below).
What Am I doing wrong?
Here's the example code. The test is executed with the H2 database:
Entity:
@Entity
public class Post
{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Long id;
@Column(unique = true, nullable = false)
String sourceUrl;
@Column
String title;
@Column
LocalDateTime publishedOn;
@LastModifiedDate
LocalDateTime editedOn;
@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
Text text;
}
Repository:
public interface PostRepository extends JpaRepository<Post, Long>
{
public Post findBySourceUrl(String sourceUrl);
public List<Post> findAllByOrderByPublishedOnDesc();
}
Test:
public class OrderByJPATest
{
@Autowired
PostRepository postRepo;
public OrderByJPATest()
{
}
@Test
public void testOrderByDays()
{
// First try: older by 1 day
Post newer = insertTestEntity("newer", LocalDateTime.of(2016, 11, 13, 0, 0));
Post older = insertTestEntity("older", LocalDateTime.of(2016, 11, 12, 0, 0));
List<Post> ordered = postRepo.findAllByOrderByPublishedOnDesc();
ordered.stream().forEach((post) -> log.info("{} => {}", post.getSourceUrl(), post.getPublishedOn()));
/*
output:
newer => 2016-11-13T00:00
older => 2016-11-12T00:00
*/
assertTrue(ordered.get(0).getPublishedOn()
.isAfter(ordered.get(1).getPublishedOn()));
postRepo.deleteAll();
postRepo.flush();
// Second try: older by 1 minute
newer = insertTestEntity("newerBy1Min", LocalDateTime.of(2016, 11, 13, 01, 02));
older = insertTestEntity("olderBy1Min", LocalDateTime.of(2016, 11, 13, 01, 01));
ordered = postRepo.findAllByOrderByPublishedOnDesc();
ordered.stream().forEach((post) -> log.info("{} => {}", post.getSourceUrl(), post.getPublishedOn()));
/*
output:
olderBy1Min => 2016-11-13T01:01
newerBy1Min => 2016-11-13T01:02
*/
// this assert fails!
assertTrue(ordered.get(0).getPublishedOn()
.isAfter(ordered.get(1).getPublishedOn()));
}
private Post insertTestEntity(String url, LocalDateTime publishDate)
{
Text text = new Text();
text.setValue("Testing...");
Post post = new Post();
post.setPublishedOn(publishDate);
post.setSourceUrl(url);
post.setText(text);
return postRepo.save(post);
}
}
POM (dependencies):
<dependencies>
<dependency>
<groupId>com.atlassian.commonmark</groupId>
<artifactId>commonmark</artifactId>
<version>0.6.0</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<!--Additional dependencies -end-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-java8time</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Upvotes: 1
Views: 3128
Reputation: 7051
JPA by default will map Java's LocalDateTime
to a BLOB
instead of a DateTime
or Timestamp
.
The database doesn't understand how to perform a date/time sort on a BLOB.
@Column
annotation or with a SQL script).hibernate-java8
dependency to your projectAfter performing the above steps, the correct ordering will take place.
Upvotes: 1