Spring数据JPA:仅按java.time.LocalDateTime列的日期部分排序
实体: 要使用排序org.springframework.data.jpa.repository.JpaSpecificationExecutorfindAllorg.springframework.data.jpa.domain.Specification进行搜索,请使用org.springframework.data.domain.Pageable 由于具体情况,我必须按两个字段排序:postingDate和created,并将postingDate视为日期,而不是实体中定义的日期时间,但我不知道如何进行排序 要向MySQL DB发出这样的请求,应该使用DATE函数,如下所示:Spring数据JPA:仅按java.time.LocalDateTime列的日期部分排序,java,spring,hibernate,sorting,jpa,Java,Spring,Hibernate,Sorting,Jpa,实体: 要使用排序org.springframework.data.jpa.repository.JpaSpecificationExecutorfindAllorg.springframework.data.jpa.domain.Specification进行搜索,请使用org.springframework.data.domain.Pageable 由于具体情况,我必须按两个字段排序:postingDate和created,并将postingDate视为日期,而不是实体中定义的日期时间,但我
import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Table;
import java.time.LocalDateTime;
import java.util.UUID;
@Table( name = "order" )
public class Order
{
@Id
@Column( name = "id", columnDefinition = "BINARY(16)" )
private UUID id;
@Column( name = "posting_date" )
private LocalDateTime postingDate;
@Column( name = "created" )
private LocalDateTime created;
}
但是我想使用JPA方法来搜索和排序
问:有没有办法告诉JPA,要排序的这个或那个列应该用一些函数包装?如何做到这一点?原则上这是可行的。但是我没有运行下面的代码!此外,function关键字被添加到JPA2.1中,如果您使用的是早期版本,那么这可能不起作用 JPQL: 从订单o中选择o,其中。。。 订购人 功能“日期”,o.postingDate DESC, o、 创建描述
Criteria API也支持这一点。我认为不可能使用Spring Sort/Pageable来实现这一点。使用Hibernate/Criteria API/JPQL应该相当简单。好吧,但是如何将其应用于排序?即使通过函数“DATE”也找不到方法,。。。在ORDER BY条款中,您还需要什么?With criteria API类似于query.orderBycb.desccb.functionDATE、Date.class、root.getpostingDate
select
order.*
from
order o
where
...
order by
date(o.posting_date) desc,
o.pae_created desc