我希望在一个实体上有多个Hibernate过滤器,我已经尝试了所有逻辑而没有运气,谷歌在这个问题上做得很短,就像Hibernate doc一样。我无法想象这是不可能的。 (使用Java 6 Hibernate 4.1.9.final)
目前,我有这个:
@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;
...
}
我想在Testcase类中添加第二个独立过滤器。我所追求的是这样的:
Select ...
From CATEGORY INNER JOIN TESTCASE on CATEGORY.CATEGORYID = TESTCASE.CATEGORYID
Where TESTCASE.TESTRUNID in (....)
and TESTCASE.TESTCASENAME like '%..%'
这是我试过的
我尝试将多个@FilterDefs添加到TestCase,但是没有编译:
@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;
...
}
Hibernate文档导致尝试这样的东西,抱怨testrunid过滤器不存在
@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;
..
}
非常感谢您的帮助
答案 0 :(得分:13)
我实际上已经解决了,但感谢您的帮助。解决方案(详见下文)是在@FilterDef s 注释中包含多个@FilterDef注释。奇怪的是,我没有在任何地方或在Hibernate doc中找到这个,我看到了这篇文章(Multiple annotations of the same type on one element?),并认为可能@FilterDefs存在而且确实如此。
@Entity
@Table(name = "TESTCASE_NEW")
@FilterDefs({
@FilterDef(name = "TESTCASE_NAME_FILTER", defaultCondition = "TESTCASENAME like :TESTCASENAME", parameters = { @ParamDef(name = "TESTCASENAME", type = "string") }),
@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;
...
}
@Entity
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")
@Filters({
@Filter(name = "TEST_RUN_ID_FILTER"),
@Filter(name = "TESTCASE_NAME_FILTER") })
private Collection<TestCase> testCases;
...
}
在DAO中,我只是打开我需要的那些
public List<Category> getCategories(List<Integer> testRunIDs, String category, String testCaseName)
{
Session session = getSession();
if (testRunIDs != null && testRunIDs.size() != 0)
{
session.enableFilter("TEST_RUN_ID_FILTER").setParameterList("IDS", testRunIDs);
}
if (testCaseName != null)
{
session.enableFilter("TESTCASE_NAME_FILTER").setParameter("TESTCASENAME", testCaseName);
}
/*
* 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;
}