Spring数据存储库:带顺序的Find&Quot;方法获得错误的顺序
我有一个带有一些JPA存储库的Spring Boot应用程序。 我在其中一个存储库接口中定义了以下方法:
public List<Post> findAllByOrderByPublishedOnDesc();
很明显,我想检索按publishOn
DESC排序的所有Post
的列表。奇怪的是,当两个或更多帖子的值publishedOn
只与LocalDateTime类的分钟组件不同时,我会得到错误的顺序(请参见下面代码中的注释)。
我做错了什么?
以下是示例代码。使用h2数据库执行测试:
实体:
@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;
}
存储库:
public interface PostRepository extends JpaRepository<Post, Long>
{
public Post findBySourceUrl(String sourceUrl);
public List<Post> findAllByOrderByPublishedOnDesc();
}
测试:
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>
<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>
解决方案
jpa默认情况下会将Java的LocalDateTime
映射到BLOB
,而不是DateTime
或Timestamp
。
数据库无法理解如何对Blob执行日期/时间排序。
- 确保使用适当的数据类型创建架构(使用
@Column
批注或使用SQL脚本)。 - 将
hibernate-java8
依赖项添加到您的项目
执行上述步骤后,将进行正确的排序。
相关文章