我的数据库中的 测试 列如下所示:
set('TEST1','TEST2','TEST3',....)
我正在尝试查询集合中的多个值。
我尝试了以下操作:
criteria.createAlias("tests", "test");
criteria.add(Restrictions.eq("test", "TEST1"));
但得到以下例外:
org.hibernate.QueryException: not an association: tests
无法弄清楚如何从“测试”设置中访问值。
我尝试的另一种方式是以下,因为我需要比较集合中的多个值,但它也不起作用:
Criterion c1 = Restrictions.like("tests", EnumSet.of("TEST1"));
Criterion c2 = Restrictions.like("tests", EnumSet.of("TEST2"));
criteria.add (Restrictions.or(c1, c2));
答案 0 :(得分:5)
考虑一下,您已创建标准为
Criteria criteria = session.createCriteria(TestCriteria.class, "testCriteria");
&安培; TestCriteria
类具有名为tests
的属性。然后,您可以创建与
criteria.createAlias("testCriteria.tests", "test");
criteria.add(Restrictions.eq("test", "TEST1"));
Criteria createAlias(String associationPath,
String alias)
throws HibernateException
Join an association, assigning an alias to the joined association.
Functionally equivalent to createAlias(String, String, JoinType ) using
JoinType.INNER_JOIN for the joinType.
Parameters:
associationPath - A dot-seperated property path
alias - The alias to assign to the joined association (for later reference).
Returns:
this (for method chaining)
Throws:
HibernateException - Indicates a problem creating the sub criteria
希望这有帮助。