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)
Currently, I 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 am after 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 multiple @FilterDefs to TestCase like such, but that didn't 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 to try something like this which complained the testrunid filter was non-existent
@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
I've solved it actually, but thanks for the help. The solution (detailed below) is to wrap multiple @FilterDef annotations in a @FilterDefs annotation. Oddly enough I didn't find this anywhere or in the Hibernate doc, I saw this post (Multiple annotations of the same type on one element?), and thought hey maybee @FilterDefs exists and it does.
In the DAO, I just turn on the ones I need