我尝试构建由各种组件组成的Criteria。想象一下下面的映射类。
class Thing {
Inner inner;
String x;
}
class User {
Id; // mapped as id.
String first; // indexed columns etc
String last;
}
我想为User.first=@1和User.last=@2创建一个查询(aka Criterion)。我可以通过在用户的根目录下创建子标准然后将限制添加到此来轻松实现此目的。
Critera root = ...
root.add( Restrictions.eq( "xxx", @2 ));
Criteria user = root.createCritera("user");
user.add( Restrictions.eq("first", @1));
...etc
但是我不能使用类似于......的代码为事物和用户的属性创建AND。
Critera root = ...
root.add( Restrictions.and( Restrictions.eq("user.first", @1), Restrictions.eq("thing.x", @2));
抛出QueryException,表明“user.first”不是关联。在添加限制之前需要手动创建标准似乎不应该是必要的但是它是..尝试在第二种形式中使用别名也会失败。
答案 0 :(得分:2)
User
列有Thing
个?或Thing
列有User
s?
下面的示例指出User
有一个Thing
s列表:
Criteria user = root.createCritera("User");
user.add( Restrictions.eq("first", @1));
user.createAlias("usersSetOfThings", "userThings", Criteria.INNER_JOIN);
user.add(Restrictions.eq("userThings.x", @2));
user.list();
这些映射将使上面的代码运行:
<class name='User' table='your user table name'>
<id name='id' column='ID' type='long' />
<property name='first' column='FIRST' type='string' />
<property last='last' column='LAST' type='string' />
<set name='usersSetOfThings' table='what_did_you_do_to_link_them_in_the_DB?'>
<key column='id' />
<one-to-many column='whatever_you_did.user_id_fk_column_name'
class='full.package.name.Thing' />
</set>
</class>
<class name='thing' table='your things table name'>
<many-to-one name="Inner" class="full.package.name.Inner" fetch="select">
<column name="inner_FK" />
</many-to-one>
<property name='x' type='string' />
</class>