使用DetachedCriteria 查詢,當(dāng)使用多表關(guān)聯(lián)查詢時(shí)只能通過其他表的主鍵設(shè)置值,非主鍵進(jìn)行關(guān)聯(lián)就提示
could not resolve property: XXX of XXX
例如:
c.add(Restrictions.eq("customer.id", customerId));
其中customer是本實(shí)體的一個(gè)屬性,設(shè)置關(guān)聯(lián).id 為customer的主鍵
這種方式可以進(jìn)行查詢,并得到正確的結(jié)果.
c.add(Restrictions.eq("customer.number", number))
這里的number 是customer 的非主鍵屬性,這種方式查詢時(shí)提示 could not resolve property customer.number of xxx.
經(jīng)過測(cè)試表明,hibernate 應(yīng)該是不支持這種非主鍵設(shè)置值進(jìn)行查詢。但是使用nameQuery 寫語(yǔ)句的方式卻是可以查詢并得到正確結(jié)果.
語(yǔ)句為:
select x from X as x where customer.number =?
誰(shuí)能合解釋一下呢?為什么呢?