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

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

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

问题

Entity:

  1. import javax.persistence.Column;
  2. import javax.persistence.Id;
  3. import javax.persistence.Table;
  4. import java.time.LocalDateTime;
  5. import java.util.UUID;
  6. @Table(name = "order")
  7. public class Order {
  8. @Id
  9. @Column(name = "id", columnDefinition = "BINARY(16)")
  10. private UUID id;
  11. @Column(name = "posting_date")
  12. private LocalDateTime postingDate;
  13. @Column(name = "created")
  14. private LocalDateTime created;
  15. }

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:

  1. select
  2. order.*
  3. from
  4. order o
  5. where
  6. ...
  7. order by
  8. date(o.posting_date) desc,
  9. 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:

  1. import javax.persistence.Column;
  2. import javax.persistence.Id;
  3. import javax.persistence.Table;
  4. import java.time.LocalDateTime;
  5. import java.util.UUID;
  6. @Table( name = &quot;order&quot; )
  7. public class Order
  8. {
  9. @Id
  10. @Column( name = &quot;id&quot;, columnDefinition = &quot;BINARY(16)&quot; )
  11. private UUID id;
  12. @Column( name = &quot;posting_date&quot; )
  13. private LocalDateTime postingDate;
  14. @Column( name = &quot;created&quot; )
  15. private LocalDateTime created;
  16. }

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:

  1. select
  2. order.*
  3. from
  4. order o
  5. where
  6. ...
  7. order by
  8. date(o.posting_date) desc,
  9. 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:

  1. SELECT o FROM Order o WHERE ...
  2. ORDER BY
  3. function('DATE', o.postingDate) DESC,
  4. 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:

  1. SELECT o FROM Order o WHERE ...
  2. ORDER BY
  3. function(&#39;DATE&#39;, o.postingDate) DESC,
  4. 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:

确定