1

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

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

波乔#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;
}

波乔#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)
4

1 回答 1

9

FilterImpl.setParameter只能处理奇异参数。

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

采用FilterImpl.setParameterList(name, values)

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

于 2013-03-19T06:24:48.280 回答