title: Hibernate: Multiple filters on an entity
body: <p>I want to have multiple Hibernate filters on an entity, I have tried everything logical without luck and Google has come up short on this one, as has the Hibernate doc. I can't imagine that this is not possible. (Using Java 6 Hibernate 4.1.9.final)</p>
  
  <p><strong>Currently, I have this:</strong></p>
  
  <pre><code>@Entity
  @Table(name = "CATEGORY")
  public class Category implements Serializable
  {
      private static final long serialVersionUID = 1L;
  
      @Id
      @Column(name = "CATEGORYID")
      private int ID;
  
      @Column(name = "CATEGORYNAME")
      private String name;
  
      @OneToMany(fetch = FetchType.EAGER)
      @JoinColumn(name = "CATEGORYID")
      @OrderBy("TESTCASEID desc")
      @Filter(name = "TEST_RUN_ID_FILTER")
      private Collection&lt;TestCase&gt; testCases;
  ...
  }
  
  @Entity
  @Table(name = "TESTCASE_NEW")
  @FilterDef(name = "TEST_RUN_ID_FILTER", defaultCondition = "TESTRUNID in (:IDS)", parameters = { @ParamDef(name = "IDS", type = "int") })
  public class TestCase implements Serializable
  {
      private static final long serialVersionUID = 1L;
  
      @Id
      @Column(name = "TESTCASEID")
      private int ID;
  
      @Column(name = "TESTCASENAME")
      private String name;
  
  ...
  }
  </code></pre>
  
  <p>I want to add a second independent filter to the Testcase class. What I am after is something like this:</p>
  
  <pre><code>Select ...  
    From CATEGORY INNER JOIN TESTCASE on CATEGORY.CATEGORYID = TESTCASE.CATEGORYID
   Where TESTCASE.TESTRUNID in (....)
     and TESTCASE.TESTCASENAME like '%..%'
  </code></pre>
  
  <p><strong>This is what I tried</strong></p>
  
  <p>I tried adding multiple @FilterDefs to TestCase like such, but that didn't compile:</p>
  
  <pre><code>    @Entity
      @Table(name = "TESTCASE_NEW")
      @FilterDef(name = "TEST_RUN_ID_FILTER", defaultCondition = "TESTRUNID in (:IDS)", 
  parameters = { @ParamDef(name = "IDS", type = "int") })
      @FilterDef(name = "TESTCASE_NAME_FILTER", defaultCondition = "TESTCASENAME like :TESTCASE_NAME", 
  parameters = { @ParamDef(name = "TESTCASE_NAME", type = "string") })
  
      public class TestCase implements Serializable
      {
          private static final long serialVersionUID = 1L;
  
          @Id
          @Column(name = "TESTCASEID")
          private int ID;
  
          @Column(name = "TESTCASENAME")
          private String name;
  
      ...
      }
  </code></pre>
  
  <p>The Hibernate documentation led to to try something like this which complained the testrunid filter was non-existent </p>
  
  <pre><code>    @Entity
      @Table(name = "CATEGORY")
      public class Category implements Serializable
      {
          private static final long serialVersionUID = 1L;
  
          @Id
          @Column(name = "CATEGORYID")
          private int ID;
  
          @Column(name = "CATEGORYNAME")
          private String name;
  
          @OneToMany(fetch = FetchType.EAGER)
          @JoinColumn(name = "CATEGORYID")
          @OrderBy("TESTCASEID desc")
          private Collection&lt;TestCase&gt; testCases;
      ...
      }
  
  
         @Entity
      @Table(name = "TESTCASE_NEW")
      @FilterDef(name = "TESTCASE_FILTER", parameters = { @ParamDef(name = "IDS", type = "int"), @ParamDef(name = "TESTCASE_NAME", type = "string") })
      @Filters({ @Filter(name = "TEST_RUN_ID_FILTER", condition = "TESTRUNID in (:IDS)"), @Filter(name = "TESTCASE_NAME_FILTER", condition = "TESTCASENAME like :TESTCASE_NAME") })
      // @FilterDef(name = "TEST_RUN_ID_FILTER", defaultCondition = "TESTRUNID in (:IDS)", parameters = { @ParamDef(name =
      // "IDS", type = "int") })
      public class TestCase implements Serializable
      {
          private static final long serialVersionUID = 1L;
  
          @Id
          @Column(name = "TESTCASEID")
          private int ID;
  
          @Column(name = "TESTCASENAME")
          private String name;
          ...
      }
       @SuppressWarnings("unchecked")
          public List&lt;Category&gt; getCategories(List&lt;Integer&gt; testRunIDs, String category, String testCaseName)
          {
              Session session = getSession();
              session.enableFilter("FILE_TYPE_FILTER");
              if (testRunIDs != null &amp;&amp; testRunIDs.size() != 0)
              {
                  session.enableFilter("TEST_RUN_ID_FILTER").setParameterList("IDS", testRunIDs);
              }
              if (category != null &amp;&amp; !category.equals("0") &amp;&amp; !category.equals(""))
              {
                  session.enableFilter("CATEGORY_FILTER").setParameter("CATEGORY", category);
              }
  
              /*
               * Hibernate wants to do an (left) outer join be default.
               * This bit of HQL is required to get it to do an inner join.
               * The query tells Hibernate to do an inner join on the testCases property inside the Category object
               */
  
              Query query = session.createQuery("select distinct c from Category c inner join c.testCases tc");
              List&lt;Category&gt; result = query.list();
              return result;
  
  ..
  }
  </code></pre>
  
  <p>Your help is greatly appreciated</p>