title: How to prevent sorting on a given column?
body: <p>My repository allows for sorting on any column when retrieving a list of users:</p>
  
  <pre><code>public interface UserRepository extends JpaRepository&lt;User, Long&gt;, UserRepositoryCustom {
  
      @Query("SELECT u FROM User u")
      public Page&lt;User&gt; all(Pageable page);
  
  }
  </code></pre>
  
  <p>The trouble is that a user has some properties that cannot offer sorting, like his <code>confirmedEmail</code> property.</p>
  
  <pre><code>@Entity
  @Table(name = "user_account")
  @SequenceGenerator(name = "id_generator", sequenceName = "user_account_id_seq", allocationSize = 10)
  public class User extends AbstractEntity {
  
      @Column(nullable = false)
      private String firstname;
      @Column(nullable = false)
      private String lastname;
      @Column(nullable = false, unique = true)
      private EmailAddress email;
      @Column(nullable = false)
      private boolean confirmedEmail;
  
  }
  </code></pre>
  
  <p>How can I prevent the <code>Pageable</code> argument from sorting on this boolean <code>confirmedEmail</code> property ?</p>
  
  <p>I stumbled upon this issue when I sorted by clicking on the <code>Confirmed</code> header in my <code>Angular</code> data table.</p>
  
  <p>This front-end event triggered the following request:</p>
  
  <pre><code>SELECT u FROM com.thalasoft.user.data.jpa.domain.User u order by u.confirmed asc
  </code></pre>
  
  <p>I know I can make this data table column not sortable, and I did. But I'd like to also have a server side safety in place.</p>
  
  <p>As a side note, I wonder if I could also provide a default sorting if none is specified in the client request.</p>
  
  <p>UPDATE: I created the utility method:</p>
  
  <pre><code>  public static final Sort stripColumnsFromSorting(Sort sort, Set&lt;String&gt; nonSortableColumns) {
      return Sort.by(sort.stream().filter(order -&gt; {
        return !nonSortableColumns.contains(order.getProperty());
      }).collect(Collectors.toList()));
    }
  </code></pre>
  
  <p>which I call like:</p>
  
  <pre><code>Set&lt;String&gt; nonSortableColumns = new HashSet&lt;String&gt;(Arrays.asList("id", "confirmedEmail"));
  
  public ResponseEntity&lt;PagedResources&lt;UserResource&gt;&gt; all(@PageableDefault(sort = { "lastname", "firstname" }, direction = Sort.Direction.ASC) Pageable pageable, Sort sort,
          PagedResourcesAssembler&lt;User&gt; pagedResourcesAssembler, UriComponentsBuilder builder) {
      sort = CommonUtils.stripColumnsFromSorting(sort, nonSortableColumns);
      userService.addSortToPageable(pageable, sort);
  </code></pre>
  
  <p>But it is still invoking the sorting on the non sortable column:</p>
  
  <pre><code>Invoking 'com.thalasoft.user.rest.controller.UserController.all' with arguments [Page request [number: 0, size 5, sort: confirmedEmail: DESC], confirmedEmail: DESC, org.springframework.data.web.MethodParameterAwarePagedResourcesAssembler@78817e7e, org.springframework.web.servlet.support.ServletUriComponentsBuilder@3e49647a]
  </code></pre>