Spring Data JPA:仅按java.time.LocalDateTime列的日期部分排序

huangapple go评论71阅读模式
英文:

Spring Data JPA: Sort only by date part of java.time.LocalDateTime column

问题

Entity:

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;
}

To search with sorting org.springframework.data.jpa.repository.JpaSpecificationExecutor#findAll(org.springframework.data.jpa.domain.Specification<T>, org.springframework.data.domain.Pageable) is used.

Due to a specific case, I have to sort by two fields: postingDate and created and treat postingDate as a date (not datetime like it is defined in the entity), but I don't know how.

To make such a request to the MySQL DB, the DATE() function should be used, like this:

select
    order.*
from
    order o
where
    ...
order by
    date(o.posting_date) desc,
    o.pae_created desc

But I want to use JPA methods to search and sort.

Question: Is there any way to tell JPA that this or that column to sort should be wrapped with some function? How to do so?

英文:

Entity:

import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Table;
import java.time.LocalDateTime;
import java.util.UUID;

@Table( name = &quot;order&quot; )
public class Order
{
    @Id
    @Column( name = &quot;id&quot;, columnDefinition = &quot;BINARY(16)&quot; )
    private UUID id;

    @Column( name = &quot;posting_date&quot; )
    private LocalDateTime postingDate;

    @Column( name = &quot;created&quot; )
    private LocalDateTime created;

}

To search with sorting org.springframework.data.jpa.repository.JpaSpecificationExecutor#findAll(org.springframework.data.jpa.domain.Specification&lt;T&gt;, org.springframework.data.domain.Pageable) is used

Due to specific case I have to sort by two fields: postingDate and created and treat postingDate as date (not datetime like it is defined in entity), but I don't know how.

To make such a request to MySQL DB, DATE() function should be used, like this:

select
        order.*
    from
        order o
    where
      ...
    order by
        date(o.posting_date) desc,
        o.pae_created desc

But I want to use JPA methods to search and sort.

Question: Is there any way to tell JPA that this or that column to sort should be wrapped with some function? How to do so?

答案1

得分: 1

这从原则上是可行的。但我并没有运行以下的代码!另外,function 关键字是在 JPA 2.1 中添加的,如果你使用的是早期版本,这可能不起作用。

JPQL:

SELECT o FROM Order o WHERE ...
ORDER BY
  function('DATE', o.postingDate) DESC,
  o.created DESC

这在 Criteria API 中也是支持的。

英文:

This is, in principle doable. I did not run the following code though! Additionally, the function keyword was added to JPA 2.1, if you are using an earlier version this will probably not work.

JPQL:

SELECT o FROM Order o WHERE ...
ORDER BY
  function(&#39;DATE&#39;, o.postingDate) DESC,
  o.created DESC

This is also supported by the Criteria API.

huangapple
  • 本文由 发表于 2020年9月3日 00:59:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/63710247.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定