问题描述
我得到了这个休眠映射:
I got this hibernate mapping:
<class name="CoverageTerm" table="coverage_term">
<composite-id name="id" class="CoverageTermPK">
<key-many-to-one name="productTerm" class="ProductTerm">
<column name="termtype_id"></column>
<column name="product_id" ></column>
</key-many-to-one>
<key-many-to-one name="productCoverage" class="ProductCoverage" column="product_coverage_id"></key-many-to-one>
</composite-id>
<property name="data"/>
</class>
这是一个简单的组合键映射,具有与表productCoverage的关系以及与productterm的组合键.
This is a simple composite key mapping with a relation to table productCoverage and a composite key relation to productterm.
现在问题出在我的搜索功能中:
Now the problem comes in my search function:
public CoverageTerm getCoverageTermFromProductTermCoverage(ProductTerm productTerm, ProductCoverage productCoverage) {
Criteria critCt = getSession().createCriteria(CoverageTerm.class);
Criteria critCtId = critCt.createCriteria("id");
critCtId.add(Restrictions.eq("productTerm", productTerm));
critCtId.add(Restrictions.eq("productCoverage", productCoverage));
return (CoverageTerm) critCt.uniqueResult();
}
这应该让我在"id"(这是主键CoverageTermPK)上创建一个子条件,并对其添加限制,但是当我运行它时,我得到了错误消息:
This should let me make a subcriteria on "id" (which is the primary key, CoverageTermPK) and add restrictions on it, but when I run it I get the error message:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.QueryException: could not resolve property: productTerm of: CoverageTerm
这感觉很奇怪,难道不应该在那儿得到CoverageTermPK吗?如果我尝试在数据"属性上使用一个子条件,则该条件有效,我似乎无法在"id"子条件上获得PK.
This feels strange, shouldn't it get the CoverageTermPK there? If I try with a subcriteria on the "data" property the criterias work, I just don't seem to be able to get the PK on the "id" subcriteria.
关于为什么发生这种情况的任何想法吗?
Any ideas as to why this is happening?
推荐答案
不确定您的特定类结构,但尝试以这种方式添加id而不是单独的条件:
Not sure about your specific class structure but try to add id this way instead of separate criteria:
Restrictions.eq("id.productTerm", productTerm);
Restrictions.eq("id.productCoverage", productCoverage);
这篇关于组合键的休眠条件问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!