小编典典

Hibernate:实体上的多个过滤器

hibernate

我想在一个实体上使用多个Hibernate过滤器,我尝试了所有没有失败的逻辑,Google以及Hibernate文档都对此提出了不足。我无法想象这是不可能的。(使用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 '%..%'

这是我尝试过的

我试图像这样向TestCase添加多个@FilterDefs,但是没有编译:

    @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;

..
}

非常感谢您的帮助


阅读 408

收藏
2020-06-20

共1个答案

小编典典

我实际上已经解决了,但感谢您的帮助。解决方案(下面详细介绍)是将多个@FilterDef批注包装在@FilterDef 批注中。奇怪的是,我没有在任何地方或Hibernate文档中找到它,我看到了这篇文章在一个元素上具有相同类型的多个注释?并认为可能@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;
    }
2020-06-20