Java 休眠:实体上的多个筛选器
我想在一个实体上有多个Hibernate过滤器,我尝试了所有符合逻辑的方法,但运气不佳,Google在这方面做得不够,Hibernate文档也是如此。我无法想象这是不可能的。使用Java6Hibernate4.1.9.final 目前,我有: 这就是我试过的 我尝试像这样向TestCase添加多个@FilterDefs,但没有编译:Java 休眠:实体上的多个筛选器,java,hibernate,hibernate-filters,Java,Hibernate,Hibernate Filters,我想在一个实体上有多个Hibernate过滤器,我尝试了所有符合逻辑的方法,但运气不佳,Google在这方面做得不够,Hibernate文档也是如此。我无法想象这是不可能的。使用Java6Hibernate4.1.9.final 目前,我有: 这就是我试过的 我尝试像这样向TestCase添加多个@FilterDefs,但没有编译: @Entity @Table(name = "TESTCASE_NEW") @FilterDef(name = "TEST_RUN_ID_F
@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;
..
}
非常感谢您的帮助事实上我已经解决了,但谢谢您的帮助。下面详细介绍的解决方案是在@FilterDefs注释中包装多个@FilterDef注释。奇怪的是,我没有在任何地方或Hibernate文档中找到这篇文章,我看到了这篇文章,并认为hey maybee@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;
...
}
在刀中,我只打开我需要的
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;
}
+1.用户发布了他尝试的内容。+1。hibernate reference中有一个引用:我也错过了。仅供参考
@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;
...
}
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;
}