Reputation: 1850
I have a very simple one to many relationship and want to filter the many side (collection) using an in clause. I can't get the filter working. Either Hibernate complains the filter parameter is undefined (when using Set or Integer as the type) or it says the values being passed in are of the wrong type (when using int the param type)
Relationship: A Category has many test cases, a test case has only one 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;
}
DAO
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;
}
Exception (when Set or Integer is param type):
Java.lang.IllegalArgumentException: Undefined filter parameter [IDS]
at org.hibernate.impl.FilterImpl.setParameter(FilterImpl.java:74)
Exception (when int is param type)
java.lang.IllegalArgumentException: Incorrect type for parameter [IDS]
at org.hibernate.impl.FilterImpl.setParameter(FilterImpl.java:77)
Upvotes: 1
Views: 7268
Reputation: 1446
FilterImpl.setParameter
can handle only singular parameter.
When it comes to pass over collection or array parameter,
use FilterImpl.setParameterList(name, values)
https://forum.hibernate.org/viewtopic.php?p=2410099
Upvotes: 9