title: Spring Data Repository: "find" method with orderBy gets the wrong order
body: <p>I have a Spring Boot application with some JPA repositories.
  I defined the following method inside one of the repository interfaces:</p>
  
  <pre><code>    public List&lt;Post&gt; findAllByOrderByPublishedOnDesc();
  </code></pre>
  
  <p>Quite obviously, I would like to retrive the list of all the <code>Post</code>s sorted by <code>publishOn</code> DESC. Strangely, I get the order wrong when two or more of the posts have a value of <code>publishedOn</code> that differs only for the minute component of the LocalDateTime class (see the comments in the code below).
  <strong>What Am I doing wrong?</strong></p>
  
  <p>Here's the example code. The test is executed with the H2 database:  </p>
  
  <p>Entity:</p>
  
  <pre><code>    @Entity
      public class Post
      {
          @Id
          @GeneratedValue(strategy = GenerationType.AUTO)
          Long id;
  
          @Column(unique = true, nullable = false)
          String sourceUrl;
  
          @Column
          String title;
  
          @Column
          LocalDateTime publishedOn;
  
          @LastModifiedDate
          LocalDateTime editedOn;
  
          @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
          Text text;
      }
  </code></pre>
  
  <p>Repository:</p>
  
  <pre><code>    public interface PostRepository extends JpaRepository&lt;Post, Long&gt;
      {
          public Post findBySourceUrl(String sourceUrl);
          public List&lt;Post&gt; findAllByOrderByPublishedOnDesc();
      }
  </code></pre>
  
  <p>Test:</p>
  
  <pre><code>    public class OrderByJPATest
      {
      @Autowired
      PostRepository postRepo;
  
      public OrderByJPATest()
      {
      }
  
      @Test
      public void testOrderByDays()
      {
              // First try: older by 1 day
              Post newer = insertTestEntity("newer", LocalDateTime.of(2016, 11, 13, 0, 0));
              Post older = insertTestEntity("older", LocalDateTime.of(2016, 11, 12, 0, 0));
  
              List&lt;Post&gt; ordered = postRepo.findAllByOrderByPublishedOnDesc();
  
              ordered.stream().forEach((post) -&gt; log.info("{} =&gt; {}", post.getSourceUrl(), post.getPublishedOn()));
  
              /*
              output:
              newer =&gt; 2016-11-13T00:00
              older =&gt; 2016-11-12T00:00
              */
  
              assertTrue(ordered.get(0).getPublishedOn()
                      .isAfter(ordered.get(1).getPublishedOn()));
  
              postRepo.deleteAll();
              postRepo.flush();
  
              // Second try: older by 1 minute
              newer = insertTestEntity("newerBy1Min", LocalDateTime.of(2016, 11, 13, 01, 02));
              older = insertTestEntity("olderBy1Min", LocalDateTime.of(2016, 11, 13, 01, 01));
  
              ordered = postRepo.findAllByOrderByPublishedOnDesc();
  
              ordered.stream().forEach((post) -&gt; log.info("{} =&gt; {}", post.getSourceUrl(), post.getPublishedOn()));
  
              /* 
              output:
              olderBy1Min =&gt; 2016-11-13T01:01
              newerBy1Min =&gt; 2016-11-13T01:02
              */
  
              // this assert fails!
              assertTrue(ordered.get(0).getPublishedOn()
                      .isAfter(ordered.get(1).getPublishedOn()));
      }
  
      private Post insertTestEntity(String url, LocalDateTime publishDate)
      {
              Text text = new Text();
              text.setValue("Testing...");
              Post post = new Post();
              post.setPublishedOn(publishDate);
              post.setSourceUrl(url);
              post.setText(text);
  
              return postRepo.save(post);
      }
      }
  </code></pre>
  
  <p>POM (dependencies):</p>
  
  <pre><code>   &lt;dependencies&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;com.atlassian.commonmark&lt;/groupId&gt;
              &lt;artifactId&gt;commonmark&lt;/artifactId&gt;
              &lt;version&gt;0.6.0&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;commons-io&lt;/groupId&gt;
              &lt;artifactId&gt;commons-io&lt;/artifactId&gt;
              &lt;version&gt;2.4&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;commons-lang&lt;/groupId&gt;
              &lt;artifactId&gt;commons-lang&lt;/artifactId&gt;
              &lt;version&gt;2.6&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;!--Additional dependencies -end--&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-data-rest&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
              &lt;artifactId&gt;lombok&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-security&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-thymeleaf&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-logging&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.thymeleaf.extras&lt;/groupId&gt;
              &lt;artifactId&gt;thymeleaf-extras-java8time&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;com.h2database&lt;/groupId&gt;
              &lt;artifactId&gt;h2&lt;/artifactId&gt;
              &lt;scope&gt;runtime&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.security&lt;/groupId&gt;
              &lt;artifactId&gt;spring-security-test&lt;/artifactId&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
  </code></pre>