簡體   English   中英

休眠中的過濾器

[英]Filters in Hibernate

我有一個非常簡單的一對多關系,並希望使用in子句過濾多邊(集合)。 我無法使過濾器正常工作。 Hibernate抱怨filter參數未定義(當使用Set或Integer作為類型時),或者它說傳遞的值是錯誤的類型(當使用int param類型時)

關系:一個類別有很多測試用例,一個測試用例只有一個類別

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)

FilterImpl.setParameter只能處理單個參數。

當要傳遞集合或數組參數時,

使用FilterImpl.setParameterList(name, values)

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM