休眠Joda DateTime排序 [英] Hibernate Joda DateTime Sorting
问题描述
我正在将Joda DateTime和UserType库用于休眠4
I am using Joda DateTime and the UserType library for hibernate 4
我有一个JPA实体,其中包含以下字段
I have a JPA entity with the following field
@Columns(columns = { @Column(name = "lastUsedDateTimeStamp"), @Column(name = "lastUsedDateTimeStamp_TMZ") })
@Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTimeAsString")
private DateTime lastUsedDateTimeStamp;
我正在使用常规的Spring Data JPA存储库,如下所示:
I am using a normal Spring Data JPA repository as follows:
return repository.findAll(new PageRequest(0,5,new Sort(Sort.Direction.DESC,"lastUsedDateTimeStamp"))).getContent();
return repository.findAll(new PageRequest(0, 5, new Sort(Sort.Direction.DESC, "lastUsedDateTimeStamp"))).getContent();
但是,当我查看休眠中抛出的sql时,它的结束如下:
However when I look at the sql that hibernate throws out in the logs it end as follows:
order by
entity.lastUsedDateTimeStamp,
entity.lastUsedDateTimeStamp_TMZ asc limit ?
这意味着按预期,对lastUsedDateTimeStamp列的排序不起作用,因为"asc"关键字位于lastUsedDateTimeStamp_TMZ之后而不是lastUsedDateTimeStamp.
This means that the sorting is not working on the lastUsedDateTimeStamp column as expected, as the "asc" keyword is after lastUsedDateTimeStamp_TMZ instead of lastUsedDateTimeStamp.
有人知道我该如何解决,以便查询在正确的字段上指定"asc"吗?
Does anyone know how I can fix it so that the query specifies "asc" on the correct field?
推荐答案
我自己解决了这个问题, 必须编写我自己的自定义PersistentDateTimeAsString和AbstractMultiColumnDateTime类,以颠倒这两个字段的默认顺序.
Solved this one myself, had to write my own custom PersistentDateTimeAsString and AbstractMultiColumnDateTime classes that reversed the default order of the 2 fields.
时区是顺序中的第一位,然后是日期时间. 因此,SQL现在看起来像这样:
timezone is now first in the order and then date time. So the sql now looks like this:
order by
entity.lastUsedDateTimeStamp_TMZ,
entity.lastUsedDateTimeStamp asc limit ?
这篇关于休眠Joda DateTime排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!