【发布时间】:2014-11-03 14:30:26
【问题描述】:
我是 Hibernate 的新手,我正在尝试动态加入更多表。我用三个表构建了一个示例:Employee、Address、Country。我想检索某个国家的所有员工。这是我的配置文件:
<class entity-name="Employee">
<id name="id" type="int" column="id">
<generator class="native"/>
</id>
<property name="firstName" column="first_name" type="string"/>
<property name="lastName" column="last_name" type="string"/>
<property name="salary" column="salary" type="int"/>
<many-to-one name="address" column="address" unique="true"
class="Address" not-null="true"/>
</class>
<class entity-name="Address">
<id name="id" type="int" column="id">
<generator class="native"/>
</id>
<property name="street" column="street_name" type="string"/>
<property name="city" column="city_name" type="string"/>
<property name="state" column="state_name" type="string"/>
<property name="zipcode" column="zipcode" type="string"/>
<many-to-one name="country" column="country" unique="true"
class="Country" not-null="true"/>
</class>
<class entity-name="Country">
<id name="id" type="int" column="id">
<generator class="native"/>
</id>
<property name="name" column="name" type="string"/>
<property name="code" column="code" type="string"/>
</class>
如果我这样做
List employees = session.createCriteria("Employee")
.createCriteria("address")
.createCriteria("country")
.add(Restrictions.eq("code","IT"))
.list();
我得到了正确的结果,但我的目标不是手动指定起始表和过滤表之间的整个路径:我希望 hibernate 为我完成这项工作。我想写这样的东西:
List employees = session.createCriteria("Employee")
.createCriteria("country")
.add(Restrictions.eq("code","IT"))
.list();
但我收到错误消息
org.hibernate.QueryException: could not resolve property: country of: Employee
【问题讨论】:
标签: java hibernate join criteria hibernate-criteria