java 复合键的休眠条件问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6120852/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hibernate criteria problem with composite key
提问by Dytut
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>
This is a simple composite key mapping with a relation to table productCoverage and a composite key relation to productterm.
这是一个简单的复合键映射,与表 productCoverage 有关系,复合键与 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();
}
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:
这应该让我对“id”(它是主键,CoverageTermPK)创建一个子标准并对其添加限制,但是当我运行它时,我收到错误消息:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.QueryException: could not resolve property: productTerm of: CoverageTerm
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.
这感觉很奇怪,难道它不应该在那里获得 CoverageTermPK 吗?如果我尝试在“数据”属性上使用子标准,则标准有效,我似乎无法在“id”子标准上获得 PK。
Any ideas as to why this is happening?
关于为什么会发生这种情况的任何想法?
回答by Alex Gitelman
Not sure about your specific class structure but try to add id this way instead of separate criteria:
不确定您的特定类结构,但尝试以这种方式添加 id 而不是单独的标准:
Restrictions.eq("id.productTerm", productTerm);
Restrictions.eq("id.productCoverage", productCoverage);