【问题标题】:Filters in HibernateHibernate 中的过滤器
【发布时间】:2013-03-19 00:31:56
【问题描述】:

我有一个非常简单的一对多关系,并希望使用 in 子句过滤多方(集合)。我无法让过滤器工作。 Hibernate 抱怨过滤器参数未定义(当使用 Set 或 Integer 作为类型时)或者它说传入的值的类型错误(当使用 int 参数类型时)

关系:一个Category有很多测试用例,一个测试用例只有一个category

Pojo#1

@Entity
@Table(name = "CATEGORY")
public class Category
{
    @Id
    @Column(name = "CATEGORYID")
    private int ID;

    @Column(name = "CATEGORYNAME")
    private String name;

    @OneToMany(fetch = FetchType.EAGER)
    @JoinColumn(name = "CATEGORYID")
    @Filter(name = "TEST_RUN_ID_FILTER")
    private Collection<SimpleTestCase> testCases;
}

Pojo #2

@Entity
@Table(name = "TESTCASE_NEW")
@FilterDef(name = "TEST_RUN_ID_FILTER", defaultCondition = "TESTRUNID in (:IDS)", parameters = { @ParamDef(name = "IDS", type = "Integer") })
public class SimpleTestCase
{
    @Id
    @Column(name = "TESTCASEID")
    private int ID;

    @Column(name = "TESTCASENAME")
    private String name;

    @Column(name = "STATUS")
    private String status;

    @Column(name = "TESTRUNID")
    private int testRunId;
}

public List<Category> getAllCategoriesForTestRuns(Set<Integer> testRunIDs)
{
    Session session = getSession();
    session.enableFilter("TEST_RUN_ID_FILTER")
            .setParameter("IDS", testRunIDs);
    Query query = session.createQuery("FROM " + Category.class.getSimpleName());
    List<Category> result = query.list();
    return result;
}

异常(当 Set 或 Integer 为参数类型时):

Java.lang.IllegalArgumentException: Undefined filter parameter [IDS]
at org.hibernate.impl.FilterImpl.setParameter(FilterImpl.java:74)

异常(当 int 为参数类型时)

java.lang.IllegalArgumentException: Incorrect type for parameter [IDS]
    at org.hibernate.impl.FilterImpl.setParameter(FilterImpl.java:77)

【问题讨论】:

    标签: hibernate filter annotations one-to-many


    【解决方案1】:

    FilterImpl.setParameter 只能处理单数参数。

    当涉及到传递集合或数组参数时,

    使用FilterImpl.setParameterList(name, values)

    https://forum.hibernate.org/viewtopic.php?p=2410099

    【讨论】:

    • 它一直盯着我的脸,一定要爱那些。太棒了,非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-24
    相关资源
    最近更新 更多