<i id='1fUcy'><tr id='1fUcy'><dt id='1fUcy'><q id='1fUcy'><span id='1fUcy'><b id='1fUcy'><form id='1fUcy'><ins id='1fUcy'></ins><ul id='1fUcy'></ul><sub id='1fUcy'></sub></form><legend id='1fUcy'></legend><bdo id='1fUcy'><pre id='1fUcy'><center id='1fUcy'></center></pre></bdo></b><th id='1fUcy'></th></span></q></dt></tr></i><div id='1fUcy'><tfoot id='1fUcy'></tfoot><dl id='1fUcy'><fieldset id='1fUcy'></fieldset></dl></div>
    1. <legend id='1fUcy'><style id='1fUcy'><dir id='1fUcy'><q id='1fUcy'></q></dir></style></legend>
        <tfoot id='1fUcy'></tfoot>
          <bdo id='1fUcy'></bdo><ul id='1fUcy'></ul>

      1. <small id='1fUcy'></small><noframes id='1fUcy'>

        Spring Data JPA - 是否可以对计算的属性进行排序?

        时间:2024-08-24
          <tfoot id='z5dU3'></tfoot>

            <bdo id='z5dU3'></bdo><ul id='z5dU3'></ul>

              <tbody id='z5dU3'></tbody>
                <i id='z5dU3'><tr id='z5dU3'><dt id='z5dU3'><q id='z5dU3'><span id='z5dU3'><b id='z5dU3'><form id='z5dU3'><ins id='z5dU3'></ins><ul id='z5dU3'></ul><sub id='z5dU3'></sub></form><legend id='z5dU3'></legend><bdo id='z5dU3'><pre id='z5dU3'><center id='z5dU3'></center></pre></bdo></b><th id='z5dU3'></th></span></q></dt></tr></i><div id='z5dU3'><tfoot id='z5dU3'></tfoot><dl id='z5dU3'><fieldset id='z5dU3'></fieldset></dl></div>
                  <legend id='z5dU3'><style id='z5dU3'><dir id='z5dU3'><q id='z5dU3'></q></dir></style></legend>
                1. <small id='z5dU3'></small><noframes id='z5dU3'>

                  本文介绍了Spring Data JPA - 是否可以对计算的属性进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  假设您有以下实体:

                  @Entity
                  public class Game {
                  
                      @Id
                      @GeneratedValue
                      private Integer id;
                  
                      private String name;
                  
                      private Calendar startTime;
                      private int durationInSeconds;
                  
                      public GameStatus getStatus() {
                          if( startTime.after(Calendar.getInstance()))
                          {
                              return GameStatus.SCHEDULED;
                          } else {
                              Calendar endTime = Calendar.getInstance();
                              endTime.setTime(startTime.getTime());
                              endTime.roll(Calendar.SECOND, durationInSeconds);
                  
                              if( endTime.after(Calendar.getInstance())) {
                                  return GameStatus.OPEN_FOR_PLAY;
                              }
                              else {
                                  return GameStatus.FINISHED;
                              }
                          }
                      }
                  }
                  

                  如果我的 GameRepositoryPagingAndSortingRepository,我怎样才能获得按 status 属性排序的结果页面?

                  If my GameRepository is a PagingAndSortingRepository, how can I get a page of results, sorted by the status property?

                  我目前得到:

                  java.lang.IllegalArgumentException: Unable to locate Attribute  with the the 
                  given name [status] on this ManagedType [org.test.model.Game]
                  

                  我可以理解,因为 status 确实没有 JPA 属性.有没有办法解决这个问题?

                  Which I can understand since status is indeed no JPA attribute. Is there a way around this?

                  (我在下面使用 Hibernate,所以任何特定于 Hibernate 的东西也可以)

                  (I am using Hibernate underneath, so anything Hibernate specific is also ok)

                  推荐答案

                  问题是Spring Data的PageRequest排序是在数据库层通过形成ORDER BY子句来完成的.

                  The problem is that Spring Data's PageRequest sort is done on the database layer by forming the ORDER BY clause.

                  您可以创建一个@Formula 列,例如

                  You could create a @Formula column, e.g.

                  @Entity
                  public class Game {
                  ...
                       // rewrite your logic here in HQL
                       @Formula("case when startTime >= endTime then 'FINISHED' ... end")
                       private String status;
                  

                  然后可以按排序顺序使用新列,因为您在公式中编写的所有内容都将传递给 ORDER BY 子句.

                  Then it will be possible to use the new column in sort order as everything you write in the formula will be passed to ORDER BY clause.

                  这篇关于Spring Data JPA - 是否可以对计算的属性进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何将 Spring Boot @RepositoryRestResource 映射到特定的 url? 下一篇:Flutter 中的 Firebase (FCM) 注册令牌

                  相关文章

                  • <bdo id='8GUkm'></bdo><ul id='8GUkm'></ul>
                  1. <tfoot id='8GUkm'></tfoot>

                    <small id='8GUkm'></small><noframes id='8GUkm'>

                    1. <legend id='8GUkm'><style id='8GUkm'><dir id='8GUkm'><q id='8GUkm'></q></dir></style></legend>
                      <i id='8GUkm'><tr id='8GUkm'><dt id='8GUkm'><q id='8GUkm'><span id='8GUkm'><b id='8GUkm'><form id='8GUkm'><ins id='8GUkm'></ins><ul id='8GUkm'></ul><sub id='8GUkm'></sub></form><legend id='8GUkm'></legend><bdo id='8GUkm'><pre id='8GUkm'><center id='8GUkm'></center></pre></bdo></b><th id='8GUkm'></th></span></q></dt></tr></i><div id='8GUkm'><tfoot id='8GUkm'></tfoot><dl id='8GUkm'><fieldset id='8GUkm'></fieldset></dl></div>