title: How do I use JPA to select the record with the closest date?
body: <p>Let's say I have a collection of Rates that all inherit from an <code>AbstractRate</code></p>
  
  <pre class="lang-java prettyprint-override"><code>@MappedSuperclass
  @Inheritance(strategy = InheritanceType.SINGLE_TABLE)
  @DiscriminatorColumn(name = "name")
  @Table(name = "rates")
  public abstract class AbstractRate {
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id;
      private String name;
      @Column(precision = 13, scale = 4)
      private BigDecimal value;
      @OneToOne
      private EffectiveDate effectiveDate;
      ...
  }
  </code></pre>
  
  <p>And, that all have an EffectiveDate:</p>
  
  <pre class="lang-java prettyprint-override"><code>@Entity
  public class EffectiveDate {
      @Id
      private LocalDateTime date;
      ...
  }
  </code></pre>
  
  <p>(I acknowledge that a separate date object is a little over-kill for this model, but it's allowing me to bind multiple rates and other figures in the database.)</p>
  
  <p>Now, I'd like to get a specific Rate, say <code>SalaryRate</code>, that is effective as of a certain date.  I can do something like</p>
  
  <pre class="lang-java prettyprint-override"><code>salaryRateRepository.findByEffectivedate(
       effectiveDateRepository.findTopByDateLessThanEqualOrderByDateDesc(sampleDate)
  );
  </code></pre>
  
  <p>This should effectively give me a the <code>MAX(date)</code> and its matching Rate.  Is this the right way to query these things?  Some posts suggest </p>
  
  <p>As an additional option, I have Querydsl setup and the repositories extend <code>QuerydslPredicateExecutor</code>.  However, I'm not really familiar with how Querydsl's syntax works.</p>