Hibernate Joda DateTime Sorting

帅比萌擦擦* 提交于 2020-01-24 12:47:32

问题


I am using Joda DateTime and the UserType library for hibernate 4

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;

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();

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 ?

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.

Does anyone know how I can fix it so that the query specifies "asc" on the correct field?


回答1:


Solved this one myself, had to write my own custom PersistentDateTimeAsString and AbstractMultiColumnDateTime classes that reversed the default order of the 2 fields.

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 ?


来源:https://stackoverflow.com/questions/14278812/hibernate-joda-datetime-sorting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!