I want to have several Hibernate filters for the entity, I tried everything logically without any luck, and Google did not miss this, like the Hibernate document. I cannot imagine that this is impossible. (Using Java 6 Hibernate 4.1.9.final)
I currently have this:
@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<TestCase> 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; ... }
I want to add a second independent filter to the Testcase class. What I need is something like this:
Select ... From CATEGORY INNER JOIN TESTCASE on CATEGORY.CATEGORYID = TESTCASE.CATEGORYID Where TESTCASE.TESTRUNID in (....) and TESTCASE.TESTCASENAME like '%..%'
This is what I tried
I tried adding some @FilterDefs tags to TestCase, for example, but this did not compile:
@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; ... }
The Hibernate documentation led to trying something like this that complained that the testrunid filter did not exist
@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<TestCase> 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<Category> getCategories(List<Integer> testRunIDs, String category, String testCaseName) { Session session = getSession(); session.enableFilter("FILE_TYPE_FILTER"); if (testRunIDs != null && testRunIDs.size() != 0) { session.enableFilter("TEST_RUN_ID_FILTER").setParameterList("IDS", testRunIDs); } if (category != null && !category.equals("0") && !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<Category> result = query.list(); return result; .. }
Your help is greatly appreciated