Hibernate Criteria查询Set列

时间:2015-04-27 08:59:37

标签: java mysql hibernate criteria

我的数据库中的 测试 列如下所示:

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));

1 个答案:

答案 0 :(得分:5)

考虑一下,您已创建标准为

Criteria criteria = session.createCriteria(TestCriteria.class, "testCriteria");

&安培; TestCriteria类具有名为tests的属性。然后,您可以创建与

相同的别名
criteria.createAlias("testCriteria.tests", "test");
criteria.add(Restrictions.eq("test", "TEST1"));

来自Hibernate Docs

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

希望这有帮助。