我是Hibernate的新手,我試圖動態加入更多的表。我用三個表格構建了一個例子:員工,地址,國家。我想檢索某個國家的所有員工。在這裏我的配置文件:休眠:動態連接多個表
<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();
我得到正確的結果回來,但我的目標不是要手動指定開始表和篩選器表之間的整個路徑:我想是休眠爲我工作。我想編寫這樣的事:
List employees = session.createCriteria("Employee")
.createCriteria("country")
.add(Restrictions.eq("code","IT"))
.list();
,但我得到的錯誤信息
org.hibernate.QueryException: could not resolve property: country of: Employee
好的,我理解你的比較。關鍵是Hibernate有一個查詢(構建器)引擎,但它不能像我所想的那樣動態地在圖中的兩個實體之間找到路徑。 – Zardo 2014-11-11 09:55:30
你是對的。 Hibernate需要在查詢中加入一個從員工到國家/地區實體的路徑 – OndrejM 2015-01-07 11:19:19