title: Using a Hibernate filter with Spring Boot JPA
body: <p>I have found the need to limit the size of a child collection by a property in the child class.</p>
  
  <p>I have the following after following <a href="https://www.mkyong.com/hibernate/hibernate-data-filter-example-xml-and-annotation/" rel="nofollow noreferrer">this guide</a>:</p>
  
  <pre><code>@FilterDef(name="dateFilter", parameters=@ParamDef( name="fromDate", type="date" ) )
  public class SystemNode implements Serializable {
  
      @Getter
      @Setter
      @Builder.Default
      // "startTime" is a property in HealthHistory
      @Filter(name = "dateFilter", condition = "startTime &gt;= :fromDate")
      @OneToMany(mappedBy = "system", targetEntity = HealthHistory.class, fetch = FetchType.LAZY)
      private Set&lt;HealthHistory&gt; healthHistory = new HashSet&lt;HealthHistory&gt;();
  
      public void addHealthHistory(HealthHistory health) {
          this.healthHistory.add(health);
          health.setSystem(this);
      }
  }
  </code></pre>
  
  <p>However, I don't really understand how to toggle this filter when using Spring Data JPA. I am fetching my parent entity like this:</p>
  
  <pre><code>public SystemNode getSystem(UUID uuid) {
      return systemRepository.findByUuid(uuid)
          .orElseThrow(() -&gt; new EntityNotFoundException("Could not find system with id " + uuid));
  }
  </code></pre>
  
  <p>And this method in turn calls the Spring supported repository interface:</p>
  
  <pre><code>public interface SystemRepository extends CrudRepository&lt;SystemNode, UUID&gt; {
  
      Optional&lt;SystemNode&gt; findByUuid(UUID uuid);
  
  }
  </code></pre>
  
  <p>How can I make this filter play nicely together with Spring? I would like to activate it programatically <em>when I need it</em>, not globally. There are scenarios where it would be viable to disregard the filter.</p>
  
  <p>I am using Spring Boot <code>1.3.5.RELEASE</code>, I cannot update this at the moment.</p>